package com.unis.sxzcl.ui.allround;

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

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.model.BasicModel;
import com.unis.model.UserModel;
import com.unis.model.bean.detail.DetailApproveBean;
import com.unis.sxzcl.R;
import com.unis.sxzcl.widget.dialog.SystemDialog;

import org.jetbrains.annotations.NotNull;

/**
 * 审批适配器
 * 创建人: mQ
 * 创建时间: 2021/1/26 18:22
 */
public class ProcessApproveAdapter extends BaseQuickAdapter<DetailApproveBean, BaseViewHolder> {

    private String mPageType;

    public ProcessApproveAdapter() {
        super(R.layout.item_detail_approve_item);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, DetailApproveBean item) {
        int position = holder.getLayoutPosition();

        TextView line = holder.getView(R.id.line);
        TextView vLine1 = holder.getView(R.id.v_line1);
        TextView vLine2 = holder.getView(R.id.v_line2);
        // 第一条不显示顶部线
        if (position == 0) vLine1.setVisibility(View.GONE);
        else vLine1.setVisibility(View.VISIBLE);
        // 最后一条不显示底部线
        if (position == getItemCount() - 1) {
            vLine2.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        } else {
            vLine2.setVisibility(View.VISIBLE);
            line.setVisibility(View.VISIBLE);
        }

        // 审批标题
        holder.setText(R.id.tv_title, item.getTitle());

        // 审批人
        String content = item.getContent();
        if (content == null) {
            holder.setText(R.id.tv_person, "");
        } else if (content.equals("0")) {
            // 保管员
            holder.setText(R.id.tv_person, UserModel.get().getUser().getPersonnelName());
        } else {
            // 审批人id取值
            holder.setText(R.id.tv_person, BasicModel.get().getPersonName(item.getContent()));
        }

        // 审批时间
        holder.setText(R.id.tv_time, item.getAuditTime());
        // 审批状态更改颜色
        String status = item.getAuditState();
        TextView tvStatus = holder.getView(R.id.tv_status);
        if (position == 0) {
            // 保管员
            tvStatus.setText("");
            if (mPageType.equals("add")) {
                // 新增时灰色
                setGaryColor(holder);
            } else {
                // 其他状态高亮
                setThemeColor(holder);
            }
        } else {
            if (status == null) {
                tvStatus.setText("");
                setGaryColor(holder);
            } else {
                // 审批人时  判断状态
                switch (status) {
                    case "0":
                        tvStatus.setText("待审批");
                        setGaryColor(holder);
                        break;
                    case "1":
                        tvStatus.setText("通过");
                        setThemeColor(holder);
                        break;
                    case "2":
                        tvStatus.setText("不通过");
                        setRedColor(holder);
                        break;
                }
            }
        }


        if (mPageType.equals("approve")) {
            LinearLayout layoutOption = holder.getView(R.id.layout_option);
            TextView tvNo = holder.getView(R.id.tv_no);
            TextView tvPass = holder.getView(R.id.tv_pass);
            // 是否显示操作按钮
            // 当前登录人 且 状态为待审批 显示审批按钮
            String uid = UserModel.get().getUser().getUid().toString();
            if (uid.equals(content) && (status != null && status.equals("0"))) {
                layoutOption.setVisibility(View.VISIBLE);
            } else {
                layoutOption.setVisibility(View.GONE);
            }
            tvNo.setTag(item);
            tvNo.setOnClickListener(v -> {
                new SystemDialog(getContext())
                        .setHideTitle().setHideClose()
                        .setMsg("确定审批不通过?")
                        .setLeftButton("取消", null)
                        .setRightButton("确定", dialog -> {
                            dialog.dismiss();
                            if (mAuditCallback != null) {
                                DetailApproveBean bean = (DetailApproveBean) v.getTag();
                                int index = getItemPosition(bean);
                                mAuditCallback.onAuditListener(bean, false, index, index == getItemCount() - 1);
                            }
                        })
                        .show();
            });
            tvPass.setTag(item);
            tvPass.setOnClickListener(v -> {
                new SystemDialog(getContext())
                        .setHideTitle().setHideClose()
                        .setMsg("确定审批通过?")
                        .setLeftButton("取消", null)
                        .setRightButton("确定", dialog -> {
                            dialog.dismiss();
                            if (mAuditCallback != null) {
                                DetailApproveBean bean = (DetailApproveBean) v.getTag();
                                int index = getItemPosition(bean);
                                mAuditCallback.onAuditListener(bean, true, index, index == getItemCount() - 1);
                            }
                        })
                        .show();
            });
        }
    }

    // 设置灰色
    private void setGaryColor(@NotNull BaseViewHolder holder) {
        holder.setTextColorRes(R.id.tv_person, R.color.colorText88);
        holder.setImageResource(R.id.ic_approve, R.drawable.ic_want_approve);
        holder.setBackgroundResource(R.id.v_line1, R.color.colorText88);
        holder.setBackgroundResource(R.id.v_line2, R.color.colorText88);
    }

    // 设置高亮色
    private void setThemeColor(@NotNull BaseViewHolder holder) {
        holder.setTextColorRes(R.id.tv_person, R.color.colorTheme);
        holder.setImageResource(R.id.ic_approve, R.drawable.ic_pass_approve);
        holder.setBackgroundResource(R.id.v_line1, R.color.colorTheme);
        holder.setBackgroundResource(R.id.v_line2, R.color.colorTheme);
    }

    // 设置红色
    private void setRedColor(@NotNull BaseViewHolder holder) {
        holder.setTextColorRes(R.id.tv_person, R.color.colorRed);
        holder.setImageResource(R.id.ic_approve, R.drawable.ic_refuse_approve);
        holder.setBackgroundResource(R.id.v_line1, R.color.colorText88);
        holder.setBackgroundResource(R.id.v_line2, R.color.colorText88);
    }

    public void setPageType(String pageType) {
        mPageType = pageType;
    }

    private AuditCallback mAuditCallback;

    public void setAuditCallback(AuditCallback auditCallback) {
        mAuditCallback = auditCallback;
    }

    public interface AuditCallback {
        /**
         * 审批按钮
         *
         * @param item  审批项信息
         * @param pass  是否审批通过
         * @param index 审批项位置
         * @param isEnd 最后一项审批
         */
        void onAuditListener(DetailApproveBean item, boolean pass, int index, boolean isEnd);
    }
}
