package com.bbitcn.silk.ui.fragment;


import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.bbitcn.silk.adapter.ItemInfoAdapter;
import com.bbitcn.silk.base.BaseFragment;
import com.bbitcn.silk.databinding.FragmentQueryBinding;
import com.bbitcn.silk.databinding.ItemInfoBinding;
import com.bbitcn.silk.ui.dialog.TipsDialog;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.TimeUtils;

import java.util.Calendar;

/**
 * 只显示列表
 */
public class QueryFragment extends BaseFragment<FragmentQueryBinding, QueryFragmentPresenter> {

    private int queryType;
    private int clickType;
    private int type;
    /**
     * 当前所在页数
     */
    private int page = 1;
    ItemInfoAdapter adapter;

    /**
     * @param queryType 0单显示 1显示日期 2显示关键字 3均显示
     * @param clickType 0无点击事件， 1去扣皮，2去转换 3弃售/确认售
     * @param paramType 类型 磅单查询：1:待定价/扣皮 2:待确认售 3:全部榜单 4:待定价 5：待扣皮
     */
    public QueryFragment(int queryType, int clickType, int paramType) {
        this.queryType = queryType;
        this.clickType = clickType;
        this.type = paramType;
    }

    @Override
    public void init() {
        switch (queryType) {
            case 0 -> {
                binding.llDate.setVisibility(View.GONE);
                binding.llKey.setVisibility(View.GONE);
            }
            case 1 -> binding.llKey.setVisibility(View.GONE);
            case 2 -> binding.llDate.setVisibility(View.GONE);
        }
        adapter = new ItemInfoAdapter(mContext, null, ItemInfoBinding.class, clickType);
        binding.recycleView.setAdapter(adapter);
        binding.recycleView.setLayoutManager(new LinearLayoutManager(mContext));
        presenter.getAddListLiveData().observe(this, queryList -> {
            if (queryList != null) {
                page++;
                adapter.addDataList(queryList.getData());
                if (queryList.getData().size() == 0) {
                    binding.refreshLayout.finishLoadMoreWithNoMoreData();
                } else {
                    binding.refreshLayout.finishLoadMore();
                }
            }
        });
        presenter.getSetListLiveData().observe(this, queryList -> {
            if (queryList != null) {
                adapter.setDataList(queryList.getData());
                //回到顶部
                binding.recycleView.scrollToPosition(0);
            }
            binding.refreshLayout.finishRefresh();
        });
        //设置默认开始时间为当前时间的一年前
        binding.tvStartDate.setText(TimeUtils.date2String(TimeUtils.getDateByNow(-31, TimeConstants.DAY), "yyyy-MM-dd"));
        binding.tvEndDate.setText(TimeUtils.date2String(TimeUtils.getNowDate(), "yyyy-MM-dd"));
        presenter.query(type, binding.tvStartDate.getText().toString(), binding.tvEndDate.getText().toString(), binding.etKey.getText().toString(), 1, 5);
    }

    @Override
    public void initListener() {
        super.initListener();
        //选择开始日期
        binding.tvStartDate.setOnClickListener(getDateListener(true));
        binding.tvEndDate.setOnClickListener(getDateListener(false));
        binding.btnQuery.setOnClickListener(v ->
                presenter.query(type, binding.tvStartDate.getText().toString(), binding.tvEndDate.getText().toString(), binding.etKey.getText().toString(), 1, 5));
        binding.refreshLayout.setOnRefreshListener(refreshlayout -> {
            page = 1;
            presenter.query(type, binding.tvStartDate.getText().toString(), binding.tvEndDate.getText().toString(), binding.etKey.getText().toString(), 1, 5);
        });
        binding.refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            presenter.query(type, binding.tvStartDate.getText().toString(), binding.tvEndDate.getText().toString(), binding.etKey.getText().toString(), ++page, 5);
        });
    }

    private View.OnClickListener getDateListener(boolean isStart) {
        return v -> {
            //隐藏系统软键盘
            KeyboardUtils.hideSoftInput(binding.etKey);
            Calendar defaultDate = Calendar.getInstance();
            if (isStart) {
                defaultDate.setTime(TimeUtils.string2Date(binding.tvStartDate.getText().toString(), "yyyy-MM-dd"));
            } else {
                defaultDate.setTime(TimeUtils.string2Date(binding.tvEndDate.getText().toString(), "yyyy-MM-dd"));
            }
            new TimePickerBuilder(mContext, (date, v1) -> {
                if (isStart) {
                    binding.tvStartDate.setText(TimeUtils.date2String(date, "yyyy-MM-dd"));
                } else {
                    binding.tvEndDate.setText(TimeUtils.date2String(date, "yyyy-MM-dd"));
                }
                //判断时间区间是否合理
                if (TimeUtils.string2Date(binding.tvStartDate.getText().toString(), "yyyy-MM-dd").getTime()
                        > TimeUtils.string2Date(binding.tvEndDate.getText().toString(), "yyyy-MM-dd").getTime()) {
                    new TipsDialog(mContext, "开始时间不能大于结束时间").show();
                    return;
                }
                presenter.query(type, binding.tvStartDate.getText().toString(), binding.tvEndDate.getText().toString(), binding.etKey.getText().toString(), 1, 5);
            })
                    .setDate(defaultDate)
                    .setContentTextSize(35)
                    .setTitleSize(35)
                    .isCyclic(false)
                    .setSubCalSize(20)
                    .setType(new boolean[]{true, true, true, false, false, false})
                    .build()
                    .show();
        };
    }
}