package com.yono.messeripos.adapter;

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

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

import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemCartBinding;
import com.yono.messeripos.databinding.ItemCheckoutBinding;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.utils.Utils;

import java.util.List;

public class CheckoutAdapter extends RecyclerView.Adapter<CheckoutAdapter.MyViewHolder> {
    private List<ProductCartModels> cartModels;
    private int price;
    Context context;
    Utils utils = new Utils();

    public void setCheckoutAdapter(Context context, List<ProductCartModels> cartModels) {
        this.cartModels = cartModels;
        this.context = context;
        Log.d("Checkout Adapter: ", String.valueOf(this.cartModels));
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CheckoutAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCheckoutBinding itemCheckoutBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.item_checkout,
                parent,
                false
        );
        return new MyViewHolder(itemCheckoutBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull CheckoutAdapter.MyViewHolder holder, int position) {
        holder.binData(cartModels.get(position));
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private long subtotal = 0;

        private ItemCheckoutBinding itemCheckoutBinding;
        public MyViewHolder(@NonNull ItemCheckoutBinding itemView) {
            super(itemView.getRoot());
            this.itemCheckoutBinding = itemView;
        }

        public void binData(ProductCartModels cartModels){
            subtotal = cartModels.getPrice_orders() * cartModels.getQuantity_orders();
            itemCheckoutBinding.setCheckout(cartModels);
            itemCheckoutBinding.setSubtotal(utils.convertPrice("Rp. ", subtotal));
        }
    }
}
