package net.minecraft.server.network;

import net.minecraft.network.chat.Component;
import net.minecraft.network.PacketListener;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.SharedConstants;
import net.minecraft.network.ConnectionProtocol;
import net.minecraft.network.protocol.handshake.ClientIntentionPacket;
import net.minecraft.network.Connection;
import net.minecraft.server.MinecraftServer;
import net.minecraft.network.protocol.handshake.ServerHandshakePacketListener;

public class ServerHandshakePacketListenerImpl implements ServerHandshakePacketListener {
    private final MinecraftServer server;
    private final Connection connection;
    
    public ServerHandshakePacketListenerImpl(final MinecraftServer minecraftServer, final Connection kt) {
        this.server = minecraftServer;
        this.connection = kt;
    }
    
    @Override
    public void handleIntention(final ClientIntentionPacket re) {
        switch (re.getIntention()) {
            case LOGIN: {
                this.connection.setProtocol(ConnectionProtocol.LOGIN);
                if (re.getProtocolVersion() > SharedConstants.getCurrentVersion().getProtocolVersion()) {
                    final Component lf3 = new TranslatableComponent("multiplayer.disconnect.outdated_server", new Object[] { SharedConstants.getCurrentVersion().getName() });
                    this.connection.send(new ClientboundLoginDisconnectPacket(lf3));
                    this.connection.disconnect(lf3);
                    break;
                }
                if (re.getProtocolVersion() < SharedConstants.getCurrentVersion().getProtocolVersion()) {
                    final Component lf3 = new TranslatableComponent("multiplayer.disconnect.outdated_client", new Object[] { SharedConstants.getCurrentVersion().getName() });
                    this.connection.send(new ClientboundLoginDisconnectPacket(lf3));
                    this.connection.disconnect(lf3);
                    break;
                }
                this.connection.setListener(new ServerLoginPacketListenerImpl(this.server, this.connection));
                break;
            }
            case STATUS: {
                this.connection.setProtocol(ConnectionProtocol.STATUS);
                this.connection.setListener(new ServerStatusPacketListenerImpl(this.server, this.connection));
                break;
            }
            default: {
                throw new UnsupportedOperationException("Invalid intention " + re.getIntention());
            }
        }
    }
    
    @Override
    public void onDisconnect(final Component lf) {
    }
    
    @Override
    public Connection getConnection() {
        return this.connection;
    }
}
