package org.ts.ddcs.Tool;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.DataItemTemplate.DataItemTemplateInterface;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.DataTemplate.DataTemplateLine;
import org.ts.ddcs.DataTemplate.DataTemplateManager;
import org.ts.ddcs.DataTemplate.TemplateLineNode;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.utils.GBConstants;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

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

@SuppressWarnings("all")
public class DatagramHelp {


    /**
     * 水文 重启
     *
     * @param rtuCode
     * @param dt
     * @return
     */
    public static String GetRebootDatagramSw(String rtuCode, Date dt) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String tsStr = timeFormat.format(new Date());
            String message = "7E7E" + rtuCode;
            message = message + "91" + "0000" + "F3" + "8008" + "02" + "0000" + tsStr.substring(2) + "05";
            byte[] buff = BytesHelp.hexStr2Bytes(message);
            byte[] crc16 = BytesHelp.crcFun2014(buff);
            byte[] datagrambuff = BytesHelp.arrayApend(buff, crc16);//CRC16
            String datagram = BytesHelp.byte2HexStr(datagrambuff);
            return datagram;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }

    /**
     * 水资源 重启 版本1.0
     *
     * @param rtuCode
     * @param dt
     * @return
     */
    public static String GetDatagramRebootSzyV1(String rtuCode, Date dt) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            byte[] dataContent = BytesHelp.hexStr2Bytes("30");//C
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(rtuCode));//CODE
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("90"));//ANF
            byte[] dc = new byte[1];
            dc[0] = 0x01;
            dataContent = BytesHelp.arrayApend(dataContent, dc);
            //pw
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("0000"));
            //tp
            String tp = timeFormat.format(new Date());
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 2, tp.length())));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 4, tp.length() - 2)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 6, tp.length() - 4)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 8, tp.length() - 6)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("00"));
            byte[] lenBuff = new byte[1];
            byte[] crc = new byte[1];
            crc[0] = BytesHelp.GetCRCByte(dataContent);
            lenBuff[0] = (byte) dataContent.length;
            String datagram = "68" + BytesHelp.byte2HexStr(lenBuff) + "68" + BytesHelp.byte2HexStr(dataContent) + BytesHelp.byte2HexStr(crc) + "16";
            return datagram;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }

    /**
     * 水资源重启 版本2
     *
     * @param rtuCode
     * @param dt
     * @return
     */
    public static String GetDatagramRebootSzyV2(String rtuCode, Date dt) {
        try {
            byte[] respPacket_h = BytesHelp.hexStr2Bytes("680768");  //帧起始符
            byte[] respPacket = BytesHelp.hexStr2Bytes("30");//中心站地址
            respPacket = BytesHelp.arrayApend(respPacket, BytesHelp.hexStr2Bytes(rtuCode));//遥测站地址
            respPacket = BytesHelp.arrayApend(respPacket, BytesHelp.hexStr2Bytes("F3"));//功能码
            byte[] crc = new byte[1]; //系统计算的校验码
            crc[0] = BytesHelp.GetCRCByte(respPacket);
            respPacket = BytesHelp.arrayApend(respPacket_h, respPacket);
            respPacket = BytesHelp.arrayApend(respPacket, crc);//CRC
            respPacket = BytesHelp.arrayApend(respPacket, BytesHelp.hexStr2Bytes("16"));
            String datagram = BytesHelp.byte2HexStr(respPacket);
            return datagram;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }

    /**
     * 水文 恢复出厂
     *
     * @param rtuCode
     * @param dt
     * @return
     */
    public static String GetDatagramResetSw(String rtuCode, Date dt) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String tsStr = timeFormat.format(dt);
            String message = "7E7E" + rtuCode;
            //  String funcCode = null;
            //  funcCode = "48";
            message = message + "91" + "0000" + "48" + "800A" + "02" + "0000" + tsStr.substring(2) + "98" + "00" + "05";
            byte[] buff = BytesHelp.hexStr2Bytes(message);
            byte[] crc16 = BytesHelp.crcFun2014(buff);
            byte[] datagrambuff = BytesHelp.arrayApend(buff, crc16);//CRC16
            String datagram = BytesHelp.byte2HexStr(datagrambuff);
            return datagram;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }

    /**
     * 水资源 恢复出厂
     *
     * @param rtuCode
     * @param dt
     * @return
     */
    public static String GetDatagramResetSzy(String rtuCode, Date dt) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            byte[] dataContent = BytesHelp.hexStr2Bytes("30");//C
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(rtuCode));//CODE
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("90"));//ANF
            byte[] dc = new byte[1];
            dc[0] = 0x02;
            dataContent = BytesHelp.arrayApend(dataContent, dc);
            //pw
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("0000"));
            //tp
            String tp = timeFormat.format(new Date());
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 2, tp.length())));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 4, tp.length() - 2)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 6, tp.length() - 4)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 8, tp.length() - 6)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("00"));
            byte[] lenBuff = new byte[1];
            byte[] crc = new byte[1];
            crc[0] = BytesHelp.GetCRCByte(dataContent);
            lenBuff[0] = (byte) dataContent.length;
            String datagram = "68" + BytesHelp.byte2HexStr(lenBuff) + "68" + BytesHelp.byte2HexStr(dataContent) + BytesHelp.byte2HexStr(crc) + "16";

            return datagram;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }

    /**
     * 水文时钟
     *
     * @param rtuCode
     * @param dt
     * @return
     */
    public static String GetDatagramClockSetSw(String rtuCode, Date dt) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String tsStr = timeFormat.format(dt);
            String message = "7E7E" + rtuCode;
            tsStr = timeFormat.format(dt);
            message = message + "91" + "0000" + "4A" + "8008" + "02" + "0000" + tsStr.substring(2) + "05";
            byte[] buff = BytesHelp.hexStr2Bytes(message);
            byte[] crc16 = BytesHelp.crcFun2014(buff);
            byte[] datagrambuff = BytesHelp.arrayApend(buff, crc16);//CRC16
            String datagram = BytesHelp.byte2HexStr(datagrambuff);
            return datagram;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }

    /**
     * 水资源设置时钟
     *
     * @param rtuCode
     * @param dt
     * @return
     */
    public static String GetDatagramClockSetSzy(String rtuCode, Date dt) {
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            byte[] dataContent = BytesHelp.hexStr2Bytes("30");//C
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(rtuCode));//CODE
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("11"));//ANF

            //时钟
            String tsStr = timeFormat.format(new Date());

            tsStr = timeFormat.format(dt);
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tsStr.substring(2)));

            //周
            byte[] dc = new byte[1];
            dc[0] = 0x00;
            dataContent = BytesHelp.arrayApend(dataContent, dc);
            //pw
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("0000"));
            //tp
            String tp = timeFormat.format(new Date());
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 2, tp.length())));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 4, tp.length() - 2)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 6, tp.length() - 4)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes(tp.substring(tp.length() - 8, tp.length() - 6)));
            dataContent = BytesHelp.arrayApend(dataContent, BytesHelp.hexStr2Bytes("00"));
            byte[] lenBuff = new byte[1];
            byte[] crc = new byte[1];
            crc[0] = BytesHelp.GetCRCByte(dataContent);
            lenBuff[0] = (byte) dataContent.length;
            String datagram = "68" + BytesHelp.byte2HexStr(lenBuff) + "68" + BytesHelp.byte2HexStr(dataContent) + BytesHelp.byte2HexStr(crc) + "16";

            return datagram;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }

    /**
     * 转换报文到文本显示信息
     *
     * @param datagram
     * @return
     */
    public static List<JSONObject> toDatagramString(String datagram) {
        try {
            SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            List<JSONObject> viewInfoList = new LinkedList<>();
            byte[] data_gram_buff = BytesHelp.hexStr2Bytes(datagram);
            if (datagram.substring(0, 2).equals("68")) {
                DataTemplateLine line = DataTemplateManager.newInstance().createTemplateLine(GBConstants.AGREEMENT_KIND_SZY);
                line.Stream(data_gram_buff);
                TemplateLineNode macth = line.MacthTemplate();
                if (null != macth) {
                    DataTemplateInterface dataTemplateInterface = macth.getDataTemplate();
                    StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime()));
                    String tm = dataStr.toString();
                    if (tm != null && tm.length() > 0) {
                        tm = "20" + tm;
                        Date tm1 = timeFormat.parse(tm);
                        JSONObject text = new JSONObject();
                        text.put("name", "观测时间");
                        text.put("text", timeFormat2.format(tm1));
                        viewInfoList.add(text);
                    }
                    LinkedList<DataItemTemplateInterface> dataItem = dataTemplateInterface.MacthDataItem("wq_river");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }

                    dataItem = dataTemplateInterface.MacthDataItem("t_szy_flow_rate");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("t_szy_warining_info");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("t_szy_status_info");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("t_base_rtu_info");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }
                }
            } else {
                DataTemplateLine line = DataTemplateManager.newInstance().createTemplateLine(GBConstants.AGREEMENT_KIND_SW);
                line.Stream(data_gram_buff);
                TemplateLineNode macth = line.MacthTemplate();
                if (null != macth) {
                    DataTemplateInterface dataTemplateInterface = macth.getDataTemplate();
                    StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime()));
                    String tm = dataStr.toString();
                    if (tm != null && tm.length() > 0) {
                        tm = "20" + tm + "00";
                        Date tm1 = timeFormat.parse(tm);
                        JSONObject text = new JSONObject();
                        text.put("name", "观测时间");
                        text.put("text", timeFormat2.format(tm1));
                        viewInfoList.add(text);

                    }
                    dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.SendPacketTime()));
                    String dt = dataStr.toString();
                    if (dt != null && dt.length() > 0) {
                        dt = "20" + dt;
                        Date dt1 = timeFormat.parse(dt);
                        JSONObject text = new JSONObject();
                        text.put("name", "发报时间");
                        text.put("text", timeFormat2.format(dt1));
                        viewInfoList.add(text);
                    }
                    LinkedList<DataItemTemplateInterface> dataItem = dataTemplateInterface.MacthDataItem("ST_PPTN_R");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("st_pptn_r_5_min");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            if (item.GetDecimal() != null) {
                                text.put("text", item.GetDecimal().toPlainString());
                            } else {
                                text.put("text", "");
                            }
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("st_river_r");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            if (item.GetDecimal() != null) {
                                text.put("text", item.GetDecimal().toPlainString());
                            } else {
                                text.put("text", "");
                            }
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("st_rsvr_r");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            if (item.GetDecimal() != null) {
                                text.put("text", item.GetDecimal().toPlainString());
                            } else {
                                text.put("text", "");
                            }
                            viewInfoList.add(text);
                        }
                    }

                    dataItem = dataTemplateInterface.MacthDataItem("st_water_level_drz");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }

                    dataItem = dataTemplateInterface.MacthDataItem("t_base_rtu_info");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("st_ground");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("wr_water_qa");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("st_sw_water");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                        }
                    }

                    dataItem = dataTemplateInterface.MacthDataItem("st_weather");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);

                            // viewInfoList.add(item.GetDesc() + " " + item.toString());
                        }
                    }
                    dataItem = dataTemplateInterface.MacthDataItem("st_watergate");
                    if (dataItem.size() > 0) {
                        for (DataItemTemplateInterface item : dataItem) {
                            JSONObject text = new JSONObject();
                            text.put("name", item.GetDesc());
                            text.put("text", item.toString());
                            viewInfoList.add(text);
                            // viewInfoList.add(item.GetDesc() + " " + item.toString());
                        }
                    }

                }
            }

            return viewInfoList;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }
}
