package com.example.android.tvleanback.presenter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.RowHeaderPresenter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.android.tvleanback.R;

public class IconHeaderItemPresenter extends RowHeaderPresenter {
    private float mUnselectedAlpha;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup) {
        mUnselectedAlpha = viewGroup.getResources()
                .getFraction(R.fraction.lb_browse_header_unselect_alpha, 1, 1);
        LayoutInflater inflater = (LayoutInflater) viewGroup.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.icon_header_item, null);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object o) {
        HeaderItem headerItem = ((ListRow) o).getHeaderItem();
        View rootView = viewHolder.view;

        ImageView iconView = (ImageView) rootView.findViewById(R.id.header_icon);
        Drawable icon = rootView.getResources().getDrawable(R.drawable.android_header, null);
        iconView.setImageDrawable(icon);

        TextView label = (TextView) rootView.findViewById(R.id.header_label);
        label.setText(headerItem.getName());
    }

    @Override
    public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
        // no op
    }

    // TODO: TEMP - remove me when leanback onCreateViewHolder no longer sets the mUnselectAlpha,AND
    // also assumes the xml inflation will return a RowHeaderView
    @Override
    protected void onSelectLevelChanged(RowHeaderPresenter.ViewHolder holder) {
        // this is a temporary fix
        holder.view.setAlpha(mUnselectedAlpha + holder.getSelectLevel() *
                (1.0f - mUnselectedAlpha));
    }
}
@@ -610,16 +610,17 @@
 enter %7B%0A
+%0A
     priv
@@ -1071,16 +1071,100 @@
 , null);
+%0A        view.setAlpha(mUnselectedAlpha); // Initialize icons to be at half-opacity.
 %0A%0A      
@@ -1872,15 +1872,33 @@
 O: T
-EMP - r
+his is a temporary fix. R
 emov
@@ -1953,16 +1953,23 @@
 sets the
+%0A    //
  mUnsele
@@ -1980,18 +1980,12 @@
 pha,
-AND%0A    //
+ and
  als
@@ -2039,16 +2039,17 @@
 aderView
+.
 %0A    @Ov
@@ -2139,43 +2139,8 @@
 ) %7B%0A
-        // this is a temporary fix%0A
     
@@ -2259,9 +2259,10 @@
 ;%0A    %7D%0A
-
 %7D
+%0A

Initialize sidebar icons with correct alpha