package com.yono.messeripos.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.button.MaterialButton;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.card.MaterialCardView;
import com.yono.messeripos.PaymentActivity;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.response.PaymentResponse;
import com.yono.messeripos.R;
import com.yono.messeripos.databinding.PaymentListBinding;

import java.util.ArrayList;

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

    private ArrayList<PaymentResponse> paymentResponses;
    Context context;
    int getPosition = -1;
    NfcAdapter nfcAdapter;



    public interface PaymentListener {
        void onSelectPayment(PaymentResponse paymentResponse);
    }

    private PaymentListener listener;

    public void setListener(PaymentListener listener) {
        this.listener = listener;
    }

    public PaymentAdapter(ArrayList<PaymentResponse> paymentResponses, Context mainActivity) {
        this.paymentResponses = paymentResponses;
        this.context = mainActivity;
        nfcAdapter= NfcAdapter.getDefaultAdapter(this.context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

//        View view = LayoutInflater.from(parent.getContext())
//                .inflate(R.layout.payment_list, parent, false);
//
//        return new ViewHolder(view);
        PaymentListBinding paymentListBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.payment_list,
                parent,
                false
        );

//        View view = LayoutInflater.from(parent.getContext())
//                .inflate(R.layout.payment_list, parent, false);

        return new ViewHolder(paymentListBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.binData(paymentResponses.get(position));

        holder.radioButton.setChecked(position == getPosition);
//        final PaymentModel paymentModel = paymentModels.get(position);
//
//        ImageView imageView = holder.logoComp;
//
//        Glide.with(context)
//                .load(paymentModel.getUrl())
//                .apply(new RequestOptions().placeholder(R.drawable.skeleton))
//                .into(imageView);
    }


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

    public class ViewHolder extends RecyclerView.ViewHolder {
        PaymentListBinding binding;
        RadioButton radioButton;

        public ViewHolder(@NonNull PaymentListBinding view) {
            super(view.getRoot());
            this.binding = view;
            this.radioButton = view.rbPayment;
        }

        @SuppressLint("ResourceAsColor")
        public void binData(PaymentResponse paymentResponse){
            binding.setPayment(paymentResponse);
            binding.setLogo(paymentResponse.getUrl());

            binding.cvPayment.setOnClickListener(view ->{
                    getPosition = getAdapterPosition();
                    notifyDataSetChanged();
                    listener.onSelectPayment(paymentResponse);});
        }
    }
}
