package org.ts.ddcs.DataItemTemplate;

import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.Tool.StringUtil;


/**
 * 0x04
 */
@SuppressWarnings("all")
public class DataItemTemplateDRxnn extends DataItemTemplate {
    @Override
    public byte[] Stream(byte[] data) {
        byte[] remain = data;
        if (this.macth) {
            return remain;
        }

        int count = 0;
        String flag = BytesHelp.byte2HexStr(remain, 0, 1);
        count += 1;
        if (flag.equals(this.feture)) {
            byte[] len = BytesHelp.subBytes(remain, 1, 1);
            count += 1;
            int dataTotalLen = len[0] >> 3;
            byte[] buff = BytesHelp.subBytes(remain, 2, dataTotalLen);
            count += dataTotalLen;
            String dataStr = BytesHelp.byte2HexStr(buff);
            //日，时，分，只有一个值大于0
            String tm = dataStr.substring(0, 2);
            int d_value = Integer.parseInt(tm);
            tm = dataStr.substring(2, 4);
            int h_value = Integer.parseInt(tm);
            tm = dataStr.substring(4, 6);
            int m_value = Integer.parseInt(tm);

            if (m_value == 5) {
                byte[] dataFlag = BytesHelp.subBytes(remain, count, 1);
                count += 1;
                byte[] dataLen = BytesHelp.subBytes(remain, count, 1);
                count += 1;
                int valueLen = dataLen[0] >> 3;
                int decimalLen = dataLen[0] & 7;
                int arrLen = valueLen * 12;
                buff = BytesHelp.subBytes(remain, count, arrLen);

                for (int i = 0; i < 12; i++) {
                    byte[] valueBuf = BytesHelp.subBytes(buff, i * valueLen, valueLen);
                    String valueBufStr = BytesHelp.byte2HexStr(valueBuf);
                    if (valueBufStr.startsWith("FF")) {
                        this.datas.put(String.format("%d", i + 1), valueBufStr);
                    } else {
                        StringBuffer dataStrBuf = new StringBuffer();
                        if (decimalLen != 0) {
                            dataStrBuf.insert(dataStrBuf.length() - decimalLen, ".");
                        }
                        String value = StringUtil.getPrettyNumber(dataStrBuf.toString());
                        this.datas.put(String.format("%d", i + 1), value);
                    }
                }
            }

            if (remain.length - count > 0) {
                remain = BytesHelp.subBytes(remain, count, remain.length - count);
            } else {
                remain = new byte[0];
            }

            this.macth = true;
        }
        return remain;
    }

    @Override
    public String toString() {
        if (this.datas.size() == 12) {
            String text = "";
            for (int i = 0; i < 12; i++) {
                String value = (String) this.datas.get(String.format("%d", i + 1));
                text += " " + String.format("%d", (i + 1) * 5) + "分：" + value;
            }
            return text;
        }
        return null;
    }
}
