package com.example.iot_controlhost.adapter;

import android.widget.TextView;

import com.example.iot_controlhost.base.BaseRecyclerAdapter;
import com.example.iot_controlhost.databinding.ItemBtnBinding;
import com.example.iot_controlhost.model.net.WorkList;
import com.example.iot_controlhost.model.net.WorkStage;
import com.example.iot_controlhost.ui.dialog.WorkNewDialog;
import com.example.iot_controlhost.utils.DialogUtil;
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.w3c.dom.Text;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;


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

    TextView tvWorkCurrent;
    TextView tvWorkContent;
    WorkAdapterInterface wai;

    public WorkAdapter(List<WorkList.Data> dataList, Class<ItemBtnBinding> bindingClass, TextView tvWorkCurrent, TextView tvWorkContent, WorkAdapterInterface wai) {
        super(dataList, bindingClass);
        this.tvWorkCurrent = tvWorkCurrent;
        this.tvWorkContent = tvWorkContent;
        this.wai = wai;
    }

    @Override
    protected void bindData(ItemBtnBinding binding, WorkList.Data data) {
        binding.tv.setText(data.getName());
        binding.tv.setOnClickListener(v -> {
            tvWorkCurrent.setText(data.getName());
            refreshWorkContent(data.getId());
            wai.startWork(data.getName());
        });
    }

    private void refreshWorkContent(String content) {
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.getWorkDetail);
        params.addParameter("jobContentConfigId", content);
        x.http().get(params, new XHttpShorthand<WorkStage>() {
            @Override
            public void success(WorkStage model) {
                tvWorkContent.setText(WorkNewDialog.getInfo(model,true));
                if (RoomSetting.getMode() == 1) {
                    // 如果是自动模式，则刷新自动模式参数
                    RxBusUtils.get().post(RxTag.UPDATE_AUTO, 3);
                }
            }

            @Override
            public void error(Throwable e) {
                tvWorkContent.setText("获取失败");
                UserLog.taskError("作业内容获取失败");
                MyLog.networkError("作业内容获取失败：" + e.getMessage());
            }

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

}
