package com.inspect.patrol.linkage.frame;

import com.inspect.base.core.utils.ByteUtil;
import com.inspect.base.core.utils.HexUtils;
import lombok.Getter;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

@Getter
public class DeviceInfoEntity {
    private String type = "EB90EB9043";
    private int stationNo;
    private int subBit = 0;
    private int frameNo = 0;
    private int startTransPos = 0;
    private int dataLen = 0;
    private String content;
    private int sumCheck;

    public DeviceInfoEntity() {
    }

    public DeviceInfoEntity(String content, int stationNo) {
        this.content = content;
        this.stationNo = stationNo;
    }

    public void setType(byte[] type) {
        this.type = HexUtils.binary(type, 16);
    }

    public void setStationNo(byte[] stationNo) {
        this.stationNo = Integer.parseInt(HexUtils.binary(stationNo, 16), 16);
    }

    public void setSubBit(byte[] subBit) {
        this.subBit = Integer.parseInt(HexUtils.binary(subBit, 16), 16);
    }

    public void setFrameNo(byte[] frameNo) {
        this.frameNo = ByteUtil.getShort(frameNo);
    }

    public void setStartTransPos(byte[] startTransPos) {
        this.startTransPos = ByteUtil.getInt(startTransPos);
    }

    public void setDataLen(byte[] dataLen) {
        this.dataLen = Integer.parseInt(HexUtils.binary(dataLen, 16), 16);
    }

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

    public void setSumCheck(byte[] sumCheck) {
        this.sumCheck = Integer.parseInt(HexUtils.binary(sumCheck, 16), 16);
    }

    public List<DeviceInfoByte> toDeviceInfoBytes() throws UnsupportedEncodingException {
        List<DeviceInfoByte> deviceInfoBytes = new ArrayList();
        byte[] contentBytes = this.content.getBytes("GB2312");

        for (int len = contentBytes.length; len > 0; ++this.frameNo) {
            this.startTransPos += this.dataLen;
            if (len >= 240) {
                this.dataLen = 240;
                this.subBit = 1;
                len -= 240;
            } else {
                this.dataLen = len;
                this.subBit = 0;
                len = 0;
            }

            this.sumCheck = 9 + this.dataLen;
            DeviceInfoByte deviceInfoByte = new DeviceInfoByte();
            deviceInfoByte.setType(this.type);
            deviceInfoByte.setStationNo(this.stationNo);
            deviceInfoByte.setSubBit(this.subBit);
            deviceInfoByte.setFrameNo(this.frameNo);
            deviceInfoByte.setStartTransPos(this.startTransPos);
            deviceInfoByte.setDataLen(this.dataLen);
            deviceInfoByte.setContent(Arrays.copyOfRange(contentBytes, this.startTransPos, this.startTransPos + this.dataLen));
            deviceInfoByte.setSumCheck(this.sumCheck);
            deviceInfoBytes.add(deviceInfoByte);
        }

        return deviceInfoBytes;
    }
}
