package com.tsi.comm.server.vo;

import com.tsi.comm.server.dto.TsiCvimDto;
import com.tsi.comm.server.protocol.TsiCpuPacket;
import com.tsi.common.utils.Counter;
import io.netty.channel.Channel;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Getter
@Setter
public class TsiNodeVo {
    private long nodeId;
    private String ipAddr;
    private boolean sendTest;
    private boolean sendNode;
    private boolean sendCvim;
    private boolean addNodeEnabled;

    private Channel channel;

    private boolean registered;         // Database 등록 여부, DB에 등록안되고 통신으로 접속하는 경우 false
    private boolean installed;
    private boolean checkInstalled;

    private Counter connectCount    = new Counter();
    private Counter disconnectCount = new Counter();
    private Counter recvPackets = new Counter();
    private Counter sendPackets = new Counter();

    private TsiCvimDto tsiCvimDto;

    TsiNodeAddVo tsiNodeAddVo;

    protected long connectTm;
    protected long disconnectTm;
    protected long lastCommTm;

    protected long recvTime;
    protected long handleTime;
    protected long queueTime;
    protected long endTime;

    private byte[] rawBytes = new byte[1];
    private byte[] nodePacket = new byte[1];

    private boolean dump;
    private int idx;

    private int pktQIdx;
    private int dbmsQIdx;
    private int logQIdx;

    public TsiNodeVo(long nodeId, String ipAddr, boolean sendTest, boolean sendNode, boolean sendCvim) {
        this.nodeId = nodeId;
        this.ipAddr = ipAddr;
        this.sendTest = sendTest;
        this.sendNode = sendNode;
        this.sendCvim = sendCvim;
        this.addNodeEnabled = false;

        this.channel = null;
        this.installed = true;
        this.checkInstalled = true;

        this.connectTm = 0;
        this.disconnectTm = 0;
        this.lastCommTm = 0;
        this.dump = false;
        this.idx = 0;
        this.pktQIdx = 0;
        this.dbmsQIdx = 0;
        this.logQIdx = 0;
    }

    public void initPacket() {
        this.tsiCvimDto = new TsiCvimDto();
        this.tsiCvimDto.setNodeId(this.nodeId);
    }

    public boolean isConnect() {
        return (this.channel != null);
    }

    public void setConnect(Channel channel) {
        this.channel = channel;
        if (channel != null) {
            this.connectCount.increment();
            this.connectTm = System.currentTimeMillis();
        }
        else {
            this.disconnectCount.increment();
            this.disconnectTm = System.currentTimeMillis();
        }
    }

    public String getKey() {
        return String.valueOf(this.nodeId);
    }

    public byte[] getTestPacket() {
        return null;//this.packet;
    }
    public byte[] getCvimPacket() {
        return null;//this.packet;
    }

    public void makeNodePacket(int length) {
        this.nodePacket = null;
        if (length > TsiCpuPacket.INDEX_NODE_ID) {
            // STX1, STX2, LEN(2), OpCode(1), Version(1), NodeId
            // LEN = LEN ~ CHECK SUM
            int packetLen = length-2-1-1-2;
            this.nodePacket = new byte[packetLen];
            try {
                //System.arraycopy(this.packet, 6, this.nodePacket, 0, packetLen);
            }
            catch(Exception e) {
            }
        }
        else {
            log.warn("makeNodePacket: The length must be at least {} bytes: {}, {}", TsiCpuPacket.INDEX_NODE_ID, this.nodeId, length);
        }
    }

}
