package com.its.vms.xnettcp.vms.handler;

import com.its.app.utils.NettyUtils;
import com.its.vms.xnettcp.vms.process.TcpServerRecvData;
import com.its.vms.xnettcp.vms.process.TcpServerRecvDataProcess;
import com.its.vms.xnettcp.vms.protocol.impl.VmsResFramePacket;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
@ChannelHandler.Sharable
public class VmsServerPacketInboundHandler extends ChannelInboundHandlerAdapter {

    private final TcpServerRecvDataProcess tcpServerRecvDataProcess;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (!(msg instanceof VmsResFramePacket)) {
            String ipAddress = NettyUtils.getRemoteIpAddress(ctx.channel());
            log.error("[{}] | VmsServerPacketInboundHandler: Received Data is not VmsServerPacketInboundHandler Object", ipAddress);
            return;
        }

        VmsResFramePacket framePacket = (VmsResFramePacket)msg;
        this.tcpServerRecvDataProcess.add(new TcpServerRecvData(TcpServerRecvData.DATA_TYPE_RES_PACKET, framePacket.getVmsObj(), ctx, framePacket));
    }

}
