/*
 * Copyright 2011 Azwan Adli Abdullah
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gh4a.fragment;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.client.PageIterator;
import org.eclipse.egit.github.core.service.RepositoryService;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.gh4a.Gh4Application;
import com.gh4a.R;
import com.gh4a.adapter.RepositoryAdapter;
import com.gh4a.adapter.RootAdapter;
import com.gh4a.utils.ApiHelpers;
import com.gh4a.utils.IntentUtils;

public class RepositoryListFragment extends PagedDataBaseFragment<Repository> {
    private String mLogin;
    private String mRepoType;
    private boolean mIsOrg;
    private String mSortOrder;
    private String mSortDirection;

    public static RepositoryListFragment newInstance(String login, boolean isOrg,
            String repoType, String sortOrder, String sortDirection) {
        RepositoryListFragment f = new RepositoryListFragment();

        Bundle args = new Bundle();
        args.putString("user", login);
        args.putBoolean("is_org", isOrg);
        args.putString("repo_type", repoType);
        args.putString("sort_order", sortOrder);
        args.putString("sort_direction", sortDirection);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLogin = getArguments().getString("user");
        mRepoType = getArguments().getString("repo_type");
        mIsOrg = getArguments().getBoolean("is_org");
        mSortOrder = getArguments().getString("sortOrder");
        mSortDirection = getArguments().getString("sortDirection");
    }

    @Override
    protected RootAdapter<Repository, ? extends RecyclerView.ViewHolder> onCreateAdapter() {
        return new RepositoryAdapter(getActivity());
    }

    @Override
    protected int getEmptyTextResId() {
        return R.string.no_repos_found;
    }

    @Override
    protected void onAddData(RootAdapter<Repository, ? extends RecyclerView.ViewHolder> adapter,
            Collection<Repository> repositories) {
        if ("sources".equals(mRepoType) || "forks".equals(mRepoType)) {
            for (Repository repository : repositories) {
                if ("sources".equals(mRepoType) && !repository.isFork()) {
                    adapter.add(repository);
                } else if ("forks".equals(mRepoType) && repository.isFork()) {
                    adapter.add(repository);
                }
            }
            adapter.notifyDataSetChanged();
        } else {
            adapter.addAll(repositories);
        }
    }

    @Override
    public void onItemClick(Repository repository) {
        IntentUtils.openRepositoryInfoActivity(getActivity(), repository);
    }

    @Override
    protected PageIterator<Repository> onCreateIterator() {
        Gh4Application app = Gh4Application.get();
        boolean isSelf = ApiHelpers.loginEquals(mLogin, app.getAuthLogin());
        RepositoryService repoService = (RepositoryService) app.getService(Gh4Application.REPO_SERVICE);

        Map<String, String> filterData = new HashMap<>();

        // We're operating on the limit of what Github's repo API supports. Specifically,
        // it doesn't support sorting for the organization repo list endpoint, so we're using
        // the user repo list endpoint for organizations as well. Doing so has a few quirks though:
        // - the 'all' filter returns an empty list when querying organization repos, so we
        //   need to omit the filter in that case
        // - 'sources' and 'forks' filter types are only supported for the org repo list endpoint,
        //   but not for the user repo list endpoint, hence we emulate it by querying for 'all'
        //   and filtering the result
        // Additionally, using affiliation together with type is not supported, so omit
        // type when adding affiliation.

        String actualFilterType = "sources".equals(mRepoType) || "forks".equals(mRepoType)
                ? "all" : mRepoType;

        if (isSelf && TextUtils.equals(actualFilterType, "all")) {
            filterData.put("affiliation", "owner,collaborator");
        } else if (!TextUtils.equals(actualFilterType, "all") || !mIsOrg) {
            filterData.put("type", actualFilterType);
        }

        if (mSortOrder != null) {
            filterData.put("sort", mSortOrder);
            filterData.put("direction", mSortDirection);
        }

        if (isSelf) {
            return repoService.pageRepositories(filterData);
        }
        return repoService.pageRepositories(mLogin, filterData);
    }
}
@@ -2277,25 +2277,26 @@
 String(%22sort
-O
+_o
 rder%22);%0A    
@@ -2342,25 +2342,26 @@
 String(%22sort
-D
+_d
 irection%22);%0A

Fix repo list sorting.