package com.example.iot_controlhost.adapter;

import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.example.iot_controlhost.base.BaseRecyclerAdapter;
import com.example.iot_controlhost.databinding.ItemBtnBinding;
import com.example.iot_controlhost.model.net.ControlMode;
import com.example.iot_controlhost.model.net.StageList;
import com.example.iot_controlhost.model.net.WorkStage;
import com.example.iot_controlhost.ui.dialog.ConfirmDialog;
import com.example.iot_controlhost.ui.dialog.WorkNewDialog;
import com.example.iot_controlhost.utils.DialogUtil;
import com.example.iot_controlhost.utils.MMKVUtil;
import com.example.iot_controlhost.utils.database.TempTHVDBManager;
import com.example.iot_controlhost.utils.global.AutoModelSet;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.global.RxTag;
import com.example.iot_controlhost.utils.log.UserLog;
import com.example.iot_controlhost.utils.log.MyLog;
import com.example.iot_controlhost.utils.network.Url;
import com.example.iot_controlhost.utils.network.XHttpManager;
import com.example.iot_controlhost.utils.network.XHttpShorthand;
import com.xuexiang.rxutil2.rxbus.RxBusUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * @Author：DuanKaiji
 */
public class StageAdapter extends BaseRecyclerAdapter<StageList.Data, ItemBtnBinding> {

    TextView tvStageCurrent;
    TextView tvStageContent;

    public StageAdapter(List<StageList.Data> dataList, Class<ItemBtnBinding> bindingClass, TextView tvStageCurrent, TextView tvStageContent) {
        super(dataList, bindingClass);
        this.tvStageCurrent = tvStageCurrent;
        this.tvStageContent = tvStageContent;
    }

    @Override
    protected void bindData(ItemBtnBinding binding, StageList.Data data) {
        binding.tv.setText(data.getName());
        binding.tv.setOnClickListener(v -> {
            tvStageCurrent.setText(data.getName());
            refreshWorkContent(data);
        });
    }

    private void refreshWorkContent(StageList.Data content) {
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.getStageDetail);
        params.addParameter("feedingStageConfigId", content.getId());
        x.http().get(params, new XHttpShorthand<WorkStage>() {
            @Override
            public void success(WorkStage model) {
                String info = WorkNewDialog.getInfo(model, false);
                TempTHVDBManager.INSTANCE.setStage(content.getName(), info);
                UserLog.task("切换作业阶段：" + content.getName());
                MyLog.app("作业阶段切换成功：" + content.getName() + "，已应用阶段控制参数");
                tvStageContent.setText(info);
                if (RoomSetting.getMode() != 1) {
                    // 如果不是自动模式，则切换为自动模式
                    RxBusUtils.get().post(RxTag.UPDATE_MAIN, 3);
                }else{
                    // 刷新自动模式下的参数
                    RxBusUtils.get().post(RxTag.UPDATE_AUTO, 3);
                }
            }

            @Override
            public void error(Throwable e) {
                tvStageContent.setText("获取失败");
                UserLog.taskError("作业阶段切换失败：" + content.getName());
                MyLog.networkError("作业阶段切换失败：" + content.getName() + "，原因=" + e.getMessage());
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUtil.hideLoadingDialog();
            }
        });
    }

}
