package com.rehome.bhdxj.activity.sbxj;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.rehome.bhdxj.DBModel.XSJJHXZBean;
import com.rehome.bhdxj.DBModel.XSJJHXZDataBean;
import com.rehome.bhdxj.DBModel.Xjjh;
import com.rehome.bhdxj.R;
import com.rehome.bhdxj.adapter.XzxsjhAdapter;
import com.rehome.bhdxj.base.BaseFragment;
import com.rehome.bhdxj.bean.XsRequestInfo;
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.UiUtlis;
import com.rehome.bhdxj.weight.WaitDialog;
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.List;
import androidx.appcompat.app.AlertDialog;
import static org.litepal.crud.DataSupport.where;

/**
 * Created by ruihong on 2018/4/8.
 */

public class XzxsjhFragment extends BaseFragment {

    ListView lv;
    TextView tvNodata;
    FrameLayout FL;
    Button btnXz;
    Button btnDelete;
    LinearLayout LL;


    private WaitDialog dialog;
    private XjMainActivity mActivity;
    private View headView;
    private View head;
    private XzxsjhAdapter adapter;
    private CheckBox cb;
    private List<Xjjh> xjjhs;
    private int selectCount = 0;
    private int requestCount = 0;
    private List<String> zxids = new ArrayList<>();
    private List<String> jhmcs = new ArrayList<>();
    private RequestQueue queue;

    public XzxsjhFragment() {

    }

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


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

    @Override
    protected void initView() {
        lv = view.findViewById(R.id.lv);
        tvNodata = view.findViewById(R.id.tv_nodata);
        FL = view.findViewById(R.id.FL);
        btnXz = view.findViewById(R.id.btn_xz);
        btnDelete = view.findViewById(R.id.btn_delete);
        LL = view.findViewById(R.id.LL);

        lv.setEmptyView(tvNodata);
        mActivity = (XjMainActivity) getActivity();
        headView = View.inflate(context, R.layout.xzxsjh_item, null);
        head = headView.findViewById(R.id.head);
        head.setVisibility(View.VISIBLE);
        cb = headView.findViewById(R.id.cb);
        cb.setOnClickListener(view -> {

            if (cb.isChecked()) {
                for (int i = 0; i < xjjhs.size(); i++) {
                    xjjhs.get(i).setChecked(true);
                    adapter.notifyDataSetChanged();
                }
            } else {
                for (int i = 0; i < xjjhs.size(); i++) {
                    xjjhs.get(i).setChecked(false);
                    adapter.notifyDataSetChanged();
                }
            }
        });

        btnXz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (xjjhs.size() != 0) {

                    zxids.clear();
                    jhmcs.clear();
                    for (int i = 0; i < xjjhs.size(); i++) {
                        if (xjjhs.get(i).isChecked()) {
                            if (!dialog.isShowing()) {
                                dialog.setMessage("下载计划中...");
                                dialog.show();
                            }

                            downData(xjjhs.get(i).getZxid(), xjjhs.get(i).getJhmc());
                            zxids.add(xjjhs.get(i).getZxid());
                        }
                    }
                } else {
                    showToast("没有可下载计划");
                }
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (xjjhs.size() != 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) {
                            List<Xjjh> delete = new ArrayList<>();
                            for (int i = 0; i < xjjhs.size(); i++) {
                                if (xjjhs.get(i).isChecked()) {
                                    delete.add(xjjhs.get(i));
                                    DataSupport.deleteAll(Xjjh.class, "zxid = ?", xjjhs.get(i).getZxid());
                                }
                            }
                            xjjhs.removeAll(delete);
                            adapter.notifyDataSetChanged();
                        }
                    });
                    builder.show();

                } else {
                    showToast("没有可删除计划");
                }
            }
        });
    }

    public void initData() {

        queue = NoHttp.newRequestQueue(1);
        dialog = new WaitDialog(getActivity(), "加载中...");
        dialog.setCancelable(false);
        xjjhs = new ArrayList<>();
        //如果数据库中有数据
        if (DataSupport.count("Xjjh") != 0) {
            List<Xjjh> xjjhListall = where("download = ?", "0").find(Xjjh.class);
            xjjhs.clear();
            xjjhs.addAll(xjjhListall);
            setListData();
        } else {
            tvNodata.setText("暂无数据");
            LL.setVisibility(View.GONE);
        }
    }

    private void downData(String zxid, String jhmc) {

        selectCount++;

        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.XSCB, RequestMethod.POST);

        request.setDefineRequestBodyForJson(createZyJson(zxid, jhmc));

        queue.add(1, request, new HttpResponseListener<>(getActivity(), request, callback, false, false, ""));

    }

    private XSJJHXZBean xsjjhxzBean;

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {

            try {

                if (what == 1) {

                    ++requestCount;

                    Log.e("dataLog", response.get());

                    xsjjhxzBean = GsonUtils.GsonToBean(response.get(), XSJJHXZBean.class);

                    if (xsjjhxzBean != null && xsjjhxzBean.getState() == 1) {

                        List<XSJJHXZDataBean> xsjjhxzDataBeanList = xsjjhxzBean.getData();

                        ContentValues values = new ContentValues();

                        values.put("download", 1);

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

                            DataSupport.updateAll(Xjjh.class, values, "zxid = ?", xsjjhxzDataBeanList.get(i).getZxid());
                        }

                        int dataCount = DataSupport.count(XSJJHXZDataBean.class);

                        int index;//序号

                        index = dataCount;

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

                            xsjjhxzDataBeanList.get(i).setSN(++index);

                            DataSupport.saveAll(xsjjhxzDataBeanList.get(i).getData());

                        }

                        DataSupport.saveAll(xsjjhxzDataBeanList);

                    } else {

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

                    }
                }

                if (requestCount == selectCount && requestCount != 0) {
                    requestCount = 0;
                    selectCount = 0;
                    tvNodata.setText("暂无数据");
                    requestCount = 0;
                    selectCount = 0;
                    dialog.cancel();
                    xjjhs.clear();
                    xjjhs.addAll(where("download = ?", "0").find(Xjjh.class));
                    if (adapter != null) {
                        cb.setChecked(false);
                        adapter.notifyDataSetChanged();
                    }
                    showToast("下载成功");
                    dialog.dismiss();
                }


            } 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 String createZyJson(String zxid, String jhmc) {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("XSCB_ZXJHD_GET");
        info.setZxid(zxid);
        info.setJhmc(jhmc);
        String json = GsonUtils.GsonString(info);
        return json;
    }

    private void setListData() {

        adapter = new XzxsjhAdapter(context, xjjhs, new XzxsjhAdapter.CallBack() {
            @Override
            public void Click(View view) {

                CheckBox checkBox = (CheckBox) view;
                int index = (int) checkBox.getTag();
                xjjhs.get(index).setChecked(checkBox.isChecked());
                int count = 0;
                for (int a = 0; a < xjjhs.size(); a++) {
                    if (xjjhs.get(a).isChecked()) {
                        count++;
                    }
                }
                cb.setChecked(count == xjjhs.size());
                adapter.notifyDataSetChanged();
            }
        });


        lv.addHeaderView(headView, null, false);
        lv.setAdapter(adapter);

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

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