package com.rehome.sgbaxj.activity.sbxj;

import android.content.ContentValues;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.rehome.sgbaxj.DBModel.XSJJHDataBean;
import com.rehome.sgbaxj.DBModel.XSJJHXZDataBean;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.adapter.CommonAdapter;
import com.rehome.sgbaxj.adapter.ViewHolder;
import com.rehome.sgbaxj.base.BaseActivity3;
import com.rehome.sgbaxj.weight.ListDialog;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class SxSbListActivity extends BaseActivity3 {
    ListView lv;
    TextView tvNodata;
    Button Btn_Xjsj;


    private View headView;
    private CommonAdapter<XSJJHXZDataBean> adapter;
    private List<XSJJHXZDataBean> xsjjhxzDataBeanList_sb = new ArrayList<>();//工作列表
    Intent intent;
    private int pos = -1;
    private String state;

    @Override
    public int getLayoutId() {
        return R.layout.activity_xsxzsbstate;
    }

    @Override
    public void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        Btn_Xjsj = findViewById(R.id.btn_xjsj);
        lv.setEmptyView(tvNodata);
        headView = View.inflate(this, R.layout.xsgzsblist_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
    }

    public void initData() {
        initNFC();
        initToolbar("选择设备状态", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        getDataInSqlite();
        setListData();
        Btn_Xjsj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (xsjjhxzDataBeanList_sb.isEmpty()) {
                    showToast("你还没有下载巡检数据!");
                } else {
                    intent = new Intent(SxSbListActivity.this, SxgzActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    private void getDataInSqlite() {
        xsjjhxzDataBeanList_sb.clear();
        xsjjhxzDataBeanList_sb.addAll(DataSupport.findAll(XSJJHXZDataBean.class));
        Set<XSJJHXZDataBean> xsjjhxzDataBeanSet = new TreeSet<>((o1, o2) -> o1.getSbmc().compareTo(o2.getSbmc()));
        xsjjhxzDataBeanSet.addAll(xsjjhxzDataBeanList_sb);
        xsjjhxzDataBeanList_sb.clear();
        xsjjhxzDataBeanList_sb.addAll(xsjjhxzDataBeanSet);
    }

    private void setListData() {
        if (adapter == null) {
            adapter = new CommonAdapter<XSJJHXZDataBean>(context, R.layout.xsgzsblist_item, xsjjhxzDataBeanList_sb) {
                @Override
                protected void convert(ViewHolder viewHolder, XSJJHXZDataBean item, int position) {
                    viewHolder.setText(R.id.tv_qymc, item.getSbmc());
                    if (item.getSBMCSTATE() == null) {
                        viewHolder.setText(R.id.tv_sbstate, " ");
                    } else {
                        viewHolder.setText(R.id.tv_sbstate, item.getSBMCSTATE() + "");
                    }

                }
            };

            lv.addHeaderView(headView, null, false);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener((adapterView, view, postion, l) -> {
                pos = postion;
                List<String> datas = new ArrayList<>();
                datas.add("已运行");
                datas.add("已停用");
                datas.add("大小修");
                ListDialog dialog = new ListDialog(context, datas, (text, position) -> {
                    state = text;
                    switch (text) {
                        case "已运行": {
                            ContentValues values3 = new ContentValues();
                            values3.put("checked", false);
                            values3.put("CJJG", "");
                            values3.put("SBMCSTATE", text);
                            values3.put("SBMCSTATEVALUE", "5");
                            int i = DataSupport.updateAll(XSJJHDataBean.class, values3, "sbmc = ?", xsjjhxzDataBeanList_sb.get(postion - 1).getSbmc());
                            if (i != 0) {
                                showToast("修改设备状态成功");
                            } else {
                                showToast("修改设备状态失败");
                            }
                            break;
                        }
                        case "已停用": {
                            ContentValues values = new ContentValues();
                            values.put("checked", true);
                            values.put("SBMCSTATEVALUE", "3");
                            values.put("CJJG", text);
                            values.put("SBMCSTATE", text);
                            int i = DataSupport.updateAll(XSJJHDataBean.class, values, "sbmc = ? and  TJXJZT = ? ", xsjjhxzDataBeanList_sb.get(postion - 1).getSbmc(), "1");//如果TJXJZT为1 则不需要打点
                            if (i != 0) {
                                showToast("修改设备状态成功");
                            } else {
                                showToast("修改设备状态失败");
                            }
                            break;
                        }
                        case "大小修": {
                            ContentValues values2 = new ContentValues();
                            values2.put("checked", true);
                            values2.put("SBMCSTATEVALUE", "4");
                            values2.put("CJJG", text);
                            values2.put("SBMCSTATE", text);
                            int i = DataSupport.updateAll(XSJJHDataBean.class, values2, "sbmc = ?", xsjjhxzDataBeanList_sb.get(postion - 1).getSbmc());
                            if (i != 0) {
                                showToast("修改设备状态成功");
                            } else {
                                showToast("修改设备状态失败");
                            }
                            break;
                        }

                    }
                    ContentValues values2 = new ContentValues();
                    switch (text) {
                        case "已停用":
                            values2.put("SBMCSTATE", text);
                            values2.put("SBMCSTATEVALUE", "3");
                            break;
                        case "大小修":
                            values2.put("SBMCSTATE", text);
                            values2.put("SBMCSTATEVALUE", "4");
                            break;
                        case "已运行":
                            values2.put("SBMCSTATE", text);
                            values2.put("SBMCSTATEVALUE", "5");
                            break;
                    }

                    int J = DataSupport.updateAll(XSJJHXZDataBean.class, values2, "sbmc = ? ", xsjjhxzDataBeanList_sb.get(postion - 1).getSbmc());
                    if (J != 0) {
                        showToast("修改设备状态成功");
                    } else {
                        showToast("修改设备状态失败");
                    }
                });
                dialog.setTvTitle("选择设备状态");
                dialog.show();
                dialog.setOnDismissListener(dialog1 -> {
                    if (pos != -1) {
                        xsjjhxzDataBeanList_sb.get(pos - 1).setSBMCSTATE(state);
                        adapter.notifyDataSetChanged();
//                                SetxjSbModel setxjSbModel = new SetxjSbModel();
//                                setxjSbModel.setSbId(infos.get(pos - 1).getSbmc());
//                                setxjSbModel.setValue(state);
//                                setxjSbModel.setStatu(true);
//                                setSbModelList.add(setxjSbModel);
                    }
                });
            });
        } else {
            adapter.notifyDataSetChanged();
        }


    }

}