package com.hw.nativeapp.ui.activity.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hw.nativeapp.R;
import com.hw.nativeapp.httpnet.entity.StockOutBean;
import com.hw.nativeapp.ui.activity.holders.AftermarketHandleHolder;

import java.util.ArrayList;
import java.util.List;

public class AftermarketHandleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


    private Context mContext;
    private List<StockOutBean> mDatas ;
    private OnBtnClickListerer mOnReplenishBtnClickListerer;
    private OnBtnClickListerer mOnOutBtnClickListerer;


    public AftermarketHandleAdapter(Context context) {
        mContext = context;
        mDatas = new ArrayList<>();
    }

    public void addDatas(List<StockOutBean> datas){
        if (mDatas == null){
            mDatas = new ArrayList<>();
        }
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clearDatas(){
        if (mDatas != null){
            mDatas.clear();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_aftermarket_1, parent, false);
        return new AftermarketHandleHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        AftermarketHandleHolder normalHolder = (AftermarketHandleHolder) holder;
        StockOutBean detailBean = mDatas.get(position);
        if (detailBean == null){
            return;
        }
        normalHolder.tx_outOrderNum.setText(detailBean.orderNumber);


        normalHolder.btn_outStock.setOnClickListener(v -> {
            if (mOnOutBtnClickListerer != null){
                mOnOutBtnClickListerer.onClick(detailBean, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void setOnReplenishBtnClickListerer(OnBtnClickListerer onBtnClickListerer) {
        this.mOnReplenishBtnClickListerer = onBtnClickListerer;
    }

    public interface OnBtnClickListerer{
        void onClick(StockOutBean bean, int position);
    }
}
