package fr.masciulli.drinks.ui.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Picasso;

import fr.masciulli.drinks.R;
import fr.masciulli.drinks.model.Drink;
import fr.masciulli.drinks.ui.adapter.holder.TileViewHolder;
import fr.masciulli.drinks.ui.view.RatioImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class DrinksAdapter extends RecyclerView.Adapter<TileViewHolder> {
    private static final int TYPE_HERO_169 = 0;
    private static final int TYPE_43 = 1;

    private static final float RATIO_34 = 3.0f / 4.0f;
    private static final float RATIO_169 = 16.0f / 9.0f;

    private static final int HERO_INTERVAL = 9;

    private List<Drink> drinks = new ArrayList<>();
    private List<Drink> filteredDrinks = new ArrayList<>();
    private Map<Drink, Integer> ratioMap = new HashMap<>();

    private ItemClickListener<Drink> listener;
    private Placeholders placeHolders = new Placeholders();

    @Override
    public TileViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_drink, parent, false);
        return new TileViewHolder(root);
    }

    @Override
    public void onBindViewHolder(final TileViewHolder holder, int position) {
        final Drink drink = filteredDrinks.get(position);
        holder.getNameView().setText(drink.getName());

        RatioImageView imageView = holder.getImageView();
        switch (getItemViewType(position)) {
            case TYPE_HERO_169:
                imageView.setRatio(RATIO_169);
                break;
            case TYPE_43:
                imageView.setRatio(RATIO_34);
                break;
            default:
                throw new IllegalArgumentException("Unknown ratio type");
        }

        Context context = holder.itemView.getContext();

        Picasso.with(context)
                .load(drink.getImageUrl())
                .fit()
                .placeholder(placeHolders.get(context, position))
                .centerCrop()
                .into(imageView);

        if (listener != null) {
            holder.itemView.setOnClickListener(v -> holder.getNameView().animate()
                    .translationY(holder.getNameView().getHeight())
                    .withEndAction(() -> {
                        holder.getNameView().setTranslationY(0);
                        listener.onItemClick(holder.getAdapterPosition(), drink);
                    }).start());
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        Drink drink = drinks.get(position);
        return ratioMap.get(drink);
    }

    public void setItemClickListener(ItemClickListener<Drink> listener) {
        this.listener = listener;
    }

    public void setDrinks(List<Drink> drinks) {
        filteredDrinks.clear();
        this.drinks.clear();

        this.drinks.addAll(drinks);
        filteredDrinks.addAll(drinks);

        fakeRatios();
        notifyDataSetChanged();
    }

    private void fakeRatios() {
        ratioMap.clear();
        for (int i = 0, size = drinks.size(); i < size; i++) {
            Drink drink = drinks.get(i);
            ratioMap.put(drink, i % HERO_INTERVAL == 0 ? TYPE_HERO_169 : TYPE_43);
        }
    }

    public void filter(String text) {
        filteredDrinks.clear();
        text = text.toLowerCase(Locale.US);
        for (Drink drink : drinks) {
            if (drink.getName().toLowerCase(Locale.US).contains(text)) {
                filteredDrinks.add(drink);
            } else {
                for (String ingredient : drink.getIngredients()) {
                    if (ingredient.toLowerCase(Locale.US).contains(text)) {
                        filteredDrinks.add(drink);
                        break;
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    public void clearFilter() {
        filteredDrinks.clear();
        filteredDrinks.addAll(drinks);
        notifyDataSetChanged();
    }

    public ArrayList<Drink> getDrinks() {
        return new ArrayList<>(drinks);
    }

    public RecyclerView.LayoutManager craftLayoutManager(Context context) {
        int columnCount = context.getResources().getInteger(R.integer.column_count);
        GridLayoutManager layoutManager = new GridLayoutManager(context, columnCount);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return getItemViewType(position) == DrinksAdapter.TYPE_HERO_169 ? columnCount : 1;
            }
        });

        return layoutManager;
    }
}

@@ -99,33 +99,28 @@
 .widget.
-GridLayoutManager
+RecyclerView
 ;%0Aimport
@@ -142,36 +142,50 @@
 t.v7.widget.
-RecyclerView
+StaggeredGridLayoutManager
 ;%0Aimport and
@@ -306,16 +306,141 @@
 casso;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Locale;%0Aimport java.util.Map;%0A%0A
 import f
@@ -618,133 +618,8 @@
 w;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Locale;%0Aimport java.util.Map;%0A%0A
 publ
@@ -722,24 +722,18 @@
 nt TYPE_
-HERO_169
+34
  = 0;%0A  
@@ -869,78 +869,27 @@
 TIO_
-169 = 16
+43 = 4
 .0f / 
-9
+3
 .0f;%0A%0A
-    private static final int HERO_INTERVAL = 9;%0A%0A
     
@@ -1760,24 +1760,18 @@
 se TYPE_
-HERO_169
+34
 :%0A      
@@ -1805,19 +1805,18 @@
 o(RATIO_
-169
+34
 );%0A     
@@ -1892,34 +1892,34 @@
 .setRatio(RATIO_
-3
 4
+3
 );%0A             
@@ -3544,21 +3544,9 @@
 i %25 
-HERO_INTERVAL
+2
  == 
@@ -3554,24 +3554,18 @@
  ? TYPE_
-HERO_169
+34
  : TYPE_
@@ -4594,24 +4594,25 @@
 count);%0A
+%0A
         
 GridLayo
@@ -4607,396 +4607,95 @@
     
-GridLayoutManager layoutManager = new GridLayoutManager(context, columnCount);%0A%0A        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() %7B%0A            @Override%0A            public int getSpanSize(int position) %7B%0A                return getItemViewType(position) == DrinksAdapter.TYPE_HERO_169 ? columnCount : 1;%0A            %7D%0A        %7D);%0A%0A        return layoutManager
+return new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL)
 ;%0A  

Move back to 3/4 vs 4/3 ratios