package com.github.mobile.ui.issue;

import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;

import com.github.mobile.R.string;
import com.github.mobile.accounts.AuthenticatedUserLoader;
import com.github.mobile.core.issue.IssueFilter;
import com.github.mobile.persistence.AccountDataManager;
import com.github.mobile.ui.ItemListAdapter;
import com.github.mobile.ui.ItemListFragment;
import com.github.mobile.ui.ItemView;
import com.github.mobile.util.AvatarLoader;
import com.github.mobile.util.ListViewUtils;
import com.google.inject.Inject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Fragment to display a list of {@link IssueFilter} objects
 */
public class FilterListFragment extends ItemListFragment<IssueFilter> {

    @Inject
    private AccountDataManager cache;

    @Inject
    private AvatarLoader avatars;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ListViewUtils.configure(getActivity(), getListView(), true);
        setEmptyText(getString(string.no_filters));
    }

    @Override
    public Loader<List<IssueFilter>> onCreateLoader(int id, Bundle args) {
        return new AuthenticatedUserLoader<List<IssueFilter>>(getActivity()) {

            public List<IssueFilter> load() {
                List<IssueFilter> filters = new ArrayList<IssueFilter>(cache.getIssueFilters());
                Collections.sort(filters, new Comparator<IssueFilter>() {

                    public int compare(IssueFilter lhs, IssueFilter rhs) {
                        int compare = lhs.getRepository().generateId()
                                .compareToIgnoreCase(rhs.getRepository().generateId());
                        if (compare == 0)
                            compare = lhs.toDisplay().toString().compareToIgnoreCase(rhs.toDisplay().toString());
                        return compare;
                    }
                });
                return filters;
            }
        };
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        IssueFilter filter = (IssueFilter) l.getItemAtPosition(position);
        startActivity(IssueBrowseActivity.createIntent(filter));
    }

    @Override
    public void onResume() {
        super.onResume();

        refresh();
    }

    @Override
    protected ItemListAdapter<IssueFilter, ? extends ItemView> createAdapter(List<IssueFilter> items) {
        return new FilterListAdapter(getActivity().getLayoutInflater(), items.toArray(new IssueFilter[items.size()]),
                avatars);
    }
}
@@ -30,16 +30,71 @@
 issue;%0A%0A
+import static java.lang.String.CASE_INSENSITIVE_ORDER;%0A
 import a
@@ -241,16 +241,19 @@
 ile.
-R.string
+AsyncLoader
 ;%0Aim
@@ -279,40 +279,16 @@
 ile.
-accounts.AuthenticatedUserLoader
+R.string
 ;%0Aim
@@ -892,16 +892,51 @@
 eFilter%3E
+ implements Comparator%3CIssueFilter%3E
  %7B%0A%0A    
@@ -1401,24 +1401,12 @@
 ew A
-uthenticatedUser
+sync
 Load
@@ -1441,24 +1441,46 @@
 ivity()) %7B%0A%0A
+            @Override%0A
             
@@ -1508,16 +1508,28 @@
 er%3E load
+InBackground
 () %7B%0A   
@@ -1668,510 +1668,31 @@
 rs, 
-new Comparator%3CIssueFilter%3E() %7B%0A%0A                    public int compare(IssueFilter lhs, IssueFilter rhs) %7B%0A                        int compare = lhs.getRepository().generateId()%0A                                .compareToIgnoreCase(rhs.getRepository().generateId());%0A                        if (compare == 0)%0A                            compare = lhs.toDisplay().toString().compareToIgnoreCase(rhs.toDisplay().toString());%0A                        return compare;%0A                    %7D%0A                %7D
+FilterListFragment.this
 );%0A 
@@ -2354,10 +2354,401 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public int compare(final IssueFilter lhs, final IssueFilter rhs) %7B%0A        int compare = CASE_INSENSITIVE_ORDER%0A                .compare(lhs.getRepository().generateId(), rhs.getRepository().generateId());%0A        if (compare == 0)%0A            compare = CASE_INSENSITIVE_ORDER.compare(lhs.toDisplay().toString(), rhs.toDisplay().toString());%0A        return compare;%0A    %7D
 %0A%7D

Implement IssueFilter comparator in FilterListFragment