package com.its.dsrc.xnettcp.dsrc.handler;

import com.beanit.asn1dsrc.dsrc.C2CAuthenticatedMessage;
import com.its.app.utils.NettyUtils;
import com.its.dsrc.xnettcp.dsrc.process.TcpServerData;
import com.its.dsrc.xnettcp.dsrc.process.TcpServerDataProcess;
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 DsrcAsn1ServerPacketInboundHandler extends ChannelInboundHandlerAdapter {

    private final TcpServerDataProcess tcpServerDataProcess;

    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (!(msg instanceof C2CAuthenticatedMessage)) {
            log.error("{} | Received Data is not C2CAuthenticatedMessage", NettyUtils.getRemoteAddress(ctx.channel()));
            return;
        }
//        C2CAuthenticatedMessage c2c = (C2CAuthenticatedMessage)msg;
//        log.info("DsrcAsn1ServerPacketHandler: channelRead: {}, {}", NettyUtils.getRemoteAddress(ctx.channel()), c2c);
        this.tcpServerDataProcess.add(new TcpServerData(TcpServerData.DATA_TYPE_PACKET, null, ctx, msg));
    }
}
