package openfoodfacts.github.scrachx.openfood.views.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.NutrimentItem;

/**
 * @author herau
 */
public class NutrimentsRecyclerViewAdapter extends RecyclerView.Adapter {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    private final List<NutrimentItem> nutrimentItems;

    public NutrimentsRecyclerViewAdapter(List<NutrimentItem> nutrimentItems) {
        super();
        this.nutrimentItems = nutrimentItems;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.nutriment_item_list, parent, false);
            return new NutrimentsRecyclerViewAdapter.NutrimentViewHolder(v);
        } else if (viewType == TYPE_HEADER) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.nutriment_item_list_header, parent, false);
            return new NutrimentsRecyclerViewAdapter.NutrimentHeaderViewHolder(v);
        }

        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (!(holder instanceof NutrimentViewHolder)) {
            return;
        }

        NutrimentItem item = nutrimentItems.get(position);

        NutrimentViewHolder nutrimentViewHolder = (NutrimentViewHolder) holder;

        nutrimentViewHolder.vNutrimentName.setText(item.getTitle());
        nutrimentViewHolder.vNutrimentValue.append(item.getValue());
        nutrimentViewHolder.vNutrimentValue.append(" ");
        nutrimentViewHolder.vNutrimentValue.append(item.getUnit());

        nutrimentViewHolder.vNutrimentServingValue.append(item.getServingValue());
        nutrimentViewHolder.vNutrimentServingValue.append(" ");
        nutrimentViewHolder.vNutrimentServingValue.append(item.getUnit());
    }

    @Override
    public int getItemViewType(int position) {
        return isPositionHeader(position) ? TYPE_HEADER : TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

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

    class NutrimentViewHolder extends RecyclerView.ViewHolder {
        TextView vNutrimentName;
        TextView vNutrimentValue;
        TextView vNutrimentServingValue;

        public NutrimentViewHolder(View v) {
            super(v);
            vNutrimentName = (TextView) v.findViewById(R.id.nutriment_name);
            vNutrimentValue = (TextView) v.findViewById(R.id.nutriment_value);
            vNutrimentServingValue = (TextView) v.findViewById(R.id.nutriment_serving_value);
        }
    }

    class NutrimentHeaderViewHolder extends RecyclerView.ViewHolder {
        public NutrimentHeaderViewHolder(View itemView) {
            super(itemView);
        }
    }
}

@@ -878,301 +878,179 @@
     
-if (viewType == TYPE_ITEM) %7B%0A            View v = LayoutInflater.from(parent.getContext())%0A                    .inflate(R.layout.nutriment_item_list, parent, false);%0A            return new NutrimentsRecyclerViewAdapter.NutrimentViewHolder(v);%0A        %7D else if (viewType == TYPE_HEADER) %7B%0A    
+boolean isViewTypeHeader = viewType == TYPE_HEADER;%0A%0A        int layoutResourceId = isViewTypeHeader ? R.layout.nutriment_item_list_header : R.layout.nutriment_item_list;%0A
     
@@ -1106,29 +1106,8 @@
 t())
-%0A                    
 .inf
@@ -1115,43 +1115,24 @@
 ate(
-R.
 layout
-.nutriment_item_list_header
+ResourceId
 , pa
@@ -1141,28 +1141,25 @@
 nt, false);%0A
-    
+%0A
         retu
@@ -1165,214 +1165,87 @@
 urn 
-new NutrimentsRecyclerViewAdapter.NutrimentHeaderViewHolder(v);%0A        %7D%0A%0A        throw new RuntimeException(%22there is no type that matches the type %22 + viewType + %22 + make sure your using types correctly%22
+isViewTypeHeader ? new NutrimentHeaderViewHolder(v) : new NutrimentViewHolder(v
 );%0A 

fix #493