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

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
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.OutboundFridModel;
import com.sxyz.rfid.presenter.model.OutboundModel;

import java.util.List;

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

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

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, OutboundFridModel.RowsBean dataBean) {
        //批次号
        baseViewHolder.setText(R.id.tv_batch_code, dataBean.getBatchNo());
        String warehouseName = "-";
        if (dataBean.getMaterial() != null && !StringUtils.isEmpty(dataBean.getMaterial().getMaterialName())) {
            warehouseName = dataBean.getMaterial().getMaterialName();
        }
        if (dataBean.getMaterial() != null && !StringUtils.isEmpty(dataBean.getMaterial().getSpecification())) {
            warehouseName += " / " + dataBean.getMaterial().getSpecification();
        }
        if (dataBean.getLocation() != null && !StringUtils.isEmpty(dataBean.getLocation().getLocationName())) {
            warehouseName += " / " + dataBean.getLocation().getLocationName();
        }
        //有效期
        baseViewHolder.setText(R.id.tv_cancel_time, StringUtils.isEmpty(dataBean.getExpirationDate()) ? "-" : dataBean.getExpirationDate());
        //库存数量
        int inventory = dataBean.getAvailableQuantity();
        baseViewHolder.setText(R.id.tv_inventory, inventory + "");

        //物资/规格型号/货架
        baseViewHolder.setText(R.id.tv_warehouse, warehouseName);
        //还需出库
        baseViewHolder.getView(R.id.txt_need_quantityView_default);
        double num;
        //如果计划数量等于已出库数量，还需出库数量为0
        if (dataBean.getPlanQuantity() == dataBean.getActualQuantity()) {
            num = 0;
        } else {
            //否则，还需出库数量为计划数量减去已出库数量
            num = dataBean.getPlanQuantity() - dataBean.getActualQuantity();
        }
        //如果还需出库数量为0，设置文本颜色为红色
        EditText etQuantityViewDefault = baseViewHolder.getView(R.id.quantityView_default);
        etQuantityViewDefault.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        baseViewHolder.setText(R.id.txt_need_quantityView_default, num + "");
        //如果输入的出库数量大于还需出库数量，设置按钮为不可点击
        TextView btnConfirmOut = baseViewHolder.getView(R.id.btn_confirm_out);

        //如果库存数量大于还需出库数量，设置还需出库数量
        if (inventory >= num) {
            etQuantityViewDefault.setText(num + "");
        }
        String inputNum = etQuantityViewDefault.getText().toString();
        //如果库存数量为0，设置按钮为不可点击
        //如果输入的出库数量为空、为0.0或大于还需出库数量，设置按钮为不可点击
        if (inputNum.isEmpty() || inputNum.equals("0.0") || inputNum.equals("0") || inventory == 0) {
            btnConfirmOut.setEnabled(false);
            btnConfirmOut.setBackgroundResource(R.drawable.gray_bg1);
            etQuantityViewDefault.setText("0");
        } else {
            btnConfirmOut.setEnabled(true);
            btnConfirmOut.setBackgroundResource(R.drawable.green_bg);
            etQuantityViewDefault.setText(num + "");
        }

        etQuantityViewDefault.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String input = etQuantityViewDefault.getText().toString();
                try {
                    if (input.isEmpty() || input.equals("0.0") || input.equals("0") || inventory == 0) {
                        btnConfirmOut.setEnabled(false);
                        btnConfirmOut.setBackgroundResource(R.drawable.gray_bg1);
                        return;
                    }
                    double inputValue = Double.parseDouble(input);
                    boolean isValid = inputValue > 0 && inputValue <= num;
                    btnConfirmOut.setEnabled(isValid);
                    btnConfirmOut.setBackgroundResource(isValid ? R.drawable.green_bg : R.drawable.gray_bg1);
                } catch (NumberFormatException e) {
                    btnConfirmOut.setEnabled(false);
                    btnConfirmOut.setBackgroundResource(R.drawable.gray_bg1);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        //出库按钮
        baseViewHolder.getView(R.id.btn_confirm_out).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onConfirmOutClick(baseViewHolder.getLayoutPosition(), dataBean, etQuantityViewDefault.getText().toString());
            }
        });
        baseViewHolder.getView(R.id.btn_cancel_out).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onCancelOutClick(baseViewHolder.getLayoutPosition(), dataBean);
            }
        });
    }

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

        void onConfirmOutClick(int layoutPosition, OutboundFridModel.RowsBean dataBean, String inputNum);

        void onCancelOutClick(int layoutPosition, OutboundFridModel.RowsBean dataBean);
    }

    private OnItemClickListener mItemClickListener;

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