package com.rehome.bhdxj.activity.sbxj;

import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.rehome.bhdxj.DBModel.Djjh;
import com.rehome.bhdxj.DBModel.QYDDATABean;
import com.rehome.bhdxj.DBModel.XDJJHXZDataBean;
import com.rehome.bhdxj.DBModel.XSJJHDataBean;
import com.rehome.bhdxj.DBModel.XSJJHXZDataBean;
import com.rehome.bhdxj.DBModel.Xjjh;
import com.rehome.bhdxj.R;
import com.rehome.bhdxj.adapter.ScxsAdapter;
import com.rehome.bhdxj.base.BaseFragment;
import com.rehome.bhdxj.bean.ScxjjhBean;
import com.rehome.bhdxj.bean.StatusInfo2;
import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.HttpResponseListener;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.utils.UiUtlis;
import com.rehome.bhdxj.weight.WaitDialog;
import com.rehome.bhdxj.weight.toastviewbymyself;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.appcompat.app.AlertDialog;

/**
 * 设备巡点检管理-点检点上传
 */
public class ScxsjhFragment extends BaseFragment {

    ListView lv;
    Button btn_sc;
    TextView tvNodata;
    Button btn_del;

    private XjMainActivity mActivity;
    private View headView;
    private View head;
    private CheckBox xj_cb;
    private ScxsAdapter adapter;
    private WaitDialog dialog;
    private RequestQueue queue;
    private int noCheck = 0;
    private List<XSJJHXZDataBean> xsjjhxzDataBeanList = new ArrayList<>();
    private String zxid = "";
    private List<XSJJHXZDataBean> xsjhxzdatalist = new ArrayList<>();
    private Map<String, List<XSJJHDataBean>> xsJjhDataBeanMap = new HashMap<>();

    public ScxsjhFragment() {

    }

    public static ScxsjhFragment newInstance() {
        ScxsjhFragment fragment = new ScxsjhFragment();
        return fragment;
    }


    @Override
    protected void initView() {
        lv = view.findViewById(R.id.lv);
        btn_sc = view.findViewById(R.id.btn_sc);
        btn_del = view.findViewById(R.id.btn_del);
        tvNodata = view.findViewById(R.id.tv_nodata);

        lv.setEmptyView(tvNodata);
        dialog = new WaitDialog(context, "上传中...");
        dialog.setCancelable(false);
        mActivity = (XjMainActivity) getActivity();
        headView = View.inflate(context, R.layout.scxscb_item, null);
        head = headView.findViewById(R.id.head);
        xj_cb = headView.findViewById(R.id.xj_cb);
        xj_cb.setOnClickListener(view -> {

            if (xj_cb.isChecked()) {
                for (int i = 0; i < xsjhxzdatalist.size(); i++) {
                    xsjhxzdatalist.get(i).setChecked(true);
                    adapter.notifyDataSetChanged();
                }
            } else {
                for (int i = 0; i < xsjhxzdatalist.size(); i++) {
                    xsjhxzdatalist.get(i).setChecked(false);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        btn_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadData();//上传勾选中的数据
            }
        });
        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除勾选中的数据
                deleteData();
            }
        });

    }


    @Override
    public int getContentViewId() {
        return R.layout.fragment_xssc;
    }

    public void initData() {
        queue = NoHttp.newRequestQueue(1);
        getDataInSQL();//获取数据
        //显示数据
        setListData();
    }


    /**
     * 获取数据库的数据
     */
    public void getDataInSQL() {


        xsjjhxzDataBeanList.clear();
        xsjjhxzDataBeanList.addAll(DataSupport.findAll(XSJJHXZDataBean.class));

        xsjhxzdatalist.clear();
        xsJjhDataBeanMap.clear();

        for (int i = 0; i < xsjjhxzDataBeanList.size(); i++) {

            if (!xsjjhxzDataBeanList.get(i).getZxid().equals(zxid)) {
                List<XSJJHDataBean> qydDataBeen = DataSupport.where("zxid = ?", xsjjhxzDataBeanList.get(i).getZxid()).find(XSJJHDataBean.class);
                zxid = xsjjhxzDataBeanList.get(i).getZxid();
                int count = 0;
                for (int j = 0; j < qydDataBeen.size(); j++) {
                    if (qydDataBeen.get(j).isChecked()) {
                        count++;
                    }
                }
                XSJJHXZDataBean xsjjhxzDataBean = new XSJJHXZDataBean();
                xsjjhxzDataBean.setQymc(xsjjhxzDataBeanList.get(i).getJhmc());
                xsjjhxzDataBean.setZxid(xsjjhxzDataBeanList.get(i).getZxid());
                xsjjhxzDataBean.setSczt(xsjjhxzDataBeanList.get(i).getSczt());
                xsjjhxzDataBean.setFxnr(xsjjhxzDataBeanList.get(i).getFxnr());
                xsjjhxzDataBean.setQybh(xsjjhxzDataBeanList.get(i).getQybh());
                xsjjhxzDataBean.setCountPercent(count + "/" + qydDataBeen.size());
                xsjhxzdatalist.add(xsjjhxzDataBean);
                xsJjhDataBeanMap.put(xsjjhxzDataBeanList.get(i).getZxid(), qydDataBeen);

                if (count != 0) {
                    toastviewbymyself.makeText(getActivity(), "有巡检数据未上传,请检查!!!", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    private void setListData() {

        adapter = new ScxsAdapter(context, xsjhxzdatalist, view -> {
            CheckBox checkBox = (CheckBox) view;
            int index = (int) checkBox.getTag();
            xsjhxzdatalist.get(index).setChecked(checkBox.isChecked());
            int count = 0;
            for (int a = 0; a < xsjhxzdatalist.size(); a++) {
                if (xsjhxzdatalist.get(a).isChecked()) {
                    count++;
                }
            }
            xj_cb.setChecked(count == xsjhxzdatalist.size());
            adapter.notifyDataSetChanged();
        });
        lv.addHeaderView(headView, null, false);
        lv.setAdapter(adapter);


        lv.setOnItemClickListener((adapterView, view, i, l) -> {

            xsjhxzdatalist.get(i - 1).setChecked(!xsjhxzdatalist.get(i - 1).isChecked());
            int count = 0;
            for (int a = 0; a < xsjhxzdatalist.size(); a++) {
                if (xsjhxzdatalist.get(a).isChecked()) {
                    count++;
                }
            }
            xj_cb.setChecked(count == xsjhxzdatalist.size());
            adapter.notifyDataSetChanged();
        });


    }

    private void uploadData() {

        noCheck = 0;//没有检查的数量

        for (int i = 0; i < xsjhxzdatalist.size(); i++) {

            if (xsjhxzdatalist.get(i).isChecked()) {

                ScxjjhBean scxjjhbean = new ScxjjhBean();

                scxjjhbean.setAction("XSCB_ZXJHD_SET");

                String zxidUpload = xsjhxzdatalist.get(i).getZxid();

                List<XSJJHDataBean> xsjjhdatabeanList = xsJjhDataBeanMap.get(xsjhxzdatalist.get(i).getZxid());

                scxjjhbean.setZxid(xsjhxzdatalist.get(i).getZxid());

                scxjjhbean.setQybh(xsjhxzdatalist.get(i).getQybh());

                List<ScxjjhBean.data> datalist = new ArrayList<>();

                for (int j = 0; j < xsjjhdatabeanList.size(); j++) {
                    ScxjjhBean.data datas = new ScxjjhBean.data();
                    datas.setScid(xsjjhdatabeanList.get(j).getScid());
                    datas.setDbh(xsjjhdatabeanList.get(j).getDbh());
                    if (xsjjhdatabeanList.get(j).getCJJG() == null) {
                        datas.setCbsz("");
                    } else {
                        datas.setCbsz(xsjjhdatabeanList.get(j).getCJJG());
                    }
                    if (xsjjhdatabeanList.get(j).getDATE() == null) {
                        datas.setDjsj("");
                    } else {
                        datas.setDjsj(xsjjhdatabeanList.get(j).getDATE());
                    }
                    if (xsjjhdatabeanList.get(j).getCJJG() == null) {
                        datas.setSBZT("");
                    } else if (xsjjhdatabeanList.get(j).getCJJG().equals("已停用")) {
                        datas.setSBZT("3");
                    } else if (xsjjhdatabeanList.get(j).getCJJG().equals("大小修")) {
                        datas.setSBZT("4");
                    } else {
                        datas.setSBZT("1");
                    }
                    datas.setZcmc((String) SPUtils.get(context, Contans.BZBH, ""));

                    String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
                    String username = RSAUtils.decryptBASE64StrLocal(usernameTemp);

                    datas.setCbr(username);
                    datas.setFxnr(xsjjhdatabeanList.get(j).getFxnr());
                    datas.setSmfx("");
                    datalist.add(datas);
                }


                scxjjhbean.setData(datalist);


                final String json = GsonUtils.GsonString(scxjjhbean);


                if (noCheck > 0) {     //如果未检查的数量大于0 则提示有未检查的项目

                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("提示");
                    builder.setTitle("你还有项目未检查，是否上传？");
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            sCData(json, zxidUpload);
                        }
                    });
                    builder.show();

                } else {
                    sCData(json, zxidUpload);
                }
            } else {
                noCheck++;//代表有没有检查的数量
            }
        }
    }

    private void sCData(String json, String zxidUpload) {
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.XSCB, RequestMethod.POST);
        request.setDefineRequestBodyForJson(json);
        queue.add(1, request, new HttpResponseListener<>(getActivity(), request, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                StatusInfo2 info = GsonUtils.GsonToBean(response.get(), StatusInfo2.class);

                try {
                    if (what == 1) {
                        if (info.getState() == 1) { //成功

                            showToast("上传数据成功");

                            xsjhxzdatalist.removeIf((item) -> {
                                if (item.getZxid().equals(zxidUpload)) {
                                    //这里处理那个删除item，更新UI
                                    DataSupport.deleteAll(XSJJHXZDataBean.class, "zxid = ?", zxidUpload);
                                    DataSupport.deleteAll(XSJJHDataBean.class, "zxid = ?", zxidUpload);
                                    DataSupport.deleteAll(Xjjh.class, "zxid = ?", zxidUpload);
                                }
                                return item.getZxid().equals(zxidUpload);
                            });

//                            for (XSJJHXZDataBean item : xsjhxzdatalist) {
//                                if (item.getZxid().equals(zxidUpload)) {
//                                    xsjhxzdatalist.remove(item);
//                                }
//                            }

                            adapter.notifyDataSetChanged();

                        } else {

                            showToast("上传数据失败");

                        }
                    }


                } catch (Exception e) {

                    showToast(UiUtlis.getString(context, R.string.data_error));

                    dialog.dismiss();

                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        }, false, true, "上传数据中...请稍等..."));
    }

//    private HttpListener<String> callback = new HttpListener<String>() {
//        @Override
//        public void onSucceed(int what, Response<String> response) {
//
//
//            StatusInfo2 info = GsonUtils.GsonToBean(response.get(), StatusInfo2.class);
//
//            try {
//                if (what == 1) {
//                    if (info.getState() == 1) { //成功
//
//                        showToast("上传数据成功");
//
//                        //这里处理那个删除item，更新UI
//                        for (int i = 0; i < xsjhxzdatalist.size(); i++) {
//
//                            if (xsjhxzdatalist.get(i).isChecked()) {
//
//                                DataSupport.deleteAll(XSJJHXZDataBean.class, "zxid = ?", xsjhxzdatalist.get(i).getZxid());
//
//                                DataSupport.deleteAll(XSJJHDataBean.class, "zxid = ?", xsjhxzdatalist.get(i).getZxid());
//
//                                DataSupport.deleteAll(Xjjh.class, "zxid = ?", xsjhxzdatalist.get(i).getZxid());
//
//                                xsjhxzdatalist.remove(xsjhxzdatalist.get(i));
//                            }
//
//                        }
//
//                        adapter.notifyDataSetChanged();
//
//                    } else {
//
//                        showToast("上传数据失败");
//
//                    }
//                }
//
//
//            } catch (Exception e) {
//
//                showToast(UiUtlis.getString(context, R.string.data_error));
//
//                dialog.dismiss();
//
//            }
//        }
//
//        @Override
//        public void onFailed(int what, Response<String> response) {
//            if (dialog != null && dialog.isShowing()) {
//                dialog.dismiss();
//            }
//        }
//    };


    private void deleteData() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("提示");
        builder.setTitle("你确定要删除？");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                xsjhxzdatalist.removeIf((item) -> {
                    if (item.isChecked()) {
                        DataSupport.deleteAll(XSJJHXZDataBean.class, "zxid = ?", item.getZxid());
                        DataSupport.deleteAll(XSJJHDataBean.class, "zxid = ?", item.getZxid());
                        DataSupport.deleteAll(Xjjh.class, "zxid = ?", item.getZxid());
                    }
                    return item.isChecked();
                });
//                for (int i = 0; i < xsjhxzdatalist.size(); i++) {
//                    if (xsjhxzdatalist.get(i).isChecked()) {
//                        DataSupport.deleteAll(XSJJHXZDataBean.class, "zxid = ?", xsjhxzdatalist.get(i).getZxid());
//                        DataSupport.deleteAll(XSJJHDataBean.class, "zxid = ?", xsjhxzdatalist.get(i).getZxid());
//                        DataSupport.deleteAll(Xjjh.class, "zxid = ?", xsjhxzdatalist.get(i).getZxid());
//                    }
//                }

                //刷新界面
                getDataInSQL();
                adapter.notifyDataSetChanged();
            }
        });
        builder.show();
    }
}