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 androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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.StringUtil;

import java.util.List;

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

    public OutBoundDetailsAdapter(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() : "-") + (!StringUtils.isEmpty(dataBean.getUnit()) ? "（" + dataBean.getUnit() + "）" : "-"));
        //物料规格
        baseViewHolder.setText(R.id.tv_specification, !StringUtils.isEmpty(dataBean.getMaterial().getSpecification()) ? dataBean.getMaterial().getSpecification() : "-");
        //计划出库
        baseViewHolder.setText(R.id.tv_planned_outbound, !StringUtils.isEmpty(dataBean.getPlanQuantity() + "") ? dataBean.getPlanQuantity() + "" : "-");
        //已出库
        baseViewHolder.setText(R.id.tv_actual_outbound, !StringUtils.isEmpty(dataBean.getActualQuantity() + "") ? dataBean.getActualQuantity() + "" : "-");
        //金额
        baseViewHolder.setText(R.id.tv_amount, "¥ " + (!StringUtils.isEmpty(dataBean.getAmount() + "") ? dataBean.getAmount() + "" : "-"));
        //备注
        baseViewHolder.setText(R.id.tv_remarks, !StringUtils.isEmpty(dataBean.getRemark()) ? dataBean.getRemark() : "-");
        if (!StringUtils.isEmpty(dataBean.getRemark())) {
            baseViewHolder.getView(R.id.ll_remarks).setVisibility(View.VISIBLE);
        } else {
            baseViewHolder.getView(R.id.ll_remarks).setVisibility(View.GONE);
        }

        //拣货状态
        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;
        }

        // 子项RecyclerView
        RecyclerView recyclerView = baseViewHolder.getView(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        // 子项Adapter
        OutBoundDetailsChildAdapter childAdapter = new OutBoundDetailsChildAdapter(R.layout.item_outbound_details_child, dataBean.getDetailList());
        recyclerView.setAdapter(childAdapter);
        childAdapter.setList(dataBean.getDetailList());
        childAdapter.setOnItemClickListener((position, item) -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemCancelChildClick(baseViewHolder.getLayoutPosition(), item);
            }
        });
        ImageView ivUp = baseViewHolder.getView(R.id.zhankai_button);
        if (dataBean.isExpanded()) {
            recyclerView.setVisibility(View.VISIBLE);
            ivUp.setRotation(180);
        } else {
            recyclerView.setVisibility(View.GONE);
            ivUp.setRotation(0);
        }

        // 子项LayoutManager
        //展开收起按钮
        ivUp.setOnClickListener(v -> {
            // 处理展开收起点击事件 切换展开收起状态
            boolean isExpanded = dataBean.isExpanded();
            if (isExpanded) {
                // 收起
                notifyItemChanged(baseViewHolder.getLayoutPosition());
            } else {
                if (dataBean.getDetailList() != null && !dataBean.getDetailList().isEmpty()) {
                    notifyItemChanged(baseViewHolder.getLayoutPosition());
                } else {
                    // 展开
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(baseViewHolder.getLayoutPosition(), dataBean);
                    }
                }
            }
            dataBean.setExpanded(!isExpanded);
        });

        //点击批次号  复制
        baseViewHolder.getView(R.id.tv_material_code).setOnClickListener(v -> {
            //判断是否有物料信息
            if (dataBean.getMaterial() == null) {
                return;
            }
            StringUtil.copyToClipboard(getContext(), dataBean.getMaterial().getMaterialCode());
        });

        //撤销按钮
        baseViewHolder.getView(R.id.tv_cancel).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemCancelClick(baseViewHolder.getLayoutPosition(), dataBean);
            }
        });
    }

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

        void onItemCancelClick(int position, OutBoundDetailModel.RowsBean item);

        void onItemCancelChildClick(int position, OutboundFlowingWaterModel.RowsBean item);
    }

    private OnItemClickListener mItemClickListener;

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