package com.bjzc.bhzhgd.activity.regular;

import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bjzc.bhzhgd.DBModel.Dqgz;
import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.adapter.QtgzAdapter;
import com.bjzc.bhzhgd.base.BaseFragment;
import com.bjzc.bhzhgd.bean.XsRequestInfo;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.utils.GsonUtils;
import com.bjzc.bhzhgd.utils.SPUtils;
import com.bjzc.bhzhgd.weight.WaitDialog;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.RequestQueue;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import static org.litepal.crud.DataSupport.where;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

/**
 * Created by huang on 2021/12/09
 * 定期工作-任务下载
 */

public class QtgzFragment extends BaseFragment {
    ListView lv;
    TextView tvNodata;
    FrameLayout FL;


    private WaitDialog dialog;
    private DqgzMainActivity mActivity;
    private View headView;
    private View head;
    private QtgzAdapter adapter;
    private List<Dqgz> xjjhs;
    //    private int selectCount = 0;
//    private int requestCount = 0;
    private List<String> zxids = new ArrayList<>();
    private List<String> jhmcs = new ArrayList<>();
    private RequestQueue queue;
    private String type = null;
    private String professionId = null;
    private  String username;

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Contans.ACTION_REFRESHPLANDOWNLOAD.equals(action)) {
                //刷新页面
                initData();
            }
            if(Contans.ACTION_PROFESSIONCHANGE.equals(action)){
                professionId=intent.getStringExtra("professionId");
                Log.i("app",professionId);
                Log.i("app","initData");
                //刷新页面
                initData();
            }
        }
    };

    public QtgzFragment() {

    }
    public QtgzFragment(String type) {
        this.type=type;
    }

    public static QtgzFragment newInstance(String type) {
        QtgzFragment fragment = new QtgzFragment(type);
        return fragment;
    }


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

    @Override
    protected void initView() {
        lv = view.findViewById(R.id.lv);
        tvNodata = view.findViewById(R.id.tv_nodata);
        FL = view.findViewById(R.id.FL);
        username = (String) SPUtils.get(context, Contans.USERID, "");
        lv.setEmptyView(tvNodata);
        mActivity = (DqgzMainActivity) getActivity();
        headView = View.inflate(context, R.layout.item_dqgz, null);
        head = headView.findViewById(R.id.head);
        head.setVisibility(View.VISIBLE);

        lv.addHeaderView(headView, null, false);
        TextView tv_taskName=headView.findViewById(R.id.tv_gwmc);
        tv_taskName.setText("任务名称");
        TextView tv_isCheck=headView.findViewById(R.id.tv_gwds);
        tv_isCheck.setText("状态");
        IntentFilter filter = new IntentFilter();
        filter.addAction(Contans.ACTION_REFRESHPLANDOWNLOAD);
        filter.addAction(Contans.ACTION_PROFESSIONCHANGE);
        LocalBroadcastManager.getInstance(requireActivity()).registerReceiver(myReceiver, filter);
    }

    public void initData() {

        queue = NoHttp.newRequestQueue(1);
        dialog = new WaitDialog(getActivity(), "加载中...");
        dialog.setCancelable(false);
        xjjhs = new ArrayList<>();
        //如果数据库中有数据
        if (DataSupport.count("Dqgz") != 0) {
            List<Dqgz> xjjhListall = new ArrayList<>();
            if(professionId!=null){
                xjjhListall = where("download = ? and mkbs = ? and zx_zy = ? and username = ?", "1",type,professionId,username).find(Dqgz.class);
            }else{
                xjjhListall = where("download = ? and mkbs = ? and username = ?", "1",type,username).find(Dqgz.class);
            }

            xjjhs.clear();
            //xjjhs.addAll(xjjhListall);
            if(xjjhListall!=null&&xjjhListall.size()>20){
                List<Dqgz> xjjhListTemp = new ArrayList<>();
                for(int i=0;i<xjjhListall.size();i++){
                    if(i>19){
                        Dqgz item=xjjhListall.get(i);
                        xjjhListTemp.add(item);
                    }
                }
                xjjhs.addAll(xjjhListTemp);
            }
            Log.i("app","-------");
            Log.i("app",String.valueOf(xjjhs.size()));
            setListData();
        } else {
            tvNodata.setText("暂无数据");
        }
    }


    //运行，燃料计划下载
    private void downData(String rwid) {
        //++requestCount;
        ContentValues values = new ContentValues();
        values.put("download", 1);
        DataSupport.updateAll(Dqgz.class, values, "rwid = ?", rwid);
    }

    private String createZyJson(String zxid, String jhmc) {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("YX_XJGL_RWXZ_GET");
        info.setZxid(zxid);
        //info.setJhmc(jhmc);
        String json = GsonUtils.GsonString(info);
        Log.i("app",json);
        return json;
    }



    private void setListData() {

        adapter = new QtgzAdapter(context, xjjhs, new QtgzAdapter.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++;
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });


        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++;
                }
            }
            adapter.notifyDataSetChanged();
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LocalBroadcastManager.getInstance(requireActivity()).unregisterReceiver(myReceiver);
    }
}