package org.openrs2.game.net

import io.netty.channel.Channel
import io.netty.channel.ChannelInitializer
import io.netty.handler.timeout.IdleStateHandler
import org.openrs2.game.net.login.LoginChannelHandler
import org.openrs2.protocol.Protocol
import org.openrs2.protocol.Rs2Decoder
import org.openrs2.protocol.Rs2Encoder
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton

@Singleton
public class Rs2ChannelInitializer @Inject constructor(
    private val handlerProvider: Provider<LoginChannelHandler>
) : ChannelInitializer<Channel>() {
    override fun initChannel(ch: Channel) {
        ch.pipeline().addLast(
            IdleStateHandler(true, TIMEOUT_SECS, TIMEOUT_SECS, TIMEOUT_SECS, TimeUnit.SECONDS),
            Rs2Decoder(Protocol.LOGIN_UPSTREAM),
            Rs2Encoder(Protocol.LOGIN_DOWNSTREAM),
            handlerProvider.get()
        )
    }

    private companion object {
        /*
         * On the OSRS servers, login connections take 60 seconds to time out,
         * but a JS5 connection only takes 30 seconds. It'd be awkward for us
         * to change the timeouts at runtime to fully emulate that behaviour.
         */
        private const val TIMEOUT_SECS: Long = 30
    }
}
