package com.offline.music.playermp3.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.offline.music.playermp3.api.HomeItemClickListener;
import com.offline.music.playermp3.databinding.ItemFooterLoadingBinding;

import java.util.ArrayList;
import java.util.List;

abstract public class BaseAdapter<K, T extends ViewBinding> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected List<K> data = new ArrayList<>();

    protected Context mContext;

    protected static final int TYPE_ITEM = 0;
    protected static final int TYPE_FOOTER = 1;
    protected boolean isLoadingAdded = false;

    public BaseAdapter() {

    }
    public BaseAdapter(Context mContext) {
        this.mContext = mContext;
    }

    protected HomeItemClickListener homeItemClickListener;


    public void setHomeItemClickListener(HomeItemClickListener homeItemClickListener) {
        this.homeItemClickListener = homeItemClickListener;
    }

    public boolean isLoadingAdded() {
        return isLoadingAdded;
    }

    public void addData(List<K> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }


    public void setData(List<K> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void addLoadingFooter() {
        isLoadingAdded = true;
        notifyItemInserted(data.size());
    }

    // Hide loading footer
    public void removeLoadingFooter() {
        isLoadingAdded = false;
        int position = data.size();
        if (position > 0)
            notifyItemRemoved(position);
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            T viewBinding = getViewBinding(parent);
            return new VHolder<>(viewBinding);
        } else {
            // Inflate footer layout
            ItemFooterLoadingBinding inflate = ItemFooterLoadingBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new VHolder<>(inflate);
        }
    }

    protected abstract T getViewBinding(ViewGroup parent);

    @Override
    public int getItemViewType(int position) {
        return (position == data.size() && isLoadingAdded) ? TYPE_FOOTER : TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return data.size() + (isLoadingAdded ? 1 : 0);
    }

    public static class VHolder<V extends ViewBinding> extends RecyclerView.ViewHolder {

        private V vb;

        public V getVb() {
            return vb;
        }

        public VHolder(@NonNull V itemView) {
            super(itemView.getRoot());
            vb = itemView;
        }
    }


    public static class FooterHolder extends RecyclerView.ViewHolder {

        private ItemFooterLoadingBinding vb;

        public ItemFooterLoadingBinding getVb() {
            return vb;
        }

        public FooterHolder(@NonNull ItemFooterLoadingBinding itemView) {
            super(itemView.getRoot());
            vb = itemView;
        }
    }

}
