package com.rehome.bhdxj.activity.sbxj;

import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.rehome.bhdxj.DBModel.XSJJHDataBean;
import com.rehome.bhdxj.DBModel.XSJJHXZDataBean;
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.contans.Contans;
import com.rehome.bhdxj.weight.toastviewbymyself;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;
import static org.litepal.crud.DataSupport.where;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


public class SxgzActivity extends BaseActivity3 {

    ListView lv;
    TextView tvNodata;
    private View headView;
    private CommonAdapter<XSJJHXZDataBean> adapter;
    private List<XSJJHXZDataBean> xsjjhxzDataBeanList = new ArrayList<>();
    private ArrayList<XSJJHDataBean> xsjjhDataBeanArrayList = new ArrayList<>();//点检记录列表

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


    @Override
    public void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);

        lv.setEmptyView(tvNodata);
        headView = View.inflate(this, R.layout.xsgz_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);

        initNFC();
        initToolbar("巡检工作(请使用NFC)", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    public void initData() {
        getDataInSqlite();
        setListData();
    }


    //加载数据库中已经下载的计划
    private void getDataInSqlite() {
        xsjjhxzDataBeanList.clear();
        xsjjhxzDataBeanList.addAll(DataSupport.findAll(XSJJHXZDataBean.class));

    }

    private void setListData() {

        if (adapter == null) {
            adapter = new CommonAdapter<XSJJHXZDataBean>(context, R.layout.xsgz_item, xsjjhxzDataBeanList) {
                @Override
                protected void convert(ViewHolder viewHolder, XSJJHXZDataBean item, int position) {
                    int checkedCount = 0;
                    List<XSJJHDataBean> xsjjhdatabean = where("xsjjhxzDataBean_id = ?", item.getId() + "").find(XSJJHDataBean.class);
                    for (int i = 0; i < xsjjhdatabean.size(); i++) {
                        if (xsjjhdatabean.get(i).isChecked()) {
                            checkedCount++;
                        }
                    }
                    viewHolder.setText(R.id.tv_xh, item.getSN() + "");
                    viewHolder.setText(R.id.tv_qymc, item.getQymc());
                    viewHolder.setText(R.id.tv_djrw, checkedCount + "/" + xsjjhdatabean.size());
                    int a = checkedCount;
                    int b = xsjjhdatabean.size();
                    if (a == b) {
                        viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#00CD00"));//已经检查完成绿色
                    } else {
                        viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#FF0000"));//未检查完成红色
                    }
                }
            };


            lv.addHeaderView(headView, null, false);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener((adapterView, view, postion, l) -> {
                showToast("请使用NFC进行巡检");
                xsjjhDataBeanArrayList.clear();
                xsjjhDataBeanArrayList.addAll(where("xsjjhxzDataBean_id = ?", xsjjhxzDataBeanList.get(postion - 1).getId() + "").find(XSJJHDataBean.class));
                if (xsjjhDataBeanArrayList.size() != 0) {
                    Bundle bundle = new Bundle();
                    Intent intent = new Intent(SxgzActivity.this, XjYulActivity.class);
                    bundle.putParcelableArrayList("xsjjhDataBeanArrayList", xsjjhDataBeanArrayList);
                    bundle.putBoolean("edit", false);
//                    bundle.putBoolean("edit", true);
//                    if (xsjjhDataBeanArrayList.get(0).getSecond() != null) {
//                        if (xsjjhDataBeanArrayList.get(0).getSecond().equals("YES")) {//如果曾经扫描过二维码或者NFC进入到区域 则下次可以直接点击进入编辑
//                            bundle.putBoolean("edit", true);
//                        } else {
//                            bundle.putBoolean("edit", false);
//                        }
//                    }
                    bundle.putInt(Contans.KEY_ITEM, 0);
                    bundle.putInt("itemposition", postion - 1);
                    bundle.putString("LX", "Click");
                    bundle.putString("LXResult", "LXResult");
                    bundle.putInt("from", 0);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }


    //处理NFC的数据
    @Override
    public void handleNfc(String result) {
        super.handleNfc(result);
        List<XSJJHDataBean> xsjjhDataBeen = DataSupport.where("nfcbm = ?", result).find(XSJJHDataBean.class);
        xsjjhDataBeanArrayList.clear();
        xsjjhDataBeanArrayList.addAll(xsjjhDataBeen);
        if (xsjjhDataBeen.size() == 0) {
            toastviewbymyself.makeText(SxgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
        } else {
            Bundle bundle3 = new Bundle();
            Intent intent = new Intent(SxgzActivity.this, XjYulActivity.class);
            bundle3.putParcelableArrayList("xsjjhDataBeanArrayList", xsjjhDataBeanArrayList);
            bundle3.putBoolean("edit", true);
            bundle3.putInt(Contans.KEY_ITEM, 0);
            bundle3.putInt("itemposition", 0);
            bundle3.putString("LX", "NFC");
            bundle3.putString("LXResult", result);
            bundle3.putInt("from", 0);
            intent.putExtras(bundle3);
            ContentValues values = new ContentValues();
            values.put("second", "YES");
            startActivity(intent);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getDataInSqlite();
        adapter.notifyDataSetChanged();
    }
}