package ch.pizzalemon.android.adapter.recycler;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.activity.BaseActivity;
import ch.pizzalemon.android.interfaces.RecyclerItemClickListener;
import ch.pizzalemon.android.model.CategoryModel;

/**
 * Created by cimenmus on 02/10/2017.
 */


public class NavigationMenuRecyclerAdapter extends RecyclerView.Adapter<NavigationMenuRecyclerAdapter.ViewHolder> {

    private ArrayList<CategoryModel> categoryList;
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.categoryNameItem) TextView categoryNameItem;

        public ViewHolder(final View view, final RecyclerItemClickListener recyclerItemClickListener) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(recyclerItemClickListener != null)
                        recyclerItemClickListener.onItemClick(view, getAdapterPosition());
                }
            });
        }

    }

    public NavigationMenuRecyclerAdapter(ArrayList<CategoryModel> categoryList, RecyclerItemClickListener recyclerItemClickListener){
        this.categoryList = categoryList;
        this.recyclerItemClickListener = recyclerItemClickListener;
    }

    @Override
    public NavigationMenuRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View root = LayoutInflater.from(BaseActivity.currentActivity).inflate(R.layout.row_category, viewGroup, false);
        return new NavigationMenuRecyclerAdapter.ViewHolder(root, recyclerItemClickListener);
    }

    @Override
    public void onBindViewHolder(NavigationMenuRecyclerAdapter.ViewHolder holder, int position) {
        holder.categoryNameItem.setText(categoryList.get(position).getName());
    }

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


/*
public class NavigationMenuRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private final int HOLDER_CATEGORY = 0;
    private final int HOLDER_SPACE = 1;

    private ArrayList<CategoryModel> categoryList;
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class CategoryViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.categoryNameItem) TextView categoryNameItem;

        public CategoryViewHolder(final View view, final RecyclerItemClickListener recyclerItemClickListener) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(recyclerItemClickListener != null)
                        recyclerItemClickListener.onItemClick(view, getAdapterPosition());
                }
            });

        }
    }

    public static class SpaceViewHolder extends RecyclerView.ViewHolder{
        public SpaceViewHolder(final View view) {
            super(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position == categoryList.size())
            return HOLDER_SPACE;
        return HOLDER_CATEGORY;
    }

    public NavigationMenuRecyclerAdapter(ArrayList<CategoryModel> categoryList,
                                RecyclerItemClickListener recyclerItemClickListener){
        this.categoryList = categoryList;
        this.recyclerItemClickListener = recyclerItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        RecyclerView.ViewHolder viewHolder;
        View view;
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        switch (viewType){

            case HOLDER_CATEGORY:
                view = inflater.inflate(R.layout.row_category, viewGroup, false);
                viewHolder = new CategoryViewHolder(view, recyclerItemClickListener);
                break;

            case HOLDER_SPACE:
                view = inflater.inflate(R.layout.row_space, viewGroup, false);
                viewHolder = new SpaceViewHolder(view);
                break;

            default:
                view = inflater.inflate(R.layout.row_category, viewGroup, false);
                viewHolder = new CategoryViewHolder(view, recyclerItemClickListener);
                break;
        }

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (holder.getItemViewType()){
            case HOLDER_CATEGORY :
                CategoryViewHolder categoryViewHolder = (CategoryViewHolder) holder;
                categoryViewHolder.categoryNameItem.setText(categoryList.get(position).getName());
                break;
            case HOLDER_SPACE :
                SpaceViewHolder spaceViewHolder = (SpaceViewHolder) holder;
                break;
        }
    }

    @Override
    public int getItemCount() {
        return categoryList.size() + 1 ;
    }

}
*/

