package com.sunzee.mvp.advanceparameter;

import android.util.Log;

import com.google.gson.Gson;
import com.sunzee.base.BasePresenter;
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.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("D426")) {
            if (!threadGetParameter1.isReceive1()) {
                threadGetParameter1.setReceive1(true);
                threadGetParameter1.stopReadD426();
                threadGetParameter1.getValues()[0] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
            }
        } else if (name.equals("D427")) {
            if (!threadGetParameter1.isReceive2()) {
                threadGetParameter1.setReceive2(true);
                threadGetParameter1.stopReadD427();
                threadGetParameter1.getValues()[1] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
            }

        } else if (name.equals("D428")) {
            if (!threadGetParameter1.isReceive3()) {
                threadGetParameter1.setReceive3(true);
                threadGetParameter1.stopReadD428();
                threadGetParameter1.getValues()[2] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
            }

        } else if (name.equals("D429")) {
            if (!threadGetParameter1.isReceive4()) {
                threadGetParameter1.setReceive4(true);
                //参数23 D278
                threadGetParameter1.stopReadD429();
                threadGetParameter1.getValues()[3] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
            }
        } else if (name.equals("D430")) {
            if (!threadGetParameter1.isReceive5()) {
                threadGetParameter1.setReceive5(true);
                threadGetParameter1.stopReadD430();
                threadGetParameter1.getValues()[4] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
            }
        } else if (name.equals("D431")) {
            if (!threadGetParameter1.isReceive6()) {
                threadGetParameter1.setReceive6(true);
                threadGetParameter1.stopReadD431();
                threadGetParameter1.getValues()[5] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
            }
        }
        if (threadGetParameter1.isReceive1() && threadGetParameter1.isReceive2() && threadGetParameter1.isReceive3() && threadGetParameter1.isReceive4() && threadGetParameter1.isReceive5() && threadGetParameter1.isReceive6()) {
            mvpView.getDataSuccess();
            threadGetParameter1.setReceive1(false);
            threadGetParameter1.setReceive2(false);
            threadGetParameter1.setReceive3(false);
            threadGetParameter1.setReceive4(false);
            threadGetParameter1.setReceive5(false);
            threadGetParameter1.setReceive6(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() {
            }
        });
    }
}
