package  org.ts.ddcs.datatemplate.jt_808_2019_template;


import org.ts.ddcs.dataStream.DataStreamInterface;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.util.BytesUtil;

/**
 * 交通808-2019规约数据模板
 * created by swp on 2017/8/23
 *
 * @author dylan
 */
public class JtDataTemplateTakePicture extends JtDataTemplate {

    @Override
    public void stream(DataStreamInterface stream) {
        if (stream.dataSize() == 0) {
            return;
        }
        if (stream.getByte(0) != 0x7E) {
            this.state = TestState.UNKNOWN;
            return;
        }
        if (stream.dataSize() < this.featureWeight) {
            return;
        }

        int count = 0;
        for (int i = 1; i < stream.dataSize(); i++) {
            if (stream.getByte(i) == 0x7E) {
                count = i;
                break;
            }
        }
        if (count == 0) {
            return;
        }
        if (count - 1 <= 0) {
            this.state = TestState.UNKNOWN;
            return;
        }

        byte[] feature = stream.getBytes(this.featureWeight);
        if (!this.checkFeature(feature, 0, feature.length)) {
            this.state = TestState.UNKNOWN;
            return;
        }

        byte[] tmpBuf = stream.getBytes(0, count + 1);

        String tmpHex = BytesUtil.byte2HexStr(tmpBuf);
        tmpHex = tmpHex.replace("7D02", "7E");
        tmpHex = tmpHex.replace("7D01", "7D");
        tmpBuf = BytesUtil.hexStr2Bytes(tmpHex);

        this.length = count + 1;
        this.packet = tmpBuf;

        count = 1;
        this.funcCode = new byte[2];
        System.arraycopy(this.packet, count, this.funcCode, 0, 2);
        count += 2;

        byte[] parameter = new byte[2];
        System.arraycopy(this.packet, count, parameter, 0, 2);
        count += 2;

        String bin = BytesUtil.bytesToBinStr(parameter);
        String nextFlag = bin.substring(2, 3);
        if (nextFlag.equals("1")) {
            this.hasNext = true;
        } else {
            this.hasNext = false;
        }
        String rsa = bin.substring(3, 6);
        if (rsa.equals("000")) {
            this.rsa = "0";
        } else {
            this.rsa = "1";
        }

        String bodyLenStr = "000000" + bin.substring(6, 16);
        this.bodyLen = Integer.parseUnsignedInt(bodyLenStr, 2);
        if (bodyLen <0 || bodyLen>(this.length-1-17-2)){
            this.state = TestState.UNKNOWN;
            return;
        }
        this.ver = new byte[1];
        System.arraycopy(this.packet, count, this.ver, 0, 1);
        count += 1;

        this.deviceId = new byte[10];
        System.arraycopy(this.packet, count, this.deviceId, 0, 10);
        count += 10;

        this.datagramIndex = new byte[2];
        System.arraycopy(this.packet, count, this.datagramIndex, 0, 2);
        count += 2;

        this.dataArea = new byte[bodyLen];
        System.arraycopy(this.packet, count, this.dataArea, 0, bodyLen);
        count += bodyLen;

        this.crc = new byte[1];
        System.arraycopy(this.packet, count, this.crc, 0, 1);
        count += 1;

        count += 1;

        if (this.packet.length != count) {
            //   this.state = TestState.UNKNOWN;
            return;
        }
        LogHelper.info("success get datagram!!");
        this.state = TestState.MATHCE;
    }


}
