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

import android.animation.Animator;
import android.animation.ObjectAnimator;
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.listener.OnLoadMoreListener;
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.utils.ToastUtil;

import java.util.List;

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

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

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, OutboundModel.RowsBean dataBean) {
        //出库单号
        baseViewHolder.setText(R.id.outbound_number, !StringUtils.isEmpty(dataBean.getOrderNo()) ? dataBean.getOrderNo() : "-");
        //仓库名称
        baseViewHolder.setText(R.id.txt_warehouse, !StringUtils.isEmpty(dataBean.getWarehouse().getWarehouseName()) ? dataBean.getWarehouse().getWarehouseName() : "-");
        //客户
        baseViewHolder.setText(R.id.txt_customer_type, !StringUtils.isEmpty(dataBean.getCustomerUser().getCustomerName()) ? dataBean.getCustomerUser().getCustomerName() : "-");
        //数量/金额
        baseViewHolder.setText(R.id.txt_number_money, dataBean.getTotalQuantity() + "个 / ￥" + dataBean.getTotalAmount() + "元");
        //出库类型
        switch (dataBean.getOrderType()) {
            case "0":
                baseViewHolder.setText(R.id.txt_outbound_type, "计划出库");
                break;
            case "1":
                baseViewHolder.setText(R.id.txt_outbound_type, "紧急调拨");
                break;
            case "2":
                baseViewHolder.setText(R.id.txt_outbound_type, "报废出库");
                break;
            default:
                baseViewHolder.setText(R.id.txt_outbound_type, "-");
                break;
        }
        TextView outboundButton = baseViewHolder.getView(R.id.outbound_button);
        //状态
        TextView outboundStatus = baseViewHolder.getView(R.id.outbound_status);
        switch (dataBean.getOrderStatus()) {
            case "0":
                outboundStatus.setText("未出库");
                outboundStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                outboundStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                outboundButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.green_bg));
                outboundButton.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
            case "1":
                outboundStatus.setText("拣货中");
                outboundStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_red_bg));
                outboundStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
                outboundButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.green_bg));
                outboundButton.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                break;
            case "2":
                outboundStatus.setText("已出库");
                outboundStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_green_bg));
                outboundStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.green));
                outboundButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                outboundButton.setTextColor(ContextCompat.getColor(getContext(), R.color.c_c1c1));
                break;
            default:
                outboundStatus.setText("-");
                outboundStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                outboundStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                outboundButton.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                outboundButton.setTextColor(ContextCompat.getColor(getContext(), R.color.gray2));
                break;
        }
        //复制按钮
        baseViewHolder.getView(R.id.btn_copy).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);
            }
        });
        //出库按钮
        baseViewHolder.getView(R.id.outbound_button).setOnClickListener(v -> {
            switch (dataBean.getOrderStatus()) {
                case "0":
                case "1":
                    if (mItemClickListener != null) {
                        mItemClickListener.onOutboundClick(baseViewHolder.getLayoutPosition(), dataBean);
                    }
                    break;
                case "2":
                    ToastUtil.showShort("已出库，无需再进行出库");
                    break;
                default:
                    ToastUtil.showShort("状态异常，无法进行出库操作");
                    break;
            }

        });
    }

    public interface OnItemClickListener {
        void onItemClick(int position, OutboundModel.RowsBean item);

        void onOutboundClick(int position, OutboundModel.RowsBean item);

        void onCopyClick(int position, OutboundModel.RowsBean item);
    }

    private OnItemClickListener mItemClickListener;

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