package org.pixelndice.table.pixelclient.connection.lobby.server

import org.apache.logging.log4j.LogManager
import org.pixelndice.table.pixelprotocol.ChannelCanceledException
import org.pixelndice.table.pixelprotocol.Protobuf

private val logger = LogManager.getLogger(State00Start::class.java)

class State00Start : State {

    override fun process(ctx: Context) {

        val packet: Protobuf.Packet? = ctx.channel.peekPacket()

        if( packet != null){

            when(packet.payloadCase){
                Protobuf.Packet.PayloadCase.PING -> {
                    ctx.state = State01WaitingPing()
                }
                Protobuf.Packet.PayloadCase.LOBBYAUTH -> {
                    ctx.state = State02WaitingLobbyAuth()
                }
                else -> {
                    val message = "Expecting ping or LobbyAuth, instead received: $packet. IP: ${ctx.channel.address}"
                    logger.error(message)

                    ctx.state = StateStop()
                }
            }
        }
    }
}