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 android.widget.Filter;
import android.widget.Filterable;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;
import com.yono.messeripos.MainActivity;
import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemListBinding;
import com.yono.messeripos.models.MainViewModelsCart;
import com.yono.messeripos.models.ProductModels;
import com.yono.messeripos.utils.Utils;

import java.util.ArrayList;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> implements Filterable {
    private ArrayList<ProductModels> productModels = new ArrayList<>();
    private ArrayList<ProductModels> productModelsFiltered = new ArrayList<>();
    Context context;
    Utils utils = new Utils();

    public interface ProductListener {
        void onUpdate(ProductModels product);

        void onDelete(ProductModels product, int position);

        void onAddCart(ProductModels product);

        void redirect();
    }

    private ProductListener listener;

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


    public void setProduct(Context context, ArrayList<ProductModels> productModels) {
        this.productModels = productModels;
        this.context = context;
        this.productModelsFiltered = productModels;
        notifyDataSetChanged();
    }


    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    productModelsFiltered = productModels;
                } else {
                    ArrayList<ProductModels> filteredList = new ArrayList<>();
                    for (ProductModels prdk : productModels) {
                        if (prdk.getProductName().toLowerCase().contains(charString.toLowerCase())) {
                            filteredList.add(prdk);
                        }
                    }
                    productModelsFiltered = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = productModelsFiltered;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                productModelsFiltered = (ArrayList<ProductModels>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

    @NonNull
    @Override
    public ProductAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemListBinding itemListBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.item_list,
                parent,
                false
        );

        return new MyViewHolder(itemListBinding);
    }

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

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ItemListBinding binding;
        private String statusProduct;

        public MyViewHolder(@NonNull ItemListBinding binding) {

            super(binding.getRoot());
            this.binding = binding;
        }

        @SuppressLint("ResourceAsColor")
        public void bindData(ProductModels products) {
            binding.setProducts(products);

            binding.setPrice(utils.convertPrice("Rp. ", products.getPriceProduct()));
            binding.setImage(products.getImageProduct());

            if (!MainActivity.status_update) {
                binding.btnDelete.setVisibility(View.GONE);
                binding.btnEdit.setVisibility(View.GONE);
                binding.materialBtnAddCart.setVisibility(View.VISIBLE);
            }else{
                binding.btnDelete.setVisibility(View.VISIBLE);
                binding.btnEdit.setVisibility(View.VISIBLE);
                binding.materialBtnAddCart.setVisibility(View.GONE);
            }

            if (products.getStockProduct() <= 1) {
                statusProduct = "Not Available";
                binding.status.setBackgroundColor(0x55FF0000);
                binding.setStatus(statusProduct);
            } else {
                statusProduct = "Available";
                binding.status.setBackgroundColor(0xFF12FF45);
                binding.setStatus(statusProduct);
            }

            if (products.getStockProduct() <= 1){
                binding.materialBtnAddCart.setEnabled(false);
            }else{
                binding.materialBtnAddCart.setEnabled(true);
            }

            binding.materialBtnAddCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onAddCart(products);
                    MainActivity.cartIsEmpty = true;
                    Snackbar.make(view, products.getProductName()+" berhasil ditambahkan", Snackbar.LENGTH_SHORT)
                            .setAction(R.string.open_cart, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    listener.redirect();
                                }
                            })
                            .show();
                }
            });

            binding.btnDelete.setOnClickListener(view -> {
                Log.i("position", "bindData: "+getAdapterPosition());
                listener.onDelete(products, getAdapterPosition());
            });

            binding.btnEdit.setOnClickListener(view -> listener.onUpdate(products));
        }
    }
}
