package com.unis.sxzcl.ui.todo;

import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.model.BasicModel;
import com.unis.model.bean.aeration.AerationBean;
import com.unis.model.bean.todo.TodoListType;
import com.unis.sxzcl.R;
import com.xyxsbj.mylibrary.tool.DateTool;

import org.jetbrains.annotations.NotNull;

/**
 * 包名: com.unis.sxzcl.ui.todo
 * 类名: TodoListAdapter
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/24 15:42
 * 更新人:
 * 更新时间: 2020/11/24 15:42
 * 更新说明:
 * 版本号: 1.0
 */
public class TodoListAdapter extends BaseQuickAdapter<AerationBean, BaseViewHolder> implements LoadMoreModule {

    private int listType;
    private NextCallback mNextCallback;

    public TodoListAdapter(@TodoListType int listType, TodoListAdapter.NextCallback nextCallback) {
        super(R.layout.item_list_todo);
        this.listType = listType;
        mNextCallback = nextCallback;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, AerationBean item) {
        // 隐藏状态
        holder.setGone(R.id.tv_status, true);

        // 业务类型
        holder.setText(R.id.tv_title, BasicModel.get().getWorkTypeName(item.getType()));
        // 业务内容
        // 仓号: 1号仓\n申请人: 王全德\n申请部门: 仓储部\n公司: xxx公司
        StringBuilder sb = new StringBuilder();
        sb.append("仓号: ")
                .append(BasicModel.get().getHouseName(item.getHouseId()))
                .append("\n")
                .append("申请人: ")
                .append(BasicModel.get().getPersonName(item.getApplyPeople()))
                .append("\n")
                .append("申请部门: ")
                .append("")
                .append("\n")
                .append("公司: ")
                .append(BasicModel.get().getDepotName(item.getDepotId()))
                .append("\n")
        ;
        holder.setText(R.id.tv_content, sb.toString());
        holder.setText(R.id.tv_time, item.getApplyTime());
        holder.setText(R.id.tv_next, "去审批");

        TextView tvWarnTime = holder.getView(R.id.tv_warn_time);
        TextView tvTimeout = holder.getView(R.id.tv_timeout);
        TextView tvNext = holder.getView(R.id.tv_next);

        tvWarnTime.setVisibility(View.GONE);
        tvTimeout.setVisibility(View.GONE);
//        setTime(tvWarnTime, tvTimeout, item.time, item.workTime);

        holder.itemView.setTag(item);
        holder.itemView.setOnClickListener(v -> {
            if (mNextCallback != null)
                mNextCallback.onNextCallback((AerationBean) v.getTag());
        });
    }

    private void setTime(TextView tvWarnTime, TextView tvTimeout, String planTime, String workTime) {
        // 时间
        if (planTime == null) {
            // 没有时间 隐藏 倒计时和超时
            tvWarnTime.setVisibility(View.GONE);
            tvTimeout.setVisibility(View.GONE);
        } else {
            long planDate = DateTool.getStamp(planTime, DateTool.YTDTD_DATE);
            // 没有完成时间 计算倒计时和或超时时间
            if (workTime == null) {
                long currentDate = DateTool.getCurrenTimeStamp();
                //当前时间 大于 计划时间
                if (currentDate > planDate) {
                    // 显示逾期
                    tvTimeout.setVisibility(View.VISIBLE);

                    tvTimeout.setText("逾期 " + DateTool.getTimeDiffer(currentDate - planDate));
                    // 隐藏倒计时
                    tvWarnTime.setVisibility(View.GONE);
                } else {
                    // 显示倒计时
                    tvWarnTime.setVisibility(View.VISIBLE);
                    tvWarnTime.setText("倒计时 " + DateTool.getTimeDiffer(planDate - currentDate));
                    // 隐藏逾期
                    tvTimeout.setVisibility(View.GONE);
                }
            } else {
                long workDate = DateTool.getStamp(workTime, DateTool.YTDTD_DATE);
                if (workDate > planDate) {
                    //超时
                    // 显示逾期
                    tvTimeout.setVisibility(View.VISIBLE);
                    tvTimeout.setText("逾期 " + DateTool.getTimeDiffer(workDate - planDate));
                    // 隐藏倒计时
                    tvWarnTime.setVisibility(View.GONE);
                } else {
                    //未超时 隐藏时间
                    tvWarnTime.setVisibility(View.GONE);
                    tvTimeout.setVisibility(View.GONE);
                }
            }

        }
    }

    public interface NextCallback {
        void onNextCallback(AerationBean item);
    }
}
