package tw.wancw.curator;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.Collection;

import tw.wancw.curator.api.CuratorApi;
import tw.wancw.curator.api.MeiZiCard;
import tw.wancw.curator.api.MeiZiCardsResponseHandler;
import tw.wancw.curator.widget.MeiZiCardAdapter;
import tw.wancw.widget.ListViewOnScrollListenerBroadcaster;

public class MeiZiCardsFragment extends Fragment {

    private static final CuratorApi api = new CuratorApi(BuildConfig.CURATOR_API_TOKEN);

    private ImageLoader loader;

    protected MeiZiCardAdapter adapter;
    protected ListView cardsView;
    protected View loadingFooter;

    protected PaginatedStreamLoader streamLoader = new PaginatedStreamLoader();

    public MeiZiCardsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .build();

        ImageLoaderConfiguration imageLoaderConfig = new ImageLoaderConfiguration.Builder(activity)
            .defaultDisplayImageOptions(displayImageOptions)
            .build();

        loader = ImageLoader.getInstance();
        loader.init(imageLoaderConfig);

        adapter = new MeiZiCardAdapter(activity, loader);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_meizi_cards, container, false);

        loadingFooter = inflater.inflate(R.layout.view_load_more, null);

        cardsView = (ListView) rootView.findViewById(R.id.cards);
        cardsView.setEmptyView(rootView.findViewById(android.R.id.empty));

        cardsView.addFooterView(loadingFooter);
        cardsView.setAdapter(adapter);
        cardsView.removeFooterView(loadingFooter);

        cardsView.setOnScrollListener(new ListViewOnScrollListenerBroadcaster(
//            new PauseOnScrollListener(loader, true, true),
            new LoadMoreTrigger()
        ));

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        streamLoader.loadNextPage();
    }

    private class LoadMoreTrigger implements AbsListView.OnScrollListener {

        private boolean reachEnd = false;

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (visibleItemCount > 0 && firstVisibleItem + visibleItemCount >= totalItemCount) {
                streamLoader.loadNextPage();
            }
        }

        @Override
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Do nothing
        }
    }

    private class PaginatedStreamLoader implements MeiZiCardsResponseHandler {

        private int lastPage = 0;
        private boolean loading = false;
        private boolean noMoreData = false;

        public void loadNextPage() {
            if (loading || noMoreData) {
                return;
            }

            loading = true;

            cardsView.addFooterView(loadingFooter);

            lastPage = lastPage + 1;
            api.stream(lastPage, this);
        }

        @Override
        public void onSuccess(Collection<MeiZiCard> cards) {
            if (cards.size() > 0) {
                adapter.add(cards);
            } else {
                noMoreData = true;
            }

            cardsView.removeFooterView(loadingFooter);

            loading = false;
        }

        @Override
        public void OnFailure(String message) {
            cardsView.removeFooterView(loadingFooter);
            loading = false;
            Toast.makeText(getActivity(), "Error: " + message, Toast.LENGTH_LONG).show();
        }
    }
}

@@ -1141,16 +1141,114 @@
 oader();
+%0A    protected PaginatedGirlOfTheDayLoader girlOfTheDayLoader = new PaginatedGirlOfTheDayLoader();
 %0A%0A    pu
@@ -2776,38 +2776,44 @@
 tart();%0A        
-stream
+girlOfTheDay
 Loader.loadNextP
@@ -3833,16 +3833,16 @@
 ge + 1;%0A
-
         
@@ -4194,32 +4194,1106 @@
 lse;%0A        %7D%0A%0A
+        @Override%0A        public void OnFailure(String message) %7B%0A            cardsView.removeFooterView(loadingFooter);%0A            loading = false;%0A            Toast.makeText(getActivity(), %22Error: %22 + message, Toast.LENGTH_LONG).show();%0A        %7D%0A    %7D%0A%0A    private class PaginatedGirlOfTheDayLoader implements MeiZiCardsResponseHandler %7B%0A%0A        private int lastPage = 0;%0A        private boolean loading = false;%0A        private boolean noMoreData = false;%0A%0A        public void loadNextPage() %7B%0A            if (loading %7C%7C noMoreData) %7B%0A                return;%0A            %7D%0A%0A            loading = true;%0A%0A            cardsView.addFooterView(loadingFooter);%0A%0A            lastPage = lastPage + 1;%0A            api.girlOfTheDay(lastPage, this);%0A        %7D%0A%0A        @Override%0A        public void onSuccess(Collection%3CMeiZiCard%3E cards) %7B%0A            if (cards.size() %3E 0) %7B%0A                adapter.add(cards);%0A            %7D else %7B%0A                noMoreData = true;%0A            %7D%0A%0A            cardsView.removeFooterView(loadingFooter);%0A%0A            loading = false;%0A        %7D%0A%0A
         @Overrid

Load "Girl of The Day" instead of "Stream".