package com.tuenti.tuentitv.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v17.leanback.app.BackgroundManager;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.ListRowPresenter;
import android.support.v17.leanback.widget.OnItemViewSelectedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
import android.util.DisplayMetrics;
import com.squareup.picasso.Picasso;
import com.tuenti.tuentitv.R;
import com.tuenti.tuentitv.ui.model.CardInfo;
import com.tuenti.tuentitv.ui.picasso.PicassoBackgroundManagerTarget;
import com.tuenti.tuentitv.ui.presenter.MainPresenter;
import com.tuenti.tuentitv.ui.viewpresenter.CardPresenter;
import com.tuenti.tuentitv.ui.viewpresenter.GridItemPresenter;
import java.util.List;
import javax.inject.Inject;

/**
 * Main application fragment used to show most important information related to the user.
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class MainFragment extends BrowseBaseFragment implements MainPresenter.View {

  private static final int UPDATE_BACKGROUND_IMAGE_DELAY_MILLIS = 700;
  private static final int FAVORITES_ROW = 1;
  private static final int CONVERSATIONS_ROW = 2;
  private static final int CONTACTS_ROW = 3;
  private static final int MEDIA_ROW = 4;

  @Inject MainPresenter presenter;

  private DisplayMetrics metrics;
  private PicassoBackgroundManagerTarget backgroundTarget;
  private Handler handler;
  private ChangeBackground lastChangeBackgroundRunnable;

  @Override public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    this.handler = new Handler();
    presenter.setView(this);
    prepareBackgroundManager();
    configureColors();
    configureApplicationIcon();
    hookListeners();
    presenter.loadData();
  }

  @Override public void showDefaultBackground() {
    Picasso.with(getActivity())
        .load(R.drawable.main_fragment_default_background)
        .resize(metrics.widthPixels, metrics.heightPixels)
        .centerCrop()
        .into(backgroundTarget);
  }

  @Override public void updateBackground(String imageUrl) {
    if (lastChangeBackgroundRunnable != null) {
      handler.removeCallbacks(lastChangeBackgroundRunnable);
    }
    lastChangeBackgroundRunnable = new ChangeBackground(getActivity(), imageUrl, backgroundTarget);
    handler.postDelayed(lastChangeBackgroundRunnable, UPDATE_BACKGROUND_IMAGE_DELAY_MILLIS);
  }

  @Override public void showMainInformation(List<CardInfo> favorites, List<CardInfo> conversations,
      List<CardInfo> contacts, List<CardInfo> mediaElements) {
    ArrayObjectAdapter rowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
    CardPresenter cardPresenter = new CardPresenter();

    addElementsToRowsAdapter(R.string.favorites_item_title, favorites, rowsAdapter, cardPresenter,
        FAVORITES_ROW);
    addElementsToRowsAdapter(R.string.recent_conversation_item_title, conversations, rowsAdapter,
        cardPresenter, CONVERSATIONS_ROW);
    addElementsToRowsAdapter(R.string.contacts_item_title, contacts, rowsAdapter, cardPresenter,
        CONTACTS_ROW);
    addElementsToRowsAdapter(R.string.media_elements_item_title, mediaElements, rowsAdapter,
        cardPresenter, MEDIA_ROW);

    HeaderItem gridHeader = new HeaderItem(5, getResources().getString(R.string.preferences), null);

    GridItemPresenter gridPresenter = new GridItemPresenter();
    ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(gridPresenter);
    gridRowAdapter.add("Test1");
    gridRowAdapter.add("Test2");
    gridRowAdapter.add("Test3");
    rowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));

    setAdapter(rowsAdapter);
  }

  private void addElementsToRowsAdapter(int title, List<CardInfo> elements,
      ArrayObjectAdapter rowsAdapter, CardPresenter cardPresenter, int id) {
    ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
    for (CardInfo cardInfo : elements) {
      listRowAdapter.add(cardInfo);
    }
    HeaderItem header = new HeaderItem(id, getString(title), null);
    rowsAdapter.add(new ListRow(header, listRowAdapter));
  }

  private void prepareBackgroundManager() {
    BackgroundManager backgroundManager = BackgroundManager.getInstance(getActivity());
    backgroundManager.attach(getActivity().getWindow());
    backgroundTarget = new PicassoBackgroundManagerTarget(backgroundManager);
    metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
  }

  private void configureColors() {
    setBrandColor(getResources().getColor(R.color.primary_color));
    setSearchAffordanceColor(getResources().getColor(R.color.secondary_color));
  }

  private void configureApplicationIcon() {
    setBadgeDrawable(getResources().getDrawable(R.drawable.icn_wink));
  }

  private void hookListeners() {
    setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
      @Override public void onItemSelected(Presenter.ViewHolder viewHolder, Object item,
          RowPresenter.ViewHolder viewHolder1, Row row) {
        if (row.getId() <= MEDIA_ROW) {
          presenter.onCardInfoSelected((CardInfo) item);
        } else {
          presenter.onPreferencesSelected();
        }
      }
    });
  }

  private static class ChangeBackground implements Runnable {

    private final Context context;
    private String photo;
    private final PicassoBackgroundManagerTarget backgroundTarget;

    public ChangeBackground(Context context, String photo,
        PicassoBackgroundManagerTarget backgroundTarget) {
      this.context = context;
      this.photo = photo;
      this.backgroundTarget = backgroundTarget;
    }

    @Override public void run() {
      Picasso.with(context).cancelRequest(backgroundTarget);
      Picasso.with(context)
          .load(photo)
          .placeholder(R.drawable.main_fragment_default_background)
          .into(backgroundTarget);
    }
  }
}

@@ -676,16 +676,42 @@
 etrics;%0A
+import android.view.View;%0A
 import c
@@ -1997,40 +1997,8 @@
 s);%0A
-    prepareBackgroundManager();%0A
     
@@ -2044,24 +2044,56 @@
 tionIcon();%0A
+    prepareBackgroundManager();%0A
     hookList
@@ -5190,24 +5190,343 @@
 steners() %7B%0A
+    configureOnItemSelectedListener();%0A    configureOnSearchClickedListener();%0A  %7D%0A%0A  private void configureOnSearchClickedListener() %7B%0A    setOnSearchClickedListener(new View.OnClickListener() %7B%0A%0A      @Override public void onClick(View view) %7B%0A%0A      %7D%0A    %7D);%0A  %7D%0A%0A  private void configureOnItemSelectedListener() %7B%0A
     setOnIte

Add search icon to MainFragment