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.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, item.getWorkName());

        TextView tvWarnTime = holder.getView(R.id.tv_warn_time);
        TextView tvTimeout = holder.getView(R.id.tv_timeout);
        if (item.getType().equals("tf")) {
            // 机械通风
            holder.setText(R.id.tv_next, "去审批");
            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());
            holder.setText(R.id.tv_time, item.getApplyTime());

            // 机械通风 隐藏倒计时和逾期
            tvWarnTime.setVisibility(View.GONE);
            tvTimeout.setVisibility(View.GONE);

        } else {
            // 其他作业类型
            holder.setText(R.id.tv_next, "去处理");

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

            holder.setText(R.id.tv_time, item.getStartDate());

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

        TextView tvNext = holder.getView(R.id.tv_next);

        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);
    }
}
