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 JtDataTemplateMultimedia 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);
        byte[] toBuf = new byte[count + 1];

        this.length = count + 1;

        int len = 0;
        toBuf[0] = tmpBuf[0];
        len += 1;
        count = 2;
        while (true) {
            if (count >= tmpBuf.length) {
                break;
            }
            if (tmpBuf[count - 1] == 0x7D && tmpBuf[count] == 0x02) {
                toBuf[len] = 0x7E;
                len += 1;
                count += 2;
            } else if (tmpBuf[count - 1] == 0x7D && tmpBuf[count] == 0x01) {
                toBuf[len] = 0x7D;
                len += 1;
                count += 2;
            } else {
                toBuf[len] = tmpBuf[count - 1];
                len += 1;
                count += 1;
            }
        }

        toBuf[len] = tmpBuf[tmpBuf.length - 1];
        len += 1;

        tmpBuf = new byte[len];
        System.arraycopy(toBuf, 0, tmpBuf, 0, len);

        this.packet = tmpBuf;

        count = 0;
        //start flag
        count += 1;
        this.funcCode = new byte[2];
        System.arraycopy(tmpBuf, count, this.funcCode, 0, 2);
        count += 2;
        byte[] parameter = new byte[2];
        System.arraycopy(tmpBuf, 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;

        //分包信息
        if (this.hasNext) {
            byte[] packageTotalBuff = new byte[2];
            System.arraycopy(this.packet, count, packageTotalBuff, 0, 2);
            this.packageTotal = BytesUtil.bytes2Int(packageTotalBuff, 0, 2);
            count += 2;
            byte[] packageIndexBuff = new byte[2];
            System.arraycopy(this.packet, count, packageIndexBuff, 0, 2);
            this.packageIndex = BytesUtil.bytes2Int(packageIndexBuff, 0, 2);
            count += 2;
        }

        if (bodyLen > 0) {
            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;
        //end flag
        count += 1;

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

}
