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.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.helper.ImageLoadHelper;
import ch.pizzalemon.android.helper.PriceHelper;
import ch.pizzalemon.android.helper.TextHelper;
import ch.pizzalemon.android.interfaces.RecyclerItemClickListener;
import ch.pizzalemon.android.model.menu.MenuProductModel;

/**
 * Created by cimenmus on 20/09/2017.
 */

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

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

    private ArrayList<MenuProductModel> menuProductList = new ArrayList<>();
    private RecyclerItemClickListener recyclerItemClickListener;
    private boolean isDescriptionVisible;

    public static class MenuProductViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.productNameTextView) TextView productNameTextView;
        @BindView(R.id.productImageView) ImageView productImageView;
        @BindView(R.id.productIngredientsLayout) RelativeLayout productIngredientsLayout;
        @BindView(R.id.productIngredientsTextView) TextView productIngredientsTextView;
        @BindView(R.id.productPriceTextView) TextView productPriceTextView;

        public MenuProductViewHolder(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 == menuProductList.size())
            return HOLDER_SPACE;
        return HOLDER_MENU_PRODUCT;
    }

    public MenuProductRecyclerAdapter(ArrayList<MenuProductModel> menuProductList,
                                      boolean isDescriptionVisible,
                                      RecyclerItemClickListener recyclerItemClickListener){
        this.menuProductList = menuProductList;
        this.recyclerItemClickListener = recyclerItemClickListener;
        this.isDescriptionVisible = isDescriptionVisible;
    }

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

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

        switch (viewType){

            case HOLDER_MENU_PRODUCT:
                view = inflater.inflate(R.layout.row_menu_product, viewGroup, false);
                viewHolder = new MenuProductViewHolder(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_menu_product, viewGroup, false);
                viewHolder = new MenuProductViewHolder(view, recyclerItemClickListener);
                break;
        }

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        switch (holder.getItemViewType()){
            case HOLDER_MENU_PRODUCT :
                MenuProductViewHolder menuProductViewHolder = (MenuProductViewHolder) holder;
                ImageLoadHelper.loadImage(menuProductViewHolder.productImageView, menuProductList.get(position).getImageURL());
                TextHelper.setTextFromHTML(menuProductViewHolder.productNameTextView, menuProductList.get(position).getName());
                menuProductViewHolder.productPriceTextView.setText(
                        PriceHelper.getPriceWithCurreny(menuProductList.get(position).getPrice()));
                TextHelper.setTextFromHTML(menuProductViewHolder.productIngredientsTextView, menuProductList.get(position).getDescription());

                if(isDescriptionVisible){
                    menuProductViewHolder.productIngredientsLayout.setVisibility(View.VISIBLE);
                    TextHelper.setTextFromHTML(menuProductViewHolder.productIngredientsTextView, menuProductList.get(position).getDescription());
                }
                else {
                    menuProductViewHolder.productIngredientsLayout.setVisibility(View.GONE);
                }

                break;

            case HOLDER_SPACE :
                SpaceViewHolder spaceViewHolder = (SpaceViewHolder) holder;
                break;
        }

    }

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

}

