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 java.util.List;

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

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

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, OutBoundDetailModel.RowsBean dataBean) {
        //物料编码
        baseViewHolder.setText(R.id.tv_material_code, !StringUtils.isEmpty(dataBean.getMaterial().getMaterialCode()) ? dataBean.getMaterial().getMaterialCode() : "-");
        //物料名称
        baseViewHolder.setText(R.id.tv_material_name, !StringUtils.isEmpty(dataBean.getMaterial().getMaterialName()) ? dataBean.getMaterial().getMaterialName() : "-");
        //物料规格
        baseViewHolder.setText(R.id.tv_specification, !StringUtils.isEmpty(dataBean.getMaterial().getSpecification()) ? dataBean.getMaterial().getSpecification() : "-");
        //计划数量
        baseViewHolder.setText(R.id.tv_planned_quantity, !StringUtils.isEmpty(dataBean.getPlanQuantity() + "") ? dataBean.getPlanQuantity() + "" : "-");
        //实际数量
        baseViewHolder.setText(R.id.tv_actual_quantity, !StringUtils.isEmpty(dataBean.getActualQuantity() + "") ? dataBean.getActualQuantity() + "" : "-");
        //单价
        baseViewHolder.setText(R.id.tv_price, !StringUtils.isEmpty(dataBean.getUnitPrice() + "") ? dataBean.getUnitPrice() + "" : "-");
        //计量单位
        baseViewHolder.setText(R.id.tv_unit, !StringUtils.isEmpty(dataBean.getUnit()) ? dataBean.getUnit() : "-");
        //金额
        baseViewHolder.setText(R.id.tv_amount, !StringUtils.isEmpty(dataBean.getAmount() + "") ? dataBean.getAmount() + "" : "-");
        //生产日期
//        baseViewHolder.setText(R.id.tv_production_date, !StringUtils.isEmpty(dataBean.getProductionDate()) ? dataBean.getProductionDate() : "-");
        //过期日期
//        baseViewHolder.setText(R.id.tv_expiry_date, !StringUtils.isEmpty(dataBean.getExpiryDate()) ? dataBean.getExpiryDate() : "-");
        //备注
        baseViewHolder.setText(R.id.tv_remarks, !StringUtils.isEmpty(dataBean.getRemark()) ? dataBean.getRemark() : "-");

        //拣货状态
        TextView outboundStatus = baseViewHolder.getView(R.id.tv_picking_status);
        switch (dataBean.getPickingStatus()) {
            case "0":
                outboundStatus.setText("未出库");
                outboundStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                outboundStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                break;
            case "1":
                outboundStatus.setText("拣货中");
                outboundStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_red_bg));
                outboundStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
                break;
            case "2":
                outboundStatus.setText("已出库");
                outboundStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_green_bg));
                outboundStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.green));
                break;
            default:
                outboundStatus.setText("-");
                outboundStatus.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                outboundStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                break;
        }
        //出库流水按钮
        baseViewHolder.getView(R.id.outbound_button).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(baseViewHolder.getLayoutPosition(), dataBean);
            }
        });

        //详情按钮
        baseViewHolder.getView(R.id.detail_button).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onDetailClick(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, OutBoundDetailModel.RowsBean item);

        void onDetailClick(int position, OutBoundDetailModel.RowsBean item);
    }

    private OnItemClickListener mItemClickListener;

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