package com.tsi.comm.server.process.packet;

import com.tsi.comm.server.protocol.TsiCpuPacket;
import com.tsi.comm.server.vo.TsiNodeVo;
import com.tsi.common.utils.SysUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

@Slf4j
public class TsiChannelSession {

    private TsiChannelSession() {
        // Private constructor to prevent instantiation
    }

    public static void sessionActive(TsiNodeVo nodeVo, String remoteIpAddress) {
        try {
            if (nodeVo != null) {
                final String logKey = nodeVo.getKey();
                MDC.put("id", logKey);
                log.info("Node: {}, ---Connect, Connects: {}, {}", nodeVo.getNodeId(), nodeVo.getConnectCount().get(), remoteIpAddress);
                MDC.clear();
            }
        }
        catch (Exception e) {
            // Log the exception with a meaningful message
        }
    }

    public static void sessionInactive(TsiNodeVo nodeVo, String remoteIpAddress) {
        try {
            if (nodeVo != null) {
                final String logKey = nodeVo.getKey();
                MDC.put("id", logKey);
                log.info("Node: {}, Disconnect, Connects: {}, {}", nodeVo.getNodeId(), nodeVo.getConnectCount().get(), remoteIpAddress);
                MDC.clear();
            }
        }
        catch (Exception e) {
            // Log the exception with a meaningful message
        }
    }

    public static void objectRegistered(TsiNodeVo nodeVo, String remoteIpAddress) {
        try {
            if (nodeVo != null) {
                final String logKey = nodeVo.getKey();
                MDC.put("id", logKey);
                log.info("Node: {}, Registered, Connects: {}, {}", nodeVo.getNodeId(), nodeVo.getConnectCount().get(), remoteIpAddress);
                MDC.clear();
            }
        }
        catch (Exception e) {
            // Log the exception with a meaningful message
        }
    }

    public static void sessionTimeout(TsiNodeVo nodeVo, String remoteIpAddress) {
        try {
            if (nodeVo != null) {
                final String logKey = nodeVo.getKey();
                MDC.put("id", logKey);
                log.info("Node: {}, RcvTimeout, Connects: {}, {}", nodeVo.getNodeId(), nodeVo.getConnectCount().get(), remoteIpAddress);
                MDC.clear();
            }
        }
        catch (Exception e) {
            // Log the exception with a meaningful message
        }
    }

    public static void recvPacketDump(TsiNodeVo nodeVo, TsiCpuPacket packet) {
        try {
            if (nodeVo != null) {
                final String logKey = nodeVo.getKey();
                MDC.put("id", logKey);
                byte[] buf = packet.getBuf();
                if (buf == null || buf.length == 0) {
                    log.info("RECV: {}, null bytes.", nodeVo.getNodeId());
                }
                else {
//                    log.info("RECV: {}, {} bytes, {}", nodeVo.getNodeId(), buf.length, HexString.fromBytes(buf));
                    log.info("RECV: {}, {} bytes, {}", nodeVo.getNodeId(), buf.length, SysUtils.byteArrayToHex(packet.getBuf()));
                }
                MDC.clear();
            }
        }
        catch (Exception e) {
            // Log the exception with a meaningful message
        }
    }
}
