package com.sunzee.mvp.advanceparameter;

import android.util.Log;

import com.google.gson.Gson;
import com.sunzee.base.BasePresenter;
import com.sunzee.model.Global;
import com.sunzee.model.Heartbeat;
import com.sunzee.model.HttpResult;
import com.sunzee.model.domain.ParameterBean;
import com.sunzee.retrofit.ApiCallback;
import com.sunzee.thread.advanceparameter.ThreadPoolAdvanceParameter;
import com.sunzee.utils.AdvacedUtil;
import com.sunzee.utils.HexadecimalUtil;

import java.util.ArrayList;

import okhttp3.RequestBody;

/**
 * 进阶参数presenter
 */
public class AdvanceParameterPresenter extends BasePresenter<AdvanceParameterView> {
    public AdvanceParameterPresenter(AdvanceParameterView view) {
        attachView(view);
    }

    private static final String TAG = "AdvanceParameterPresent";

    public void setAdvanced(String name, ArrayList<String> results, ThreadPoolAdvanceParameter threadGetParameter1) {
        if (name.equals("D418")) {
            if (!threadGetParameter1.isReceive2()) {
                threadGetParameter1.setReceive2(true);
                threadGetParameter1.stopReadD418();
                threadGetParameter1.getValues()[6] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(0))));
                threadGetParameter1.getValues()[7] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(1))));
                threadGetParameter1.getValues()[0] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(2))));
                threadGetParameter1.getValues()[1] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(3))));
                threadGetParameter1.getValues()[2] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(4))));
                threadGetParameter1.getValues()[3] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(5))));
                threadGetParameter1.getValues()[4] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(6))));
                threadGetParameter1.getValues()[5] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(7))));

                Log.d(TAG, "D418setAdvanced: " + results.size());
            }
        } else if (name.equals("D426")) {
            if (!threadGetParameter1.isReceive1()) {
                threadGetParameter1.setReceive1(true);
                threadGetParameter1.stopReadD426();
                threadGetParameter1.getValues()[7] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
                threadGetParameter1.getValues()[8] = String.valueOf(HexadecimalUtil.hex2dec(results.get(1)));
                threadGetParameter1.getValues()[9] = String.valueOf(HexadecimalUtil.hex2dec(results.get(2)));
                threadGetParameter1.getValues()[10] = String.valueOf(HexadecimalUtil.hex2dec(results.get(3)));
                threadGetParameter1.getValues()[11] = String.valueOf(HexadecimalUtil.hex2dec(results.get(4)));
                threadGetParameter1.getValues()[12] = String.valueOf(HexadecimalUtil.hex2dec(results.get(5)));
                threadGetParameter1.getValues()[13] = String.valueOf(HexadecimalUtil.hex2dec(results.get(6)));
                threadGetParameter1.getValues()[14] = String.valueOf(HexadecimalUtil.hex2dec(results.get(7)));
                threadGetParameter1.getValues()[15] = String.valueOf(HexadecimalUtil.hex2dec(results.get(8)));
                Log.d(TAG, "D426setAdvanced: " + results.size());
            }
        } else if (name.equals("D79")) {
            if (!threadGetParameter1.isReceive3()) {
                threadGetParameter1.setReceive3(true);
                threadGetParameter1.stopReadD79();
                threadGetParameter1.getValues()[16] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
                Log.d(TAG, "D79setAdvanced: " + results.size());
            }
        }
        if (threadGetParameter1.isReceive1() &&
                threadGetParameter1.isReceive2() &&
                threadGetParameter1.isReceive3()) {
            mvpView.getDataSuccess();
            threadGetParameter1.setReceive1(false);
            threadGetParameter1.setReceive2(false);
            threadGetParameter1.setReceive3(false);
        }
    }

    public void updateAdvanced(ArrayList<ParameterBean> parameterBeans) {

        String url = "/api/app_equipment/index/updateParam/1/" + Heartbeat.deviceId + ".htm";
        Gson gson = new Gson();
        String json = gson.toJson(parameterBeans);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
        addSubscription(apiStores.updateParamApi(url, body), new ApiCallback<HttpResult<String>>() {
            @Override
            public void onSuccess(HttpResult<String> model) {
                Log.d(TAG, "onSuccess: " + model);
                mvpView.hideLoading();
            }

            @Override
            public void onFailure(String msg) {
            }

            @Override
            public void onFinish() {
            }
        });
    }

    // 获取真正的精度
    public String getLD436RealValue(ArrayList<String> results, int position) {
        //10进制
        String s = String.valueOf(HexadecimalUtil.hex2dec(results.get(position)));
        long l = HexadecimalUtil.hex2dec(results.get(position));
        Integer integer = Integer.valueOf(s);
        //2进制
        String s1 = Integer.toBinaryString(integer);
        if (s1.length() < 16) {
            //正数 四舍五入
            String s3 = get177Data(l);
            return s3;
        } else {
            //负数
            String realData = AdvacedUtil.getRealData(s1);
            return realData;
        }


    }

    private String get177Data(long l) {
        return String.valueOf(Math.round((l / Global.num) * 10));
    }
}
