package com.sxyz.rfid.ui.review.adapter;

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

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.StringUtils;
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.sxyz.rfid.R;
import com.sxyz.rfid.presenter.model.OutboundModel;
import com.sxyz.rfid.presenter.model.ReviewModel;
import com.sxyz.rfid.utils.ToastUtil;

import java.util.List;

/**
 * 盘点适配器
 *
 * @author chunyuanwu
 * @version 1.0.0
 * @date 2025年12月27日 16:40
 */
public class ReviewAdapter extends BaseQuickAdapter<ReviewModel.RowsBean, BaseViewHolder> implements LoadMoreModule {

    public ReviewAdapter(int layoutResId, List<ReviewModel.RowsBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, ReviewModel.RowsBean dataBean) {
        //出库单号
        baseViewHolder.setText(R.id.tv_task_no, !StringUtils.isEmpty(dataBean.getTaskNo()) ? dataBean.getTaskNo() : "-");
        //任务名称
        baseViewHolder.setText(R.id.tv_task_name, !StringUtils.isEmpty(dataBean.getTaskName()) ? dataBean.getTaskName() : "-");
        //仓库名称
        baseViewHolder.setText(R.id.txt_warehouse_name, !StringUtils.isEmpty(dataBean.getWarehouseName()) ? dataBean.getWarehouseName() : "-");
        //总盘点数
        baseViewHolder.setText(R.id.txt_total_number, dataBean.getTotalItems() + "");
        //已盘数
        baseViewHolder.setText(R.id.txt_review_number, dataBean.getCountedItems() + "");
        TextView reviewButton = baseViewHolder.getView(R.id.review_button);
        TextView stopButton = baseViewHolder.getView(R.id.stop_button);
        //任务状态
        TextView taskStatus = baseViewHolder.getView(R.id.tv_task_status);
        switch (dataBean.getTaskStatus()) {
            case "0":
                taskStatus.setText("未完成");
                taskStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_red_bg));
                taskStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
                reviewButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.green_bg));
                reviewButton.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                stopButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                stopButton.setTextColor(ContextCompat.getColor(getContext(), R.color.c_c1c1));
                break;
            case "1":
                taskStatus.setText("进行中");
                taskStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_org_bg));
                taskStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.text_orange3));
                reviewButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.green_bg));
                reviewButton.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                stopButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.red_bg4));
                stopButton.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
            case "2":
                taskStatus.setText("已完成");
                taskStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_green_bg));
                taskStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.green));
                reviewButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                reviewButton.setTextColor(ContextCompat.getColor(getContext(), R.color.c_c1c1));
                stopButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                stopButton.setTextColor(ContextCompat.getColor(getContext(), R.color.c_c1c1));
                break;
//            case "3":
//                taskStatus.setText("已结束");
//                taskStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
//                taskStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
//                reviewButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
//                reviewButton.setTextColor(ContextCompat.getColor(getContext(), R.color.c_c1c1));
//                stopButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
//                stopButton.setTextColor(ContextCompat.getColor(getContext(), R.color.c_c1c1));
//                break;
            default:
                taskStatus.setText("-");
                taskStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                taskStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                reviewButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                reviewButton.setTextColor(ContextCompat.getColor(getContext(), R.color.c_c1c1));
                stopButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                stopButton.setTextColor(ContextCompat.getColor(getContext(), R.color.c_c1c1));
                break;
        }

        //复制按钮
        baseViewHolder.getView(R.id.tv_task_no).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onCopyClick(baseViewHolder.getLayoutPosition(), dataBean);
            }
        });
        //详情按钮
        baseViewHolder.getView(R.id.detail_button).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(baseViewHolder.getLayoutPosition(), dataBean);
            }
        });
        //执行盘点
        reviewButton.setOnClickListener(v -> {
            switch (dataBean.getTaskStatus()) {
                case "0":
                case "1":
                    if (mItemClickListener != null) {
                        mItemClickListener.onReviewTaskClick(baseViewHolder.getLayoutPosition(), dataBean);
                    }
                    break;
                case "2":
                    ToastUtil.showShort("盘点已完成，无需执行盘点");
                    break;
//                case "3":
//                    ToastUtil.showShort("盘点已结束，无需执行盘点");
//                    break;
                default:
                    ToastUtil.showShort("未知状态，无法执行盘点");
                    break;
            }
        });

        //删除按钮
        baseViewHolder.getView(R.id.delete_button).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onDeleteClick(baseViewHolder.getLayoutPosition(), dataBean);
            }
        });
        //结束盘点
        stopButton.setOnClickListener(v -> {
            switch (dataBean.getTaskStatus()) {
                case "0"://未开始
                    ToastUtil.showShort("盘点未开始，不能结束盘点");
                    break;
                case "1"://已开始
                    if (mItemClickListener != null) {
                        mItemClickListener.onStopTaskClick(baseViewHolder.getLayoutPosition(), dataBean);
                    }
                    break;
                case "2"://已完成
                    ToastUtil.showShort("盘点已完成，不能结束盘点");
                    break;
//                case "3":
//                    ToastUtil.showShort("盘点已结束，不能结束盘点");
//                    break;
                default:
                    ToastUtil.showShort("未知状态，无法结束盘点");
                    break;
            }
        });
    }

    public interface OnItemClickListener {
        //删除
        void onDeleteClick(int position, ReviewModel.RowsBean item);

        //详情
        void onItemClick(int position, ReviewModel.RowsBean item);

        //复制按钮
        void onCopyClick(int position, ReviewModel.RowsBean item);

        //执行盘点
        void onReviewTaskClick(int position, ReviewModel.RowsBean item);

        //结束盘点
        void onStopTaskClick(int position, ReviewModel.RowsBean item);
    }

    private OnItemClickListener mItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }
}