package com.rehome.bhdxj.activity.sbxdj;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.rehome.bhdxj.DBModel.QYDDATABean;
import com.rehome.bhdxj.R;
import com.rehome.bhdxj.adapter.CommonAdapter;
import com.rehome.bhdxj.adapter.ViewHolder;
import com.rehome.bhdxj.base.BaseActivity3;
import com.rehome.bhdxj.bean.SetSbModel;
import com.rehome.bhdxj.bean.sbInfo;
import com.rehome.bhdxj.contans.Contans;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import static org.litepal.crud.DataSupport.where;

/**
 * 设备巡点检管理-点检设备列表
 */
public class SdjSbListActivity extends BaseActivity3 {

    ListView lv;
    TextView tvNodata;

    private View headView;
    private CommonAdapter<sbInfo> adapter;
    private List<QYDDATABean> qyddataBeanArrayList;
    private boolean isEdit = true;
    private int item;
    private ArrayList<sbInfo> infos;
    Intent intent;
    private String LX, LXResult;
    private int itemposition;
    private int from;//0-来自工作页面；1-采集页面
    private long xdjjhxzDataBean_id;
    private String GWID;
    private ArrayList<SetSbModel> setSbModelList = new ArrayList<>();

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

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

    @Override
    public void initData() {
        initNFC();
        initToolbar("当前设备", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (from == 0) { //工作页面
                    finish();
                } else if (from == 1) { //采集页面
                    Intent intent = new Intent(SdjSbListActivity.this, SbxdjcjsbActivity.class);
                    intent.putParcelableArrayListExtra("setSbModelList", setSbModelList);
                    setResult(RESULT_OK, intent);
                }
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        Bundle bundle = SdjSbListActivity.this.getIntent().getExtras();

        qyddataBeanArrayList = bundle.getParcelableArrayList(Contans.KEY_DJJHRWQY);
        item = bundle.getInt(Contans.KEY_ITEM);
        GWID = bundle.getString("GWID");
        isEdit = bundle.getBoolean("edit");
        itemposition = bundle.getInt("itemposition");
        xdjjhxzDataBean_id = bundle.getLong("xdjjhxzDataBean_id");
        LX = bundle.getString("LX");
        LXResult = bundle.getString("LXResult");
        from = bundle.getInt("from");

        switch (LX) {
            case "Click":
                qyddataBeanArrayList = where("xdjjhxzDataBean_id = ?", xdjjhxzDataBean_id + "").find(QYDDATABean.class);
                break;
            case "NFC":
                qyddataBeanArrayList = where("QYNFC = ?", LXResult + "").find(QYDDATABean.class);
                break;
        }
        setListData();
    }


    private void setListData() {
        infos = new ArrayList<>();
        LinkedHashMap<String, sbInfo> maps = new LinkedHashMap<String, sbInfo>();
        for (QYDDATABean rw : qyddataBeanArrayList) {
            sbInfo sbinfo = new sbInfo();
            sbinfo.setSbmc(rw.getSBMC());
            sbinfo.setQybh(rw.getQYBH());
            sbinfo.setSbid(rw.getSBID());
            sbinfo.setSbstate(rw.getSBMCSTATE());
            String Key = rw.getSBID() + "_" + rw.getSBMC();
            sbInfo data = maps.get(Key);
            if (data == null) {
                maps.put(Key, sbinfo);
            }
        }
        infos.addAll(maps.values());
        adapter = new CommonAdapter<sbInfo>(context, R.layout.djajhsb_item, infos) {
            @Override
            protected void convert(ViewHolder viewHolder, sbInfo item, int position) {
                viewHolder.setText(R.id.tv_sbmc, item.getSbmc() + "");
                if (item.getSbstate() == null) {
                    viewHolder.setText(R.id.tv_sbstate, " ");
                } else {
                    viewHolder.setText(R.id.tv_sbstate, item.getSbstate() + "");
                }
            }
        };
        lv.setAdapter(adapter);
        if (isEdit) {
            lv.setOnItemClickListener((adapterView, view, position, l) -> {
                intent = new Intent(SdjSbListActivity.this, Yul_SBActivity.class);
                Bundle bundle2 = new Bundle();
                bundle2.putBoolean("edit", isEdit);
                bundle2.putInt(Contans.KEY_ITEM, item);
                bundle2.putLong("xdjjhxzDataBean_id", xdjjhxzDataBean_id);
                bundle2.putInt("itemposition", itemposition);
                bundle2.putString("LX", LX);
                bundle2.putString("LXResult", LXResult);
                bundle2.putString("GWID", GWID);
                bundle2.putParcelableArrayList("infos", infos);//当前设备页列表数据
                bundle2.putInt("itemIndex", position - 1);//当前点击的item索引
                intent.putExtras(bundle2);
                startActivity(intent);
                finish();
            });
        } else {
            showToast("请使用NFC");

        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                switch (LX) {
                    case "Click":
                        qyddataBeanArrayList.clear();
                        qyddataBeanArrayList = where("xdjjhxzDataBean_id = ?", xdjjhxzDataBean_id + "").find(QYDDATABean.class);
                        setListData();
                        adapter.notifyDataSetChanged();
                        break;
                    case "NFC":
                        qyddataBeanArrayList.clear();
                        qyddataBeanArrayList = where("QYNFC = ?", LXResult + "").find(QYDDATABean.class);
                        setListData();
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (LX) {
            case "Click":
                qyddataBeanArrayList.clear();
                qyddataBeanArrayList = where("xdjjhxzDataBean_id = ?", xdjjhxzDataBean_id + "").find(QYDDATABean.class);
                setListData();
                adapter.notifyDataSetChanged();
                break;
            case "NFC":
                qyddataBeanArrayList.clear();
                qyddataBeanArrayList = where("QYNFC = ?", LXResult + "").find(QYDDATABean.class);
                setListData();
                adapter.notifyDataSetChanged();
                break;
        }
    }
}