package com.yono.messeripos.adapter;

import android.annotation.SuppressLint;
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.google.gson.Gson;
import com.yono.messeripos.CartActivity;
import com.yono.messeripos.MainActivity;
import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemCartBinding;
import com.yono.messeripos.databinding.ItemCheckoutBinding;
import com.yono.messeripos.models.CartModels;
import com.yono.messeripos.models.CategoryModels;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.utils.Utils;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

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

    private List<ProductCartModels> cartModels;
    private int price;
    Context context;
    Utils utils = new Utils();

    public interface CartListener {
        void onDelete(ProductCartModels productCartModels);

        void onUpdateQuantityPlus(ProductCartModels productCartModels);

        void onUpdateQuantityMinus(ProductCartModels productCartModels);

        void onCheckout(ProductCartModels productCartModels);
    }

    private CartListener listener;

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

    public void setCartAdapter(Context context, List<ProductCartModels> cartModels) {
        this.cartModels = cartModels;
        this.context = context;
//        String gs = new Gson().toJson(cartModels);
//        Log.d("CartAdapter: ", gs);
        Log.d("CartAdapter: ", String.valueOf(this.cartModels));
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CartAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCartBinding itemCartBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.item_cart,
                parent,
                false
        );

        return new MyViewHolder(itemCartBinding);
    }

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

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

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

        private ItemCartBinding binding;
        public MyViewHolder(@NonNull ItemCartBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        @SuppressLint("SetTextI18n")
        public void bindData(ProductCartModels cartModels) {
            /*
            * Data binding for itemCart in cart activity
            * */

            subtotal = cartModels.getPrice_orders() * cartModels.getQuantity_orders();
            Log.d("TAG", "bindData: "+binding.getSubTotal());
            utils.grandTotal = (cartModels.getPrice_orders() * cartModels.getQuantity_orders());
            Log.i("Grand Total", "bindData: "+utils.grandTotal);

            binding.setCarts(cartModels);
            binding.setPrice(utils.convertPrice("Rp. ", cartModels.getPrice_orders()));
            binding.setSubTotal(utils.convertPrice("Rp. ", subtotal));
            binding.etQty.setText(Long.toString(cartModels.getQuantity_orders()));


            // set edit text qty
            binding.etQty.setText(Long.toString(cartModels.getQuantity_orders()));

            binding.btPlus.setOnClickListener(view -> {
                listener.onUpdateQuantityPlus(cartModels);
            });

            binding.btMinus.setOnClickListener(view -> {
                listener.onUpdateQuantityMinus(cartModels);
            });

            binding.btDelete.setOnClickListener(view -> {
                listener.onDelete(cartModels);
            });



        }
    }
}
