package com.zztrapp.zztrapp.ui.index.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.github.gzuliyujiang.wheelpicker.DatePicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.ZdjgdwJdxxBean;
import com.zztrapp.zztrapp.constant.Constant;
import com.zztrapp.zztrapp.databinding.ActivityZdjgdwJdxxBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.ui.index.adapter.ZdjgdwJdxxAdapter;
import com.zztrapp.zztrapp.utilcode.mUtil.MyUtil;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

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

/**
 * Describe 重点监管单位分页查询-监督信息-分页列表查询
 * Created by Bby on 2022/8/2
 * Email bby0856@163.com
 */
public class ZdjgdwJdxxActivity extends BaseActivity {

    private ActivityZdjgdwJdxxBinding binding;
    private int pageNo = 1;//页数
    private int total = 0;//总共多少页
    private String pid;
    private String startTime = "";//开始时间
    private String endTime = "";//结束时间
    private boolean isShowDialog = true;
    private boolean isRefresh = true;//true 刷新 false 加载
    private List<ZdjgdwJdxxBean.RecordsBean> list = new ArrayList<>();
    private ZdjgdwJdxxAdapter adapter;

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityZdjgdwJdxxBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onResume() {
        super.onResume();
        isShowDialog = false;
        isRefresh = true;
        pageNo = 1;
        loadList();
    }

    @Override
    protected void init() {
        pid = getIntent().getStringExtra("id");
        binding.navView.navTitle.setText("监管信息");
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.refresh.setRefreshHeader(new ClassicsHeader(ZdjgdwJdxxActivity.this));
        binding.refresh.setRefreshFooter(new ClassicsFooter(ZdjgdwJdxxActivity.this));
        binding.refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isShowDialog = false;
                isRefresh = false;
                pageNo += 1;
                if (pageNo <= total) {
                    loadList();
                }
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isShowDialog = false;
                isRefresh = true;
                pageNo = 1;
                loadList();
                refreshLayout.finishRefresh();
            }
        });

        binding.rv.setLayoutManager(new LinearLayoutManager(ZdjgdwJdxxActivity.this, LinearLayoutManager.VERTICAL, false));
        adapter = new ZdjgdwJdxxAdapter(ZdjgdwJdxxActivity.this);
        binding.rv.setAdapter(adapter);
    }

    @Override
    protected void initOnClick() {
        adapter.setOnItemClickListener(new ZdjgdwJdxxAdapter.OnItemClickListener() {
            @Override
            public void onEditClick(String id, int position) {
                Intent intent = new Intent(ZdjgdwJdxxActivity.this, AddZdjgdwJdxxActivity.class);
                intent.putExtra("jumpType", 1);//1编辑 2查看详情
                intent.putExtra("id", pid);
                intent.putExtra("navName", "编辑");
                intent.putExtra("detail", list.get(position));
                startActivity(intent);
            }

            @Override
            public void onLookDetailClick(String id, int position) {
                Intent intent = new Intent(ZdjgdwJdxxActivity.this, AddZdjgdwJdxxActivity.class);
                intent.putExtra("jumpType", 2);//1编辑 2查看详情
                intent.putExtra("id", pid);
                intent.putExtra("navName", "详情");
                intent.putExtra("detail", list.get(position));
                startActivity(intent);
            }
        });

        binding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ZdjgdwJdxxActivity.this, AddZdjgdwJdxxActivity.class);
                intent.putExtra("jumpType", 0);
                intent.putExtra("id", pid);
                intent.putExtra("navName", "添加监督信息");
                startActivity(intent);
            }
        });

        //时间选择
        binding.startTimeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePicker picker = new DatePicker(ZdjgdwJdxxActivity.this);
                picker.getWheelLayout().setResetWhenLinkage(false);
                picker.setOnDatePickedListener(new OnDatePickedListener() {
                    @Override
                    public void onDatePicked(int year, int month, int day) {
                        String time = MyUtil.numFormat(year) + "-" + MyUtil.numFormat(month) + "-" + MyUtil.numFormat(day);
                        String eTime = binding.endTime.getText().toString();
                        if (!TextUtils.isEmpty(eTime)) {
                            Date sdate = MyUtil.parseServerTime(time, "yyyy-MM-dd");
                            Date edate = MyUtil.parseServerTime(eTime, "yyyy-MM-dd");
                            if (sdate.getTime() > edate.getTime()) {
                                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("开始时间不能大于结束时间");
                            } else {
                                binding.startTime.setText(time);
                            }
                        } else {
                            binding.startTime.setText(time);
                        }
                    }
                });
                picker.show();
            }
        });
        binding.endTimeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePicker picker = new DatePicker(ZdjgdwJdxxActivity.this);
                picker.getWheelLayout().setResetWhenLinkage(false);
                picker.setOnDatePickedListener(new OnDatePickedListener() {
                    @Override
                    public void onDatePicked(int year, int month, int day) {
                        String time = MyUtil.numFormat(year) + "-" + MyUtil.numFormat(month) + "-" + MyUtil.numFormat(day);
                        String sTime = binding.startTime.getText().toString();
                        if (!TextUtils.isEmpty(sTime)) {
                            Date sdate = MyUtil.parseServerTime(sTime, "yyyy-MM-dd");
                            Date edate = MyUtil.parseServerTime(time, "yyyy-MM-dd");
                            if (sdate.getTime() > edate.getTime()) {
                                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("结束时间不能小于开始时间");
                            } else {
                                binding.endTime.setText(time);
                            }
                        } else {
                            binding.endTime.setText(time);
                        }


                    }
                });
                picker.show();
            }
        });

        binding.startTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (TextUtils.isEmpty(charSequence)) {
                    binding.delStart.setVisibility(View.GONE);
                } else {
                    binding.delStart.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.endTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (TextUtils.isEmpty(charSequence)) {
                    binding.delEnd.setVisibility(View.GONE);
                } else {
                    binding.delEnd.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.delStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.startTime.setText("");
            }
        });
        binding.delEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.endTime.setText("");
            }
        });
        binding.search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startTime = binding.startTime.getText().toString();
                endTime = binding.endTime.getText().toString();
                if (TextUtils.isEmpty(startTime) && TextUtils.isEmpty(endTime)) {
                    isShowDialog = true;
                    isRefresh = true;
                    pageNo = 1;
                    loadList();
                    return;
                }
                if (TextUtils.isEmpty(startTime)) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("请选择开始时间");
                } else if (TextUtils.isEmpty(endTime)) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("请选择结束时间");
                } else {
                    isShowDialog = true;
                    isRefresh = true;
                    pageNo = 1;
                    loadList();
                }
            }
        });
    }

    //列表
    private void loadList() {
        ISubscriberOnNextListener<HttpResult<ZdjgdwJdxxBean>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<ZdjgdwJdxxBean>>() {
            @Override
            public void onNext(HttpResult<ZdjgdwJdxxBean> httpResult) {
                if (httpResult.getCode() == 200) {
                    total = httpResult.getResult().getPages();
                    if (isRefresh) {
                        list.clear();
                    }
                    list.addAll(httpResult.getResult().getRecords());
                    for (int i = 0; i < list.size(); i++) {
                        if (!TextUtils.isEmpty(list.get(i).getJd())) {
                            list.get(i).setJd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getJd())));
                        }
                        if (!TextUtils.isEmpty(list.get(i).getWd())) {
                            list.get(i).setWd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getWd())));
                        }
                    }
                    adapter.setData(list);
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<ZdjgdwJdxxBean>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, ZdjgdwJdxxActivity.this, isShowDialog);
        RxApiManager.get().add("zdjgdwJdxxList", httpResultProgressSubscriber);
        HttpMethods.getInstance(ZdjgdwJdxxActivity.this).zdjgdwJdxxList(httpResultProgressSubscriber, pageNo, Constant.PAGE_SIZE, pid, startTime, endTime);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("zdjgdwJdxxList");
    }
}