package jguard.client.network;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import cucky.jGuard.lib.LogFile;
import cucky.jGuard.lib.Message;
import jguard.client.AutoConnect;
import jguard.client.Client;
import jguard.client.ClientMessageParser;
import jguard.client.gui.Window;

public class ClientAdapterHandler extends SimpleChannelInboundHandler<Message> {

    @Override
    protected void channelRead0(ChannelHandlerContext chc, Message msg) throws Exception {
        Channel currentChannel = chc.channel();
        new ClientMessageParser().parse(msg);
    }
    
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx); 
        Channel currentChannel = ctx.channel();
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext chc) throws Exception {
        super.channelUnregistered(chc); 
        LogFile.printInfo("Odpojeno");
        Window.setStatusBarMessage("Odpojeno");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx); 
    }
    
    @Override
    public void exceptionCaught(ChannelHandlerContext chc, Throwable cause) throws Exception {
        LogFile.printInfo("Server nedostupny: " + cause.getMessage());
        new AutoConnect().run();
    }

}