package  org.ts.ddcs.dataprocesser.jtDatagram;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.main.DataProcessHolder;
import org.ts.ddcs.common.constant.GBConstants;
import org.ts.ddcs.dataprocesser.DataProcesser;
import org.ts.ddcs.dataprocesser.DataProcesserInterface;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.datatemplate.DataTemplateInterface;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.net.DataChannelHolder;
import org.ts.ddcs.util.BytesUtil;


import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 0x0100
 * 设备注册
 */
public class DataProcessorJtLocation extends DataProcesser implements DataProcesserInterface {

    private byte[] getTakePhotoDatagram(String phoneKey) {
        byte[] funCode = new byte[2];
        funCode[0] = (byte) 0x88;
        funCode[1] = (byte) 0x01;
        byte[] respPacket = funCode;
        int len = 12;
        //消息属性
        byte[] lenBuf = BytesUtil.int2Bytes(len);
        String bin = BytesUtil.bytesToBinStr(lenBuf);
        String parameter = "010000" + bin.substring(bin.length() - 10, bin.length());
        lenBuf = BytesUtil.binStrToBytes(parameter);
        respPacket = BytesUtil.arrayApend(respPacket, lenBuf);
        //版本
        byte[] ver = new byte[1];
        ver[0] = (byte) 0x01;
        respPacket = BytesUtil.arrayApend(respPacket, ver);
        //手机号
        respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.hexStr2Bytes(phoneKey));
        //流水号
        byte[] index = new byte[2];
        index[0] = (byte) 0x00;
        index[1] = (byte) 0x01;
        respPacket = BytesUtil.arrayApend(respPacket, index);
        byte[] bodyBuff = new byte[12];
        //通道ID
        bodyBuff[0] = 0x01;
        //拍摄命令
        //   if (type.equals("0")) {
        bodyBuff[1] = 0x00;
        bodyBuff[2] = 0x01;
        bodyBuff[3] = 0x00;
//        } else {
//            //  byte[] tmpBuff = BytesUtil.hexStr2Bytes("FF");
//            bodyBuff[1] = (byte) 0xff;
//            bodyBuff[2] = (byte) 0xff;
//            bodyBuff[3] = 0x05;
//        }
        bodyBuff[4] = 0x78;
        bodyBuff[5] = 0x00;
        bodyBuff[6] = (byte) 0x00;
        bodyBuff[7] = 0x01;
        bodyBuff[8] = (byte) 0xfe;
        bodyBuff[9] = 127;
        bodyBuff[10] = 127;
        bodyBuff[11] = (byte) 0xfe;
        respPacket = BytesUtil.arrayApend(respPacket, bodyBuff);
        int crc = respPacket[0];
        for (int i = 1; i < respPacket.length; i++) {
            crc = crc ^ respPacket[i];
        }
        byte[] crcBuff = new byte[1];
        crcBuff[0] = (byte) crc;
        respPacket = BytesUtil.arrayApend(respPacket, crcBuff);
        byte[] flag = new byte[1];
        flag[0] = 0x7e;
        byte[] respBuff = flag;
        respBuff = BytesUtil.arrayApend(respBuff, respPacket);
        respBuff = BytesUtil.arrayApend(respBuff, flag);
        return respBuff;
    }

    @Override
    public void packetReadly(byte[] byteBuff, int pos, int length, DataStreamAdapter dataStreamAdapter, DataTemplateInterface template) {
        String logId = LogHelper.createLogId(dataStreamAdapter.getAdapterKey() + " " + this.getClass().getSimpleName());
        try {
            Date dt = new Date();
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            byte[] packetContext = template.datagramContext();
            byte[] rtuCodeBuff = template.rtuCode();
            String rtuCode = BytesUtil.byte2HexStr(rtuCodeBuff);
            LogHelper.info(logId + " " + " JT 808-功能码:0200 上报位置" + "-RTU:" + rtuCode);
            dataStreamAdapter.setCode(rtuCode);
            //拼装回复报文，先回复再解析入库
            byte[] funCode = new byte[2];
            funCode[0] = (byte) 0x80;
            funCode[1] = (byte) 0x01;
            byte[] respPacket = funCode;
            int len = 5;
            //消息属性
            byte[] lenBuf = BytesUtil.int2Bytes(len);
            String bin = BytesUtil.bytesToBinStr(lenBuf);
            String parameter = "010000" + bin.substring(bin.length() - 10, bin.length());
            lenBuf = BytesUtil.binStrToBytes(parameter);
            respPacket = BytesUtil.arrayApend(respPacket, lenBuf);
            //版本
            byte[] ver = new byte[1];
            ver[0] = (byte) 0x01;
            respPacket = BytesUtil.arrayApend(respPacket, ver);
            //手机号
            respPacket = BytesUtil.arrayApend(respPacket, template.rtuCode());
            //流水号
            byte[] index = new byte[2];
            index[0] = (byte) 0x00;
            index[1] = (byte) 0x01;
            respPacket = BytesUtil.arrayApend(respPacket, index);
            //消息体流水
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.subBytes(packetContext, 16, 2));
            //消息ID
            byte[] command = new byte[2];
            command[0] = (byte) 0x02;
            command[1] = (byte) 0x00;
            respPacket = BytesUtil.arrayApend(respPacket, command);
            //消息体状态
            byte[] status = new byte[1];
            status[0] = (byte) 0x00;
            respPacket = BytesUtil.arrayApend(respPacket, status);
            int crc = respPacket[0];
            for (int i = 1; i < respPacket.length; i++) {
                crc = crc ^ respPacket[i];
            }
            byte[] crcBuff = new byte[1];
            crcBuff[0] = (byte) crc;
            respPacket = BytesUtil.arrayApend(respPacket, crcBuff);
            byte[] respBuff = BytesUtil.subBytes(packetContext, 0, 1);
            respBuff = BytesUtil.arrayApend(respBuff, respPacket);
            respBuff = BytesUtil.arrayApend(respBuff, BytesUtil.subBytes(packetContext, 0, 1));
            dataStreamAdapter.output(respBuff);
            byte[] dataGram = BytesUtil.subBytes(byteBuff, pos, length);  //帧起始符
            String gram = BytesUtil.byte2HexStr(dataGram);
            JSONObject body = new JSONObject();
            body.put(GBConstants.BASE_KEY_DATAGRAM, gram);
            body.put(GBConstants.BASE_KEY_RTU, rtuCode.substring(4, 20));
            body.put(GBConstants.BASE_KEY_AGREEMENT, template.agreement());
            body.put(GBConstants.BASE_KEY_DATAGRAM_PICK_TM, timeFormat.format(dt));
            body.put("afc", BytesUtil.byte2HexStr(template.funcCode()));
            DataChannelHolder.newInstance().dataChannel.online(dataStreamAdapter);
           // DataProcessHolder.newInstance().dataProcess.submitRelayDatagramTask(body);
            //下发图片拍照指令
            byte[] outBuff = getTakePhotoDatagram(rtuCode);
            dataStreamAdapter.output(outBuff);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
