package fr.masciulli.drinks.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

import fr.masciulli.drinks.R;
import fr.masciulli.drinks.activity.LiquorDetailActivity;
import fr.masciulli.drinks.activity.MainActivity;
import fr.masciulli.drinks.adapter.LiquorListAdapter;
import fr.masciulli.drinks.data.DrinksProvider;
import fr.masciulli.drinks.model.Liquor;
import fr.masciulli.drinks.view.ViewPagerScrollListener;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class LiquorsListFragment extends Fragment implements AdapterView.OnItemClickListener, Callback<List<Liquor>>, ViewPagerScrollListener {
    private static final String STATE_LIST = "liquor";

    private ListView mListView;
    private LiquorListAdapter mListAdapter;

    private ProgressBar mProgressBar;

    private boolean mLoadingError = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        final View root = inflater.inflate(R.layout.fragment_liquors_list, container, false);

        mListView = (ListView) root.findViewById(R.id.list);
        mListView.setEmptyView(root.findViewById(android.R.id.empty));
        mListView.setOnItemClickListener(this);
        mListAdapter = new LiquorListAdapter(getActivity());
        mListView.setAdapter(mListAdapter);

        mProgressBar = (ProgressBar) root.findViewById(R.id.progressbar);

        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey(STATE_LIST)) {
                List<Liquor> savedLiquors = savedInstanceState.getParcelableArrayList(STATE_LIST);
                mListAdapter.update(savedLiquors);
            } else {
                refresh();
            }
        } else {
            refresh();
        }

        return root;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Liquor liquor = mListAdapter.getItem(i);

        // Data needed for animations in sub activity
        int[] screenLocation = new int[2];
        view.getLocationOnScreen(screenLocation);
        int orientation = getResources().getConfiguration().orientation;

        Intent intent = new Intent(getActivity(), LiquorDetailActivity.class);
        intent.putExtra("liquor_name", liquor.name).
                putExtra("liquor_imageurl", liquor.imageUrl).
                putExtra("liquor_id", liquor.id).
                putExtra("top", screenLocation[1]).
                putExtra("height", view.getHeight()).
                putExtra("orientation", orientation);
        startActivity(intent);

        if (!getResources().getBoolean(R.bool.dualpane)) {
            getActivity().overridePendingTransition(0, 0);
        }
    }

    @Override
    public void success(List<Liquor> liquors, Response response) {
        mLoadingError = false;

        if (getActivity() == null) return;

        mProgressBar.setVisibility(View.GONE);
        mListView.getEmptyView().setVisibility(View.VISIBLE);
        mListAdapter.update(liquors);
    }

    @Override
    public void failure(RetrofitError retrofitError) {
        mLoadingError = true;

        if (getActivity() == null) return;

        mProgressBar.setVisibility(View.GONE);
        mListView.getEmptyView().setVisibility(View.VISIBLE);
    }

    @Override
    public void onScroll(int position, float positionOffset, int positionOffsetPixels) {
        if (position == getResources().getInteger(R.integer.position_fragment_ingredients)) return;

        int first = mListView.getFirstVisiblePosition();
        int last = mListView.getLastVisiblePosition();

        for (int i = 0; i <= last - first; i++) {
            View itemRoot = mListView.getChildAt(i);
            if (itemRoot == null) continue;

            TextView nameView = (TextView) itemRoot.findViewById(R.id.name);

            int textWidth = nameView.getWidth();

            nameView.setRight(Math.round(textWidth - (1 - positionOffset) * textWidth));
            nameView.setLeft(Math.round(-(1 - positionOffset) * textWidth));
        }
    }

    private void refresh() {
        mProgressBar.setVisibility(View.GONE);
        mListView.getEmptyView().setVisibility(View.VISIBLE);
        ((MainActivity) getActivity()).setRefreshActionVisible(false);
        DrinksProvider.getAllLiquors(this);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

        if (mLoadingError) {
            ((MainActivity) getActivity()).setRefreshActionVisible(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.retry:
                refresh();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        if (mListAdapter.getCount() > 0) {
            outState.putParcelableArrayList(STATE_LIST, mListAdapter.getLiquors());
        }
    }
}

@@ -467,24 +467,87 @@
 util.List;%0A%0A
+import butterknife.ButterKnife;%0Aimport butterknife.InjectView;%0A
 import fr.ma
@@ -1167,23 +1167,38 @@
 %22;%0A%0A    
-private
+@InjectView(R.id.list)
  ListVie
@@ -1218,84 +1218,159 @@
     
-private LiquorListAdapter mListAdapter;%0A%0A    private ProgressBar mProgressBa
+@InjectView(android.R.id.empty) View mEmptyView;%0A    @InjectView(R.id.progressbar) ProgressBar mProgressBar;%0A%0A    private LiquorListAdapter mListAdapte
 r;%0A%0A
@@ -1658,25 +1658,24 @@
 false);%0A
-%0A
         
 mListVie
@@ -1670,61 +1670,41 @@
     
-mListView = (ListView) root.findViewById(R.id.lis
+ButterKnife.inject(this, roo
 t);%0A
+%0A
     
@@ -1734,45 +1734,18 @@
 iew(
-root.findViewById(android.R.id.empty)
+mEmptyView
 );%0A 
@@ -1901,83 +1901,8 @@
 );%0A%0A
-        mProgressBar = (ProgressBar) root.findViewById(R.id.progressbar);%0A%0A
     
@@ -3441,36 +3441,24 @@
 );%0A        m
-ListView.get
 EmptyView().
@@ -3446,34 +3446,32 @@
       mEmptyView
-()
 .setVisibility(V
@@ -3725,36 +3725,24 @@
 );%0A        m
-ListView.get
 EmptyView().
@@ -3730,34 +3730,32 @@
       mEmptyView
-()
 .setVisibility(V
@@ -4636,28 +4636,16 @@
        m
-ListView.get
 EmptyVie
@@ -4645,18 +4645,16 @@
 mptyView
-()
 .setVisi

Use Butterknife in LiquorListFragment