package com.app.simplitend.cg_subscription;

import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.app.simplitend.R;
import com.app.simplitend.databinding.SubscriptionViewholderBinding;

import java.util.ArrayList;

public class PlanAdapter extends RecyclerView.Adapter<PlanAdapter.PlanViewHolder>{

    private final ArrayList<SubscriptionPlan> subscriptionPlans;

    public PlanAdapter(@NonNull ArrayList<SubscriptionPlan> subscriptionPlans) {
        this.subscriptionPlans = subscriptionPlans;
    }

    @NonNull
    @Override
    public PlanViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        SubscriptionViewholderBinding binding = SubscriptionViewholderBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false
        );
        return new PlanViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull PlanViewHolder holder, int position) {
        holder.setData(subscriptionPlans.get(position), position);
    }

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

    public static class PlanViewHolder extends RecyclerView.ViewHolder{

        public SubscriptionViewholderBinding binding;

        public PlanViewHolder(@NonNull SubscriptionViewholderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void setData(SubscriptionPlan subscriptionPlan, int position){
            if (subscriptionPlan == null) return;

            binding.planName.setText(subscriptionPlan.plan_name);

            String price = "$" + subscriptionPlan.plan_value;
            binding.planValue.setText(price);

            switch (position){
                case 0:
                    binding.image.setImageResource(R.drawable.ic_sub_1);
                    break;
                case 1:
                default:
                    binding.image.setImageResource(R.drawable.ic_sub_2);
            }

        }
    }
}
