package com.inspect.patrol.linkage.frame;

import com.inspect.base.core.utils.ByteUtil;
import com.inspect.base.core.utils.HexUtils;
import com.inspect.patrol.linkage.util.Cp56Time2aUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Getter;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date;

@Getter
public class LinkageSignByte {
    private byte[] type;
    private byte[] stationNo;
    private byte[] monitorIndexNo;
    private byte[] linkageType;
    private byte[] linkageAttributeLen;
    private byte[] linkageAttribute;
    private byte[] linkageValueLen;
    private byte[] linkageValue;
    private byte[] linkageTime;
    private byte[] sumCheck;

    public LinkageSignByte() {
    }

    public LinkageSignByte(byte[] bytes) {
        this.type = Arrays.copyOfRange(bytes, 0, 5);
        this.stationNo = Arrays.copyOfRange(bytes, 5, 7);
        this.monitorIndexNo = Arrays.copyOfRange(bytes, 7, 9);
        this.linkageType = Arrays.copyOfRange(bytes, 9, 10);
        this.linkageAttributeLen = Arrays.copyOfRange(bytes, 10, 11);
        byte dataAttrLenInt = this.linkageAttributeLen[0];
        this.linkageAttribute = Arrays.copyOfRange(bytes, 11, 11 + dataAttrLenInt);
        this.linkageValueLen = Arrays.copyOfRange(bytes, 11 + dataAttrLenInt, 12 + dataAttrLenInt);
        byte dataLen = this.linkageValueLen[0];
        this.linkageValue = Arrays.copyOfRange(bytes, 12 + dataAttrLenInt, 12 + dataAttrLenInt + dataLen);
        this.linkageTime = Arrays.copyOfRange(bytes, bytes.length - 8, bytes.length - 1);
        this.sumCheck = Arrays.copyOfRange(bytes, bytes.length - 1, bytes.length);
    }

    public void setType(String type) {
        this.type = HexUtils.hexToByteArray(type);
    }

    public void setStationNo(short stationNo) {
        this.stationNo = ByteUtil.getBytes(stationNo);
    }

    public void setMonitorIndexNo(short monitorIndexNo) {
        this.monitorIndexNo = ByteUtil.getBytes(monitorIndexNo);
    }

    public void setLinkageType(int linkageType) {
        this.linkageType = new byte[]{(byte) linkageType};
    }

    public void setLinkageAttributeLen(int linkageAttributeLen) {
        this.linkageAttributeLen = new byte[]{(byte) linkageAttributeLen};
    }

    public void setLinkageAttribute(String linkageAttribute) {
        this.linkageAttribute = linkageAttribute.getBytes(Charset.forName("GB2312"));
    }

    public void setLinkageValueLen(int linkageValueLen) {
        this.linkageValueLen = new byte[]{(byte) linkageValueLen};
    }

    public void setLinkageValue(String linkageValue) {
        this.linkageValue = linkageValue.getBytes(Charset.forName("GB2312"));
    }

    public void setLinkageTime(Date linkageTime) {
        this.linkageTime = Cp56Time2aUtil.toBytes(linkageTime);
    }

    public void setSumCheck(int sumCheck) {
        this.sumCheck = new byte[]{(byte) sumCheck};
    }

    public LinkageSignEntity toLinkageSignEntity() {
        LinkageSignEntity linkageSignEntity = new LinkageSignEntity();
        linkageSignEntity.setType(this.type);
        linkageSignEntity.setStationNo(this.stationNo);
        linkageSignEntity.setMonitorIndexNo(this.monitorIndexNo);
        linkageSignEntity.setLinkageType(this.linkageType);
        linkageSignEntity.setLinkageAttributeLen(this.linkageAttributeLen);
        linkageSignEntity.setLinkageAttribute(this.linkageAttribute);
        linkageSignEntity.setLinkageValueLen(this.linkageValueLen);
        linkageSignEntity.setLinkageValue(this.linkageValue);
        linkageSignEntity.setLinkageTime(this.linkageTime);
        linkageSignEntity.setSumCheck(this.sumCheck);
        return linkageSignEntity;
    }

    public byte[] toBytes() {
        this.setSumCheck(14 + this.linkageAttribute.length + this.linkageValue.length);
        ByteBuf buffer = Unpooled.buffer();
        buffer.writeBytes(this.type).writeBytes(this.stationNo).writeBytes(this.monitorIndexNo).writeBytes(this.linkageType).writeBytes(this.linkageAttributeLen).writeBytes(this.linkageAttribute).writeBytes(this.linkageValueLen).writeBytes(this.linkageValue).writeBytes(this.linkageTime).writeBytes(this.sumCheck);
        byte[] total = new byte[buffer.readableBytes()];
        buffer.readBytes(total);
        return total;
    }
}
