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.MainActivity;
import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemCategoryBinding;
import com.yono.messeripos.models.CategoryModels;
import com.yono.messeripos.models.ProductModels;

import java.util.ArrayList;

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

    private ArrayList<CategoryModels> categoryModels;
    Context context;

    public interface CategoryListener {
        void onGetProduct(CategoryModels categoryModels);

        void onUpdateCategory(CategoryModels categoryModels);

        void onDeleteCategory(CategoryModels categoryModels, int position);
    }

    private CategoryListener listener;

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


    public void setCategoriAdapter(Context context, ArrayList<CategoryModels> categoryModels){
        this.categoryModels = categoryModels;
        this.context = context;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CategoryAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCategoryBinding itemCategoryBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.item_category,
                parent,
                false
        );

        return new MyViewHolder(itemCategoryBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull CategoryAdapter.MyViewHolder holder, int position) {
        Log.i("POSITION", "onBindViewHolder: "+position);
        holder.binData(categoryModels.get(position));
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ItemCategoryBinding binding;
        public MyViewHolder(@NonNull ItemCategoryBinding itemView) {
            super(itemView.getRoot());
            this.binding = itemView;
        }
        public void binData(CategoryModels categoryModels){

            binding.setCategory(categoryModels);
            binding.cvCategory.setOnClickListener(view -> listener.onGetProduct(categoryModels));
            binding.categoryDelete.setOnClickListener(view -> listener.onDeleteCategory(categoryModels, getAdapterPosition()));
            binding.categoryUpdate.setOnClickListener(view -> listener.onUpdateCategory(categoryModels));

            if (!MainActivity.status_update) {
                binding.categoryDelete.setVisibility(View.GONE);
                binding.categoryUpdate.setVisibility(View.GONE);
            }else{
                if (getAdapterPosition() == 0){
                    binding.categoryUpdate.setVisibility(View.GONE);
                    binding.categoryDelete.setVisibility(View.GONE);
                }else{
                    binding.categoryUpdate.setVisibility(View.VISIBLE);
                    binding.categoryDelete.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}
