package com.cscodetech.movers.adepter;

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

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cscodetech.movers.R;
import com.cscodetech.movers.databinding.PaymentLayoutBinding;
import com.cscodetech.movers.model.PaymentdataItem;
import com.cscodetech.movers.retrofit.APIClient;

import java.util.List;

public class PaymentAdapter extends RecyclerView.Adapter<PaymentAdapter.MyViewHolder> {



    private Context mContext;
    private List<PaymentdataItem> typeList;
    private RecyclerTouchListener listener;
    private int lastCheckedPos = -1;


    public interface RecyclerTouchListener {
        public void onClickPaymentInfo(PaymentdataItem item, int position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private PaymentLayoutBinding binding;
        public MyViewHolder(View view) {
            super(view);
            binding = PaymentLayoutBinding.bind(view);
        }
    }

    public PaymentAdapter(Context mContext, List<PaymentdataItem> typeList, final RecyclerTouchListener listener) {
        this.mContext = mContext;
        this.typeList = typeList;
        this.listener = listener;
      lastCheckedPos = -1;


    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.payment_layout, parent, false);
        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        PaymentdataItem item=typeList.get(position);
        if (lastCheckedPos == position) {
            holder.binding.lvlClick.setBackground(mContext.getDrawable(R.drawable.round_boxfill));
            holder.binding.imgSelect.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.radios_fill));
        } else {
            holder.binding.lvlClick.setBackground(mContext.getDrawable(R.drawable.round_box));
            holder.binding.imgSelect.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.radios_empty));
        }
        holder.binding.txtPname.setText(""+item.getTitle());
        holder.binding.txtSubtitle.setText(""+item.getSubtitle());
        Glide.with(mContext).load(APIClient.BASE_URL + "/" + item.getImg()).thumbnail(Glide.with(mContext).load(R.drawable.tprofile)).into(holder.binding.img);
        holder.binding.lvlClick.setOnClickListener(view -> {
            lastCheckedPos = position;
            listener.onClickPaymentInfo(item, position);
            notifyDataSetChanged();
        });
    }

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