package com.rehome.sgbaxj.activity.sbxdj;

import android.content.ContentValues;
import android.content.Intent;
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.sgbaxj.DBModel.QYDDATABean;
import com.rehome.sgbaxj.DBModel.XDJJHXZDataBean;
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.contans.Contans;
import com.rehome.sgbaxj.weight.toastviewbymyself;

import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;
import static org.litepal.crud.DataSupport.where;


public class SdjgzActivity extends BaseActivity3 {

    ListView lv;
    TextView tvNodata;
    private View headView;
    private CommonAdapter<XDJJHXZDataBean> adapter;
    private List<XDJJHXZDataBean> xdjjhxzDataBeanList = new ArrayList<>();//工作列表
    private ArrayList<QYDDATABean> qyddataBeanList = new ArrayList<>();//点检记录列表
    private String GWID;
    Intent intent;

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

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

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

            }
        });
        Bundle bundle = SdjgzActivity.this.getIntent().getExtras();
        GWID = bundle.getString("GWID");
        xdjjhxzDataBeanList.clear();
        xdjjhxzDataBeanList.addAll(where("GWID = ?", GWID + "").find(XDJJHXZDataBean.class));
        setListData();
    }


    private void setListData() {
        if (adapter == null) {
            adapter = new CommonAdapter<XDJJHXZDataBean>(context, R.layout.djajhgz_item, xdjjhxzDataBeanList) {
                @Override
                protected void convert(ViewHolder viewHolder, XDJJHXZDataBean item, int position) {
                    int checkedCount = 0;
                    List<QYDDATABean> qyddataBeen = where("xdjjhxzDataBean_id = ?", item.getId() + "").find(QYDDATABean.class);
                    for (int i = 0; i < qyddataBeen.size(); i++) {
                        if (qyddataBeen.get(i).isChecked()) {
                            checkedCount++;
                        }
                    }
                    viewHolder.setText(R.id.tv_xh, item.getSN() + "");
                    viewHolder.setText(R.id.tv_qymc, item.getGWMC() + "--" + item.getQYMC());
                    viewHolder.setText(R.id.tv_djrw, checkedCount + "/" + qyddataBeen.size());
                    int a = checkedCount;
                    int b = qyddataBeen.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进行点检");
                qyddataBeanList.clear();
                qyddataBeanList.addAll(where("xdjjhxzDataBean_id = ?", xdjjhxzDataBeanList.get(postion - 1).getId() + "").find(QYDDATABean.class));

                if (qyddataBeanList.size() != 0) {
                    Bundle bundle = new Bundle();
                    Intent intent = new Intent(SdjgzActivity.this, SdjSbListActivity.class);
                    bundle.putParcelableArrayList(Contans.KEY_DJJHRWQY, qyddataBeanList);
                    bundle.putString("GWID", GWID);
                    bundle.putBoolean("edit", false);
//                    bundle.putBoolean("edit", true);
//                    if (qyddataBeanList.get(0).getSecond() != null) {
//                        if (qyddataBeanList.get(0).getSecond().equals("YES")) {//如果曾经扫描过二维码或者NFC进入到区域 则下次可以直接点击进入编辑
//                            bundle.putBoolean("edit", true);
//                        } else {
//                            bundle.putBoolean("edit", false);
//                        }
//                    }
                    bundle.putInt(Contans.KEY_ITEM, 0);
                    bundle.putInt("itemposition", xdjjhxzDataBeanList.get(postion - 1).getSN() - 1);
                    bundle.putLong("xdjjhxzDataBean_id", xdjjhxzDataBeanList.get(postion - 1).getId());
                    bundle.putString("LX", "Click");
                    bundle.putString("LXResult", "LXResult");
                    bundle.putInt("from", 0);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    showToast("暂无该区域点检任务,请联系管理员确认");
                }
            });
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void handleNfc(String result) {
        super.handleNfc(result);
        List<QYDDATABean> qydDataBeen = DataSupport.where("QYNFC = ?", result).find(QYDDATABean.class);//ewm是根据扫描得到的二维码结果来查询
        qyddataBeanList.clear();
        qyddataBeanList.addAll(qydDataBeen);
        if (qydDataBeen.size() == 0) {
            toastviewbymyself.makeText(SdjgzActivity.this, "无数据请联系管理员", Toast.LENGTH_SHORT).show();
        } else {
            Bundle bundle3 = new Bundle();
            Intent intent = new Intent(SdjgzActivity.this, SdjSbListActivity.class);
            bundle3.putParcelableArrayList(Contans.KEY_DJJHRWQY, qyddataBeanList);
            bundle3.putInt(Contans.KEY_ITEM, 0);
            bundle3.putString("GWID", GWID);
            bundle3.putBoolean("edit", true);
            bundle3.putInt("itemposition", 0);
            bundle3.putLong("xdjjhxzDataBean_id", 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();
        adapter.notifyDataSetChanged();
    }

}