package com.bjzc.bhzhgd.activity.regular;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.bjzc.bhzhgd.DBModel.Dqgz;
import com.bjzc.bhzhgd.DBModel.XSJJHDataBean;
import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.adapter.CommonAdapter;
import com.bjzc.bhzhgd.adapter.ViewHolder;
import com.bjzc.bhzhgd.base.BaseActivity3;
import com.bjzc.bhzhgd.bean.SetxjSbModel;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.utils.SPUtils;
import com.bjzc.bhzhgd.weight.AllCheckDialog;
import com.google.gson.Gson;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;


public class DqgzActivity extends BaseActivity3 {

    ListView lv;
    TextView tvNodata;
    private boolean isEdit = true;
    private int item;
    private String qybh;
    //private ArrayList<XSJJHXZDataBean> xsjjhxzDataBeanArrayList = new ArrayList<>();//工作列表
    private ArrayList<Dqgz> xsjjhDataBeanArrayList = new ArrayList<>();
    //private List<DlbInfo> infos = new ArrayList<>();
    private View headView;
    private CommonAdapter<Dqgz> adapter;
    //private int itemposition;
    private String LX, LXResult;
    private ArrayList<SetxjSbModel> setxjSbModelArrayList;
    //测温
    public static final int REQUEST_CHECK_TEMPERATURE = 1;
    //测振
    public static final int REQUEST_CHECK_VIBRATION = 2;
    //观察和录入
    public static final int REQUEST_CHECK_SEE_INPUT = 3;
    private String type = null;
    private  String username;

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Contans.ACTION_YULTWO.equals(action)) { //更新设备点检页面返回时编辑的item内容
                Bundle bundle =intent.getExtras();
                if (bundle != null) {
                    Dqgz dqgzTemp = bundle.getParcelable("bean");
                    if (xsjjhDataBeanArrayList.size() > 0&&dqgzTemp!=null) {
                        String rwid = dqgzTemp.getRWID();
                        for (int i = 0; i < xsjjhDataBeanArrayList.size(); i++) {
                            Dqgz xsjjhDataBean = xsjjhDataBeanArrayList.get(i);
                            if (xsjjhDataBean.getRWID().equals(rwid)) {
                                xsjjhDataBean.setWorkChecked(dqgzTemp.isWorkChecked());
                                xsjjhDataBean.setSCNR(dqgzTemp.getSCNR());
                                xsjjhDataBean.setSCBZ(dqgzTemp.getSCBZ());
                                xsjjhDataBean.setDATE(dqgzTemp.getDATE());
                            }
                        }
                    }
                }
            }
        }
    };

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

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

        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);

    }

    @Override
    public void initData() {
        username = (String) SPUtils.get(context, Contans.USERID, "");
        initToolbar("定期工作记录", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        Bundle bundle = getIntent().getExtras();
        type = bundle.getString("type");

        List<Dqgz> xsjjhDataBeen = DataSupport.where("download = ? and mkbs = ? and username = ?", "1",type,username).find(Dqgz.class);
        xsjjhDataBeanArrayList.clear();
        xsjjhDataBeanArrayList.addAll(xsjjhDataBeen);

        setListAdapter();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Contans.ACTION_YULTWO);
        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, filter);
    }

    private void setListAdapter() {
        adapter = new CommonAdapter<Dqgz>(context, R.layout.dqzg_check_item, xsjjhDataBeanArrayList) {
            @Override
            protected void convert(ViewHolder viewHolder, Dqgz item, int position) {
                viewHolder.setText(R.id.tv_d, item.getZX_MC());
                if (item.isWorkChecked()) {
                    viewHolder.setText(R.id.tv_zt, "已检");
                    viewHolder.setTextColor(R.id.tv_zt, Color.parseColor("#00CD00"));//已经检查完成绿色
                }else{
                    viewHolder.setText(R.id.tv_zt, "未检");
                    viewHolder.setTextColor(R.id.tv_zt, Color.parseColor("#FF0000"));//未检查完成红色
                }
            }
        };


        //adapter = new TimerTaskAdapter(context, xsjjhDataBeanArrayList);
        lv.addHeaderView(headView, null, false);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener((parent, view, position, id) -> {
            Log.i("app", String.valueOf(position));
            Gson gson = new Gson();
            Dqgz xSJJHDataBean = xsjjhDataBeanArrayList.get(position - 1);
            Log.i("app", gson.toJson(xSJJHDataBean));

            if (xSJJHDataBean.getZX_LRFS().equals("1")) {
                Intent intent = new Intent(DqgzActivity.this, DqgzCheckActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("bean", xSJJHDataBean);
                bundle.putBoolean("edit", isEdit);
                intent.putExtras(bundle);
                startActivityForResult(intent, REQUEST_CHECK_SEE_INPUT);
            }else {
                if (xSJJHDataBean.getZX_LRFS().equals("0") && xSJJHDataBean.getZX_CBLX().equals("W")) {
//                    Intent intentTemplate = new Intent(DqgzActivity.this, CheckTemperatureDqgzActivity.class);
//                    Bundle bundle = new Bundle();
//                    bundle.putParcelable("bean", xSJJHDataBean);
//                    bundle.putString("checkTip", "日检破碎机，驱动部电机驱动端温度");
//                    bundle.putString("standerTip", "≤40℃");
//                    bundle.putString("checkType", "234233");
//                    bundle.putBoolean("edit", isEdit);
//                    intentTemplate.putExtras(bundle);
//                    startActivityForResult(intentTemplate, REQUEST_CHECK_TEMPERATURE);
                } else if (xSJJHDataBean.getZX_LRFS().equals("0") && xSJJHDataBean.getZX_CBLX().equals("Z")) {
//                    Intent intentTemplate = new Intent(DqgzActivity.this, CheckVibrationDqgzActivity.class);
//                    Bundle bundle = new Bundle();
//                    bundle.putParcelable("bean", xSJJHDataBean);
//                    bundle.putString("checkTip", "日检破碎机，驱动部电机振动");
//                    bundle.putString("standerTip", "≤11.2mm/s");
//                    bundle.putString("checkType", "234235");
//                    bundle.putBoolean("edit", isEdit);
//                    intentTemplate.putExtras(bundle);
//                    startActivityForResult(intentTemplate, REQUEST_CHECK_VIBRATION);
                } else {
                    Intent intent = new Intent(DqgzActivity.this, DqgzCheckActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("bean", xSJJHDataBean);
                    bundle.putBoolean("edit", isEdit);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, REQUEST_CHECK_SEE_INPUT);
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        if (data != null) {
//            if (Req == requestCode) {
//                setxjSbModelArrayList = data.getParcelableArrayListExtra("setSbModelList");
//                if (setxjSbModelArrayList == null) {
//
//                } else {
//                    for (int i = 0; i < setxjSbModelArrayList.size(); i++) {
//                        for (int j = 0; j < infos.size(); j++) {
//
//                            if (infos.get(j).getSbid().equals(setxjSbModelArrayList.get(i).getSbId()))
//
//                                infos.get(j).setCjjg(setxjSbModelArrayList.get(i).getValue());
//
////                        infos.get(j).setStatu(setSbModelList.get(i).getStatu());
//
//                        }
//                    }
//                    adapter.notifyDataSetChanged();
//                }
//                adapter.notifyDataSetChanged();
//            } else {
//                String temperatureValue = data.getStringExtra("value");
//                if (temperatureValue != null) {
//                    //先判断是哪个页面返回过来的
//                    switch (requestCode) {
//                        case REQUEST_CHECK_TEMPERATURE:
//                            //测温
//                            //成功了
//                            Toast.makeText(DqgzActivity.this, temperatureValue, Toast.LENGTH_SHORT).show();
//                            break;
//                        case REQUEST_CHECK_VIBRATION:
//                            //测振
//                            //成功了
//                            Toast.makeText(DqgzActivity.this, temperatureValue, Toast.LENGTH_SHORT).show();
//                            break;
//                    }
//                }
//            }
        }
    }

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


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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
    }

    public void rightBtnClickAllCheck() {
        AllCheckDialog allCheckDialog = new AllCheckDialog(DqgzActivity.this, "一键正常将会把所有选择类的选项状态改为已检，是否继续当前操作？", new AllCheckDialog.AuditDialogListener() {
            @Override
            public void confirm() {
                allCheckNow();
            }

            @Override
            public void cancel() {

            }
        });
        allCheckDialog.show();
    }

    private void allCheckNow() {
        ArrayList<XSJJHDataBean> xsjjhDataBeanArrayListTran = new ArrayList<>();
        if (xsjjhDataBeanArrayList.size() > 0) {
            for (int i = 0; i < xsjjhDataBeanArrayList.size(); i++) {
                Dqgz xSJJHDataBeanTemp = xsjjhDataBeanArrayList.get(i);
                if (xSJJHDataBeanTemp.getZX_LRFS().equals("1")) {
                    xSJJHDataBeanTemp.setWorkChecked(true);
                    xSJJHDataBeanTemp.setSCNR("正常");
                }
            }
        }
        adapter.notifyDataSetChanged();
    }
}