package com.unis.sxzcl.ui.todo;

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

import androidx.core.content.ContextCompat;

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.bean.aeration.AerationBean;
import com.unis.model.bean.todo.TodoListType;
import com.unis.sxzcl.R;
import com.xyxsbj.mylibrary.tool.DateTool;
import com.xyxsbj.mylibrary.tool.LogTool;

import org.jetbrains.annotations.NotNull;


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

    public TodoListAdapter.NextCallback mNextCallback;
    private int listType;

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

    @Override
    protected void convert(@NotNull BaseViewHolder holder, AerationBean item) {
        // 已发起列表
        // 隐藏倒计时
        holder.setGone(R.id.tv_warn_time, true);
        // 隐藏逾期
        holder.setGone(R.id.tv_timeout, true);
        // 隐藏办理按钮
        holder.setGone(R.id.tv_next, true);

        // 业务类型
        holder.setText(R.id.tv_title, item.getWorkName());
        TextView tvWarnTime = holder.getView(R.id.tv_warn_time);
        TextView tvTimeout = holder.getView(R.id.tv_timeout);

        // 业务状态
        TextView tvStatus = holder.getView(R.id.tv_status);
        if (item.getType().equals("tf")) {
            // 机械通风
            //审批状态(0:提交,1审批通过:,2:审批不通过，3:审批完成进行中，4已完成）
            switch (item.getAuditState()) {
                case "0":
                    // 审批中
                    tvStatus.setText("审批中");
                    tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.colorText55));
                    break;
//            case "1":
//                // 审批通过
//                tvStatus.setText("审批通过");
//                tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.colorTheme));
//                break;
                case "2":
                    // 审批未通过
                    tvStatus.setText("审批不通过");
                    tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.colorRed));
                    break;
//            case "3":
//                // 进行中
//                tvStatus.setText("进行中");
//                tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.colorText55));
//                break;
//            case "4":
//                // 已完成
//                tvStatus.setText("已完成");
//                tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.colorTheme));
//                break;
                default:
                    tvStatus.setText("审批通过");
                    tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.colorTheme));
                    break;
            }
            // 机械通风 显示审批时间
            holder.setText(R.id.tv_time, item.getAuditTime());

            StringBuilder sb = new StringBuilder();
            sb.append("仓号: ")
                    .append(item.getHouseName())
                    .append("\n")
                    .append("申请人: ")
                    .append(item.getApplyName())
                    .append("\n")
                    .append("申请部门: ")
                    .append(item.getDepartmentName())
                    .append("\n")
                    .append("公司: ")
                    .append(item.getCompanyName());
            holder.setText(R.id.tv_content, sb.toString());

            // 机械通风 隐藏倒计时和逾期
            tvWarnTime.setVisibility(View.GONE);
            tvTimeout.setVisibility(View.GONE);
        } else {
            tvStatus.setText("已处理");
            tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.colorTheme));
            // 业务时间
            holder.setText(R.id.tv_time, item.getOperationTime());

            StringBuilder sb = new StringBuilder();
            sb.append("仓号: ")
                    .append(item.getHouseName())
                    .append("\n")
                    .append("公司: ")
                    .append(item.getCompanyName());
            holder.setText(R.id.tv_content, sb.toString());

            // 显示倒计时和逾期
            setTime(tvWarnTime, tvTimeout, item.getEndDate(), item.getOperationTime());
        }

        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);
            return;
        }
        long planDate = DateTool.getStamp(planTime, DateTool.YTDTD_DATE);
        LogTool.d("业务计划时间:" + planDate);
        // 没有完成时间 计算倒计时和或超时时间
        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);
            LogTool.d("业务完成时间:" + workDate);
            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 void setNextCallback(TodoListAdapter.NextCallback nextCallback) {
        mNextCallback = nextCallback;
    }

}
