package fr.masciulli.drinks.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
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.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnItemClick;
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;

import java.util.List;

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

    @InjectView(R.id.list)
    ListView mListView;
    @InjectView(android.R.id.empty)
    View mEmptyView;
    @InjectView(R.id.progressbar)
    ProgressBar mProgressBar;

    private LiquorListAdapter mListAdapter;

    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);
        ButterKnife.inject(this, root);

        mListView.setEmptyView(mEmptyView);
        mListAdapter = new LiquorListAdapter(getActivity());
        mListView.setOnScrollListener(new DrinksOnScrollListener(mListView, DrinksOnScrollListener.NAMEVIEW_POSITION_TOP));
        mListView.setAdapter(mListAdapter);

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

        return root;
    }

    @OnItemClick(R.id.list)
    public void openLiquorDetail(View view, int position) {
        Liquor liquor = mListAdapter.getItem(position);

        Intent intent = new Intent(getActivity(), LiquorDetailActivity.class);
        intent.putExtra("liquor", liquor);
        startActivity(intent);

    }

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

        if (getActivity() == null) {
            return;
        }

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

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

        if (getActivity() == null) {
            return;
        }

        mProgressBar.setVisibility(View.GONE);
        mEmptyView.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 = ButterKnife.findById(itemRoot, 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);
        mEmptyView.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());
        }
    }
}

@@ -111,17 +111,104 @@
 oid.
-os.Bundle
+content.SharedPreferences;%0Aimport android.os.Bundle;%0Aimport android.preference.PreferenceManager
 ;%0Aim
@@ -483,24 +483,97 @@
 t.TextView;%0A
+%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.reflect.TypeToken;%0A%0A
 import butte
@@ -3107,24 +3107,284 @@
 te(liquors);
+%0A%0A        Gson gson = new Gson();%0A        String json = gson.toJson(liquors);%0A        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();%0A        editor.putString(%22liquors_json%22, json);%0A        editor.apply();
 %0A    %7D%0A%0A    
@@ -4666,32 +4666,619 @@
 Visible(false);%0A
+%0A        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());%0A        if (preferences.contains(%22liquors_json%22)) %7B%0A            Gson gson = new Gson();%0A            //TODO async%0A            List%3CLiquor%3E liquors = gson.fromJson(preferences.getString(%22liquors_json%22, %22null%22), new TypeToken%3CList%3CLiquor%3E%3E() %7B%0A            %7D.getType());%0A            mListAdapter.update(liquors);%0A            mListView.setVisibility(View.VISIBLE);%0A            mEmptyView.setVisibility(View.VISIBLE);%0A            mProgressBar.setVisibility(View.GONE);%0A        %7D else %7B%0A    
         DrinksPr
@@ -5301,24 +5301,34 @@
 uors(this);%0A
+        %7D%0A
     %7D%0A%0A    @

Save liquors for offline use