package com.inspect.patrol.linkage.frame;

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

import java.util.Arrays;

@Getter
public class DeviceInfoByte {
    private byte[] type;
    private byte[] stationNo;
    private byte[] subBit;
    private byte[] frameNo;
    private byte[] startTransPos;
    private byte[] dataLen;
    private byte[] content;
    private byte[] sumCheck;

    public DeviceInfoByte() {
    }

    public DeviceInfoByte(byte[] bytes) {
        this.type = Arrays.copyOfRange(bytes, 0, 5);
        this.stationNo = Arrays.copyOfRange(bytes, 5, 7);
        this.subBit = Arrays.copyOfRange(bytes, 7, 8);
        this.frameNo = Arrays.copyOfRange(bytes, 8, 10);
        this.startTransPos = Arrays.copyOfRange(bytes, 10, 14);
        this.dataLen = Arrays.copyOfRange(bytes, 14, 15);
        this.content = Arrays.copyOfRange(bytes, 15, 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(int stationNo) {
        this.stationNo = HexUtils.hexToByteArray(HexUtils.numToHex16(stationNo));
    }

    public void setSubBit(int subBit) {
        this.subBit = HexUtils.hexToByteArray(HexUtils.numToHex8(subBit));
    }

    public void setFrameNo(int frameNo) {
        this.frameNo = ByteUtil.getBytes((short) frameNo);
    }

    public void setStartTransPos(int startTransPos) {
        this.startTransPos = HexUtils.int2bytes(startTransPos);
    }

    public void setDataLen(int dataLen) {
        this.dataLen = HexUtils.hexToByteArray(HexUtils.numToHex8(dataLen));
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public void setSumCheck(int sumCheck) {
        this.sumCheck = HexUtils.hexToByteArray(HexUtils.numToHex8(sumCheck));
    }

    public DeviceInfoEntity toDeviceInfoEntity() {
        DeviceInfoEntity deviceInfoEntity = new DeviceInfoEntity();
        deviceInfoEntity.setType(this.type);
        deviceInfoEntity.setStationNo(this.stationNo);
        deviceInfoEntity.setSubBit(this.subBit);
        deviceInfoEntity.setFrameNo(this.frameNo);
        deviceInfoEntity.setStartTransPos(this.startTransPos);
        deviceInfoEntity.setDataLen(this.dataLen);
        deviceInfoEntity.setContent(this.content);
        deviceInfoEntity.setSumCheck(this.sumCheck);
        return deviceInfoEntity;
    }

    public byte[] toBytes() {
        ByteBuf buffer = Unpooled.buffer();
        buffer.writeBytes(this.type).writeBytes(this.stationNo).writeBytes(this.subBit).writeBytes(this.frameNo).writeBytes(this.startTransPos).writeBytes(this.dataLen).writeBytes(this.content).writeBytes(this.sumCheck);
        byte[] total = new byte[buffer.readableBytes()];
        buffer.readBytes(total);
        return total;
    }
}
