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

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
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.greendao.model.WmsInventoryTaskDetail;

import java.util.List;

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

    public ReviewOfflineDetailAdapter(int layoutResId, List<WmsInventoryTaskDetail> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, WmsInventoryTaskDetail dataBean) {
        //批次号
        baseViewHolder.setText(R.id.tv_batch_no, !StringUtils.isEmpty(dataBean.getBatchNo()) ? dataBean.getBatchNo() : "-");
        //RFID标签
        baseViewHolder.setText(R.id.tv_rfid_tag, !StringUtils.isEmpty(dataBean.getRfidTag()) ? dataBean.getRfidTag() : "-");
        //仓库名称
        baseViewHolder.setText(R.id.tv_warehouse_name, !StringUtils.isEmpty(dataBean.getWarehouseName()) ? dataBean.getWarehouseName() : "-");
        //区域名称
        baseViewHolder.setText(R.id.tv_area_name, !StringUtils.isEmpty(dataBean.getAreaName()) ? dataBean.getAreaName() : "-");
        //位置名称
        baseViewHolder.setText(R.id.tv_location_name, !StringUtils.isEmpty(dataBean.getLocationName()) ? dataBean.getLocationName() : "-");
        //系统数量
        baseViewHolder.setText(R.id.tv_system_quantity, !StringUtils.isEmpty(dataBean.getSystemQty() + "") ? dataBean.getSystemQty() + "" : "-");
//        //实际数量
        baseViewHolder.setText(R.id.tv_actual_quantity, !StringUtils.isEmpty(dataBean.getActualQty() + "") ? dataBean.getActualQty() + "" : "-");
        //检查人
        baseViewHolder.setText(R.id.tv_checker_name, !StringUtils.isEmpty(dataBean.getCheckerName()) ? dataBean.getCheckerName() : "-");
        //检查时间
        baseViewHolder.setText(R.id.tv_check_time, !StringUtils.isEmpty(dataBean.getCheckTime()) ? dataBean.getCheckTime() : "-");
        //差异数量
        baseViewHolder.setText(R.id.tv_difference_quantity, !StringUtils.isEmpty(dataBean.getDiffReason() + "") ? dataBean.getDiffReason() : "-");
        //差异率
        baseViewHolder.setText(R.id.tv_difference_rate, !StringUtils.isEmpty(dataBean.getDiffQty() + "") ? dataBean.getDiffQty() + "" : "-");
        //备注
        baseViewHolder.setText(R.id.tv_remark, !StringUtils.isEmpty(dataBean.getRemark()) ? dataBean.getRemark() : "-");
        //拣货状态
        TextView checkStatus = baseViewHolder.getView(R.id.tv_check_status);
        switch (dataBean.getCheckStatus()) {
            case "0":
                checkStatus.setText("待盘点");
                checkStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_org_bg));
                checkStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.text_orange3));
                break;
            case "1":
                checkStatus.setText("已盘点");
                checkStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_green_bg));
                checkStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.green));
                break;
            case "2":
                checkStatus.setText("差异待确认");
                checkStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_red_bg));
                checkStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
                break;
            default:
                checkStatus.setText("-");
                checkStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                checkStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                break;
        }
        //差异原因
        TextView tvDifferenceReason = baseViewHolder.getView(R.id.tv_difference_reason);
        if (!StringUtils.isEmpty(dataBean.getDiffReason())) {
            switch (dataBean.getDiffReason()) {
                case "py":
                    tvDifferenceReason.setText("盘盈");
                    break;
                case "pk":
                    tvDifferenceReason.setText("盘亏");
                    break;
                case "wcy":
                    tvDifferenceReason.setText("无差异");
                    break;
                default:
                    tvDifferenceReason.setText("-");
                    break;
            }
        } else {
            tvDifferenceReason.setText("-");
        }

        LinearLayout ll1 = baseViewHolder.getView(R.id.ll1);
        LinearLayout ll2 = baseViewHolder.getView(R.id.ll2);
        TextView expandButton = baseViewHolder.getView(R.id.expand_button);
        ImageView ivUp = baseViewHolder.getView(R.id.iv_up);
        //展开收起按钮
        baseViewHolder.getView(R.id.rl_up).setOnClickListener(v -> {
            // 处理展开收起点击事件 切换展开收起状态
            boolean isExpanded = dataBean.isExpanded();
            if (!isExpanded) {
                // 收起
                ivUp.setRotation(180);
                expandButton.setText("收起");
                ll1.setVisibility(View.VISIBLE);
                ll2.setVisibility(View.VISIBLE);
            } else {
                // 展开
                ivUp.setRotation(0);
                expandButton.setText("展开");
                ll1.setVisibility(View.GONE);
                ll2.setVisibility(View.GONE);
            }
            dataBean.setExpanded(!isExpanded);
        });

        //复制1按钮
        baseViewHolder.getView(R.id.tv_batch_no).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onCopy1ItemClick(baseViewHolder.getAdapterPosition(), dataBean);
            }
        });
        //复制2按钮
        baseViewHolder.getView(R.id.tv_rfid_tag).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onCopy2ItemClick(baseViewHolder.getAdapterPosition(), dataBean);
            }
        });
    }

    public interface OnItemClickListener {
        void onCopy1ItemClick(int position, WmsInventoryTaskDetail item);

        void onCopy2ItemClick(int position, WmsInventoryTaskDetail item);
    }

    private OnItemClickListener mItemClickListener;

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