package com.bjzc.bhzhgd.activity.sbxj;


import static com.bjzc.bhzhgd.utils.GsonUtils.GsonToBean;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.adapter.RlinfoAdapter;
import com.bjzc.bhzhgd.base.BaseActivity;
import com.bjzc.bhzhgd.bean.RlinfoBean;
import com.bjzc.bhzhgd.bean.RlresultBean;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.utils.GsonUtils;
import com.bjzc.bhzhgd.utils.HttpListener;
import com.bjzc.bhzhgd.utils.NohttpUtils;
import com.bjzc.bhzhgd.utils.SPUtils;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YxrlinfoActivity extends BaseActivity {
    RecyclerView recyclerView;
    private List<RlinfoBean.DataBean> datas;
    RlinfoAdapter adapter;

    @Override
    public int getContentViewID() {
        return R.layout.activity_rlzqinfo;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recycler_view);
        setTitle("燃料周期信息管理");
        setBack();
        datas = new ArrayList<>();

        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter = new RlinfoAdapter());
        GetDatas();
        adapter.setOnItemClickListener((itemView, item, position) -> {
            DialogLoader.getInstance().showSingleChoiceDialog(context, "燃料巡检周期设置", R.array.router_choice_entry,
                    0, (dialogInterface, i) -> {
                        if (i == 0) {
                            UploadDatas(datas.get(position).getJHID(), "4", "4");
                        } else if (i == 1) {
                            UploadDatas(datas.get(position).getJHID(), "24", "8");
                        }

                    }, "确定", "取消");

        });
    }

    private void GetDatas() {
        final Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.XSCB, RequestMethod.POST);
        requestxs.setDefineRequestBodyForJson(createJson());
        NohttpUtils.getInstance().add(this, 0, requestxs, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                RlinfoBean list = GsonToBean(response.get(), RlinfoBean.class);
                if (list != null) {
                    datas = list.getData();
                    adapter.refresh(datas);
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
            }
        });
    }

    private String createJson() {
        String json = "";
        Map<String, String> map = new HashMap<>();
        map.put("action", "XSCB_JHGL_GET");
        map.put("jhid", "");
        json = GsonUtils.GsonString(map);
        return json;
    }

    private void UploadDatas(String jhid, String jhzq, String zxsc) {
        final Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.XSCB, RequestMethod.POST);
        requestxs.setDefineRequestBodyForJson(createJson1(jhid, jhzq, zxsc));
        NohttpUtils.getInstance().add(this, 0, requestxs, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                RlresultBean list = GsonToBean(response.get(), RlresultBean.class);
                if (list != null) {
                    if (list.getState() == 1) {
                        showToast(list.getMsg());
                        GetDatas();
                    } else {
                        showToast(list.getMsg());
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
            }
        });
    }

    private String createJson1(String jhid, String jhzq, String zxsc) {
        String json = "";
        Map<String, String> map = new HashMap<>();
        map.put("action", "XSCB_JHGL_SET");
        map.put("jhid", jhid);
        map.put("jhzq", jhzq);
        map.put("zxsc", zxsc);
        map.put("zxid", "");
        map.put("xgr", (String) SPUtils.get(context, Contans.USERNAME, ""));
        json = GsonUtils.GsonString(map);
        return json;
    }

    @Override
    protected void onResume() {
        super.onResume();
        GetDatas();
    }
}
