package com.bbitcn.silk.ui.activity;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.bbitcn.silk.adapter.ItemContentAdapter;
import com.bbitcn.silk.base.BaseActivity;
import com.bbitcn.silk.databinding.ActivityTotalBinding;
import com.bbitcn.silk.databinding.ItemInfoContentBinding;
import com.bbitcn.silk.model.InfoContent;
import com.bbitcn.silk.model.net.Statistics;
import com.bbitcn.silk.ui.dialog.TipsDialog;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @Description 收购统计界面
 * @Author DuanKaiji
 * @CreateTime 2024年03月27日 15:12
 */
public class TotalActivity extends BaseActivity<ActivityTotalBinding, TotalActivityPresenter> {

    @Override
    public void initView() {
        binding.recycleView.setLayoutManager(new LinearLayoutManager(this));
        presenter.getQueryListLiveData().observe(this, data -> {
            if (data != null) {
                DecimalFormat df = new DecimalFormat("#.##");
                List<InfoContent> infoContents = new ArrayList<>();
                for (Statistics.DataDTO.ItemsDTO itemsDTO : data.getItems()) {
                    infoContents.add(new InfoContent(itemsDTO.getSgtypename(), itemsDTO.getJingzhong(), itemsDTO.getPrice(), df.format(itemsDTO.getMoney())));
                }// 创建 DecimalFormat 对象并指定输出格式
                binding.tvTotalWeight.setText(df.format(data.getSumjingzhong()));
                binding.recycleView.setAdapter(new ItemContentAdapter(infoContents, ItemInfoContentBinding.class, null));
                binding.tvTotalMoney.setText(df.format(data.getSummoney()));
                binding.tvTotalPrice.setText(df.format(data.getPrice()));
                binding.tvTotalWeight.setText(df.format(data.getSumjingzhong()));
            }
        });
        //设置默认开始时间
        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(binding.tvStartDate.getText().toString(), binding.tvEndDate.getText().toString());
    }

    @Override
    public void initListener() {
        super.initListener();
        binding.ivBack.setOnClickListener(v -> finish());
        //选择开始日期
        binding.tvStartDate.setOnClickListener(getDateListener(true));
        binding.tvEndDate.setOnClickListener(getDateListener(false));
    }

    /**
     * 获取日期选择监听
     */
    private View.OnClickListener getDateListener(boolean isStart) {
        return v -> {
            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(binding.tvStartDate.getText().toString(), binding.tvEndDate.getText().toString());
            })
                    .setDate(defaultDate)
                    .setContentTextSize(35)
                    .setTitleSize(35)
                    .isCyclic(false)
                    .setSubCalSize(20)
                    .setType(new boolean[]{true, true, true, false, false, false})
                    .build()
                    .show();
        };
    }

}
