package com.sunzee.mvp.debug;

import android.util.Log;

import com.hboxs.serialport.frame.ResponseFrame;
import com.hboxs.serialport.message.Message;
import com.sunzee.base.BasePresenter;
import com.sunzee.thread.debugparameter.ThreadPoolDebugParameter;
import com.sunzee.utils.HexadecimalUtil;
import com.wuxiaolong.androidutils.library.LogUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 调试页界面 presenter
 */
public class DebugPresenter extends BasePresenter<DebugView> {
    public DebugPresenter(DebugView view) {
        attachView(view);
    }

    private static final String TAG = "DebugPresenter";
    private ArrayList<String> listX = new ArrayList<>(Arrays.asList(
            "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0",
            "0"));
    private ArrayList<String> listY = new ArrayList<>(Arrays.asList(
            "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0",
            "0", "0", "0", "0", "0",
            "0", "0"));

    public void setDebugParameter(String name, ArrayList<String> results, ThreadPoolDebugParameter threadGetParameter1) {
        if (name.equals("M311")) {
            if (!threadGetParameter1.isReceive1()) {
                threadGetParameter1.setReceive1(true);
                threadGetParameter1.stopReadM311();
                //参数19 D218
                threadGetParameter1.getValues().set(0, results.get(7));
                threadGetParameter1.getValues().set(1, results.get(8));
                threadGetParameter1.getValues().set(2, results.get(9));
                threadGetParameter1.getValues().set(3, results.get(11));
                threadGetParameter1.getValues().set(4, results.get(12));
                threadGetParameter1.getValues().set(5, results.get(13));
                threadGetParameter1.getValues().set(6, results.get(14));
                LogUtil.d(TAG, "setDebugParameter: " + threadGetParameter1.getValues());
            }
        }
        if (threadGetParameter1.isReceive1()) {
            mvpView.getDataSuccess();
            mvpView.hideLoading();
            threadGetParameter1.setReceive1(false);
        }
    }

    public void setData(Message messageEvent, ResponseFrame responseFrame, String result, ThreadPoolDebugParameter poolDebugParameter) {
        //通过校验
        int id = messageEvent.getId();
        String name = messageEvent.getName();
        if (id == 311) {
            Log.d(TAG, "event: " + name);
            ArrayList<String> results = HexadecimalUtil.flipString(result);
            //    转成2进制
            String s = HexadecimalUtil.hexStringToByte(result);
            //翻转后的数据
            String s1 = HexadecimalUtil.flipString(s, 8);
            List<String> lis = Arrays.asList(s1.split(""));
            ArrayList<String> list = new ArrayList(lis);
            list.remove(0);
            setDebugParameter(name, list, poolDebugParameter);

        } else if (id == 1 || id == 2) {
            List<String> strList = HexadecimalUtil.getStrList(result, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = 0, len = strList.size(); i < len; i++) {
                String str = strList.get(i);
                //2进制
                String s3 = HexadecimalUtil.hexStringToByte(str);
                //2进制翻转
                String s2 = HexadecimalUtil.flipString(s3, 8);
                sb.append(s2);
            }
            List<String> list1 = Arrays.asList(sb.toString().split(""));
            ArrayList<String> list2 = new ArrayList(list1);
            list2.remove(0);
            if ("X0".equals(name)) {
                listX.set(0, list2.get(2));
                listX.set(1, list2.get(3));
                listX.set(2, list2.get(4));
                listX.set(3, list2.get(5));
                listX.set(4, list2.get(7));
                listX.set(5, list2.get(8));
                listX.set(6, list2.get(9));
                listX.set(7, list2.get(10));
                listX.set(8, list2.get(11));
                listX.set(9, list2.get(12));
                listX.set(10, list2.get(13));
                listX.set(11, list2.get(14));
                listX.set(12, list2.get(15));
                listX.set(13, list2.get(16));
                listX.set(14, list2.get(17));
                listX.set(15, list2.get(18));
                listX.set(16, list2.get(19));
                listX.set(17, list2.get(20));
                listX.set(18, list2.get(21));
                listX.set(19, list2.get(22));
                listX.set(20, list2.get(23));
                mvpView.setXStatu(listX);
            } else if ("Y0".equals(name)) {
                listY.set(0, list2.get(0));
                listY.set(1, list2.get(1));
                listY.set(2, list2.get(2));
                listY.set(3, list2.get(3));
                listY.set(4, list2.get(4));
                listY.set(5, list2.get(5));
                listY.set(6, list2.get(6));
                listY.set(7, list2.get(7));
                listY.set(8, list2.get(8));
                listY.set(9, list2.get(9));
                listY.set(10, list2.get(10));
                listY.set(11, list2.get(11));
                listY.set(12, list2.get(12));
                listY.set(13, list2.get(13));
                listY.set(14, list2.get(14));
                listY.set(15, list2.get(15));
                listY.set(16, list2.get(16));
                listY.set(17, list2.get(17));
                listY.set(18, list2.get(18));
                listY.set(19, list2.get(19));
                listY.set(20, list2.get(20));
                listY.set(21, list2.get(21));
                mvpView.setYStatu(listY);
                Log.d(TAG, list2.size() + "setYStatu: " + list2);
            }
            mvpView.hideLoading();
        }
    }
}