package com.its.pis.xnettcp.vds.handler;

import com.its.app.utils.NettyUtils;
import com.its.pis.xnettcp.vds.process.VdsData;
import com.its.pis.xnettcp.vds.process.VdsDataProcess;
import com.its.pis.xnettcp.vds.protocol.VdsResFramePacket;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@ChannelHandler.Sharable
public class VdsTcpClientInboundHandler extends ChannelInboundHandlerAdapter {

    private final VdsDataProcess vdsDataProcess;

    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        String ipAddress = NettyUtils.getRemoteIpAddress(ctx.channel());
        if (!(msg instanceof VdsResFramePacket)) {
            log.error("[{}] | Received Data is not VdsResFramePacket Object", NettyUtils.getRemoteIpAddress(ctx.channel()));
            return;
        }

        VdsResFramePacket resFramePacket = (VdsResFramePacket)msg;
        this.vdsDataProcess.add(new VdsData(resFramePacket.getVdsObj(), ipAddress, ctx, resFramePacket));
    }

}
