package com.sxyz.rfid.ui.out.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.presenter.model.OutBoundDetailModel;
import com.sxyz.rfid.presenter.model.OutboundFlowingWaterModel;
import com.sxyz.rfid.utils.TimeUtils;

import java.util.List;

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

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

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, OutboundFlowingWaterModel.RowsBean dataBean) {
        //RFID 标签
        baseViewHolder.setText(R.id.tv_material_code, !StringUtils.isEmpty(dataBean.getRfidCode()) ? dataBean.getRfidCode() : "-");
        //批   次   号
        baseViewHolder.setText(R.id.tv_batch_code, !StringUtils.isEmpty(dataBean.getBatchNo()) ? dataBean.getBatchNo() : "-");
        //仓库
        baseViewHolder.setText(R.id.tv_warehouse, !StringUtils.isEmpty(dataBean.getWarehouse().getWarehouseName()) ? dataBean.getWarehouse().getWarehouseName() : "-");
        //仓库区域
        baseViewHolder.setText(R.id.tv_warehouse_area, !StringUtils.isEmpty(dataBean.getArea().getAreaName()) ? dataBean.getArea().getAreaName() : "-");
        //货架
        baseViewHolder.setText(R.id.tv_shelf, !StringUtils.isEmpty(dataBean.getLocation().getLocationName()) ? dataBean.getLocation().getLocationName() : "-");
        //数量
        baseViewHolder.setText(R.id.tv_quantity, !StringUtils.isEmpty(dataBean.getQuantity() + "") ? dataBean.getQuantity() + "" : "-");
        //单价
        baseViewHolder.setText(R.id.tv_in_price, !StringUtils.isEmpty(dataBean.getUnitCost() + "") ? dataBean.getUnitCost() + "" : "-");
        if (!StringUtils.isEmpty(dataBean.getOutboundTime())) {
            String outboundTime = TimeUtils.toChineseFormat(dataBean.getOutboundTime(), "yyyy-MM-dd");
            //出库时间
            baseViewHolder.setText(R.id.tv_outbound_time, outboundTime);
        } else {
            //出库时间
            baseViewHolder.setText(R.id.tv_outbound_time, "-");
        }
        if (!StringUtils.isEmpty(dataBean.getCancelTime())) {
            String cancelTime = TimeUtils.toChineseFormat(dataBean.getCancelTime(), "yyyy-MM-dd'T'HH:mm:ss");
            cancelTime = cancelTime.replace("T", " ");
            //撤销时间
            baseViewHolder.setText(R.id.tv_cancel_time, cancelTime);
        } else {
            //撤销时间
            baseViewHolder.setText(R.id.tv_cancel_time, "-");
        }
        //拣货状态
        TextView status = baseViewHolder.getView(R.id.tv_status);
        switch (dataBean.getIsCanceled()) {
            case "0":
                status.setText("未撤销");
                status.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                status.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                break;
            case "1":
                status.setText("已撤销");
                status.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_red_bg));
                status.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
                break;
            default:
                status.setText("-");
                status.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                status.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                break;
        }
        if (dataBean.getIsCanceled().equals("1")) {
            //已撤销  隐藏撤销按钮
            baseViewHolder.getView(R.id.btn_cancel).setVisibility(View.GONE);
        } else {
            //未撤销  显示撤销按钮
            baseViewHolder.getView(R.id.btn_cancel).setVisibility(View.VISIBLE);
        }
        //撤销按钮
        baseViewHolder.getView(R.id.btn_cancel).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(baseViewHolder.getLayoutPosition(), dataBean);
            }
        });
        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);
        });
    }

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

    private OnItemClickListener mItemClickListener;

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