package com.rehome.sgbaxj.activity.sbxj;


import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
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.contans.Contans;
import com.rehome.sgbaxj.utils.SPUtils;

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


public class CheckedStatusListActivity extends BaseActivity3 implements View.OnClickListener {

    ListView lv;
    TextView tvNodata;
    private View headView;
    private TextView tv_djrw;
    private CommonAdapter<XSJJHXZDataBean> adapter;
    private List<XSJJHXZDataBean> xsjjhxzDataBeanList = new ArrayList<>();
    private List<XSJJHXZDataBean> checkedList = new ArrayList<>();
    private List<XSJJHXZDataBean> noCheckeList = new ArrayList<>();
    private List<XSJJHXZDataBean> checkedListBA = new ArrayList<>();
    private List<XSJJHXZDataBean> noCheckeListBA = new ArrayList<>();
    private List<String> qybh = new ArrayList<>();
    private ArrayList<XSJJHDataBean> xsjjhDataBeanArrayList = new ArrayList<>();//点检记录列表
    private String type = null;
    private String checkType = null;
    private String zxid = null;
    private String username;

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_right:
                break;
        }
    }

    @Override
    public void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        checkType = intent.getStringExtra("checkType");
        zxid = intent.getStringExtra("zxid");
        username = (String) SPUtils.get(context, Contans.USERID, "");
        lv.setEmptyView(tvNodata);
        headView = View.inflate(this, R.layout.checked_status_xsgz_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        tv_djrw = headView.findViewById(R.id.tv_djrw);
        if(checkType!=null&&checkType.equals("checked")){
            initToolbar("已检", "", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }else{
            initToolbar("未检", "", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }

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

    //加载数据库中已经下载的计划
    private void getDataInSqlite() {
        xsjjhxzDataBeanList.clear();
        checkedListBA.clear();
        noCheckeListBA.clear();
        checkedList.clear();
        noCheckeList.clear();
        List<XSJJHXZDataBean> planTemp = where("mk = ? and username = ? and zxid = ?", type, username,zxid).find(XSJJHXZDataBean.class);
        List<XSJJHXZDataBean> planTemp1 = this.removeDuplicate(planTemp);
        xsjjhxzDataBeanList.addAll(planTemp1);
        Log.i("app",String.valueOf(xsjjhxzDataBeanList.size()));
        for (int i = 0; i < xsjjhxzDataBeanList.size(); i++) {
            XSJJHXZDataBean item = xsjjhxzDataBeanList.get(i);
            if (type != null && type.equals("BA")) {
                if(item.isBAChecked()){
                    checkedListBA.add(item);
                }else{
                    noCheckeListBA.add(item);
                }
            }else{
                int checkedCount = 0;
                List<XSJJHDataBean> xsjjhdatabean = DataSupport.where("qybh = ? and mk = ? and username = ?", item.getQybh(), type, username).find(XSJJHDataBean.class);
                for (int j = 0; j< xsjjhdatabean.size(); j++) {
                    if (xsjjhdatabean.get(j).isChecked()) {
                        checkedCount++;
                    }
                }
                int a = checkedCount;
                int b = xsjjhdatabean.size();
                if (a == b) {
                    checkedList.add(item);
                } else {
                    noCheckeList.add(item);
                }
            }
        }
        if (type != null && type.equals("BA")) {
            if(checkType!=null&&checkType.equals("checked")){
                xsjjhxzDataBeanList.clear();
                xsjjhxzDataBeanList.addAll(checkedListBA);
            }else{
                xsjjhxzDataBeanList.clear();
                xsjjhxzDataBeanList.addAll(noCheckeListBA);
            }
        }else{
            if(checkType!=null&&checkType.equals("checked")){
                xsjjhxzDataBeanList.clear();
                xsjjhxzDataBeanList.addAll(checkedList);
            }else{
                xsjjhxzDataBeanList.clear();
                xsjjhxzDataBeanList.addAll(noCheckeList);
            }
        }
    }

    public List<XSJJHXZDataBean> removeDuplicate(List<XSJJHXZDataBean> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).getQybh().equals(list.get(i).getQybh())) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    private void setListData() {

        if (adapter == null) {
            adapter = new CommonAdapter<XSJJHXZDataBean>(context, R.layout.checked_status_xsgz_item, xsjjhxzDataBeanList) {
                @Override
                protected void convert(ViewHolder viewHolder, XSJJHXZDataBean item, int position) {
                    if (type != null && type.equals("BA")) {
                        viewHolder.setText(R.id.tv_xh, String.valueOf(position + 1));
                        viewHolder.setText(R.id.tv_qymc, item.getQymc());
                        if (item.isBAChecked()) {
                            viewHolder.setText(R.id.tv_djrw, "已检");
                            viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#00CD00"));//已经检查完成绿色
                        } else {
                            viewHolder.setText(R.id.tv_djrw, "未检");
                            viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#FF0000"));//未检查完成红色
                        }
                    } else {
                        int checkedCount = 0;
                        List<XSJJHDataBean> xsjjhdatabean = DataSupport.where("qybh = ? and mk = ? and username = ?", item.getQybh(), type, username).find(XSJJHDataBean.class);
                        for (int i = 0; i < xsjjhdatabean.size(); i++) {
                            if (xsjjhdatabean.get(i).isChecked()) {
                                checkedCount++;
                            }
                        }
                        viewHolder.setText(R.id.tv_xh, String.valueOf(position + 1));
                        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.setText(R.id.tv_djrw, "已检");
                            viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#00CD00"));//已经检查完成绿色
                        } else {
                            viewHolder.setText(R.id.tv_djrw, "未检");
                            viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#FF0000"));//未检查完成红色
                        }
                    }
                }
            };

            lv.addHeaderView(headView, null, false);
            lv.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }
}