package com.github.mobile.android;

import static com.github.mobile.android.authenticator.Constants.GITHUB_ACCOUNT_TYPE;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

import com.github.kevinsawicki.http.HttpRequest;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;

import java.io.File;
import java.io.IOException;

import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.GitHubRequest;
import org.eclipse.egit.github.core.client.PagedRequest;
import org.eclipse.egit.github.core.service.CollaboratorService;
import org.eclipse.egit.github.core.service.GistService;
import org.eclipse.egit.github.core.service.IssueService;
import org.eclipse.egit.github.core.service.OrganizationService;
import org.eclipse.egit.github.core.service.PullRequestService;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.eclipse.egit.github.core.service.UserService;

/**
 * Main module provide services and clients
 */
public class GitHubModule extends AbstractModule {

    @Override
    protected void configure() {
        HttpRequest.keepAlive(false);
    }

    @Provides
    Account currentAccount(AccountManager accountManager) {
        Account[] accounts = accountManager.getAccountsByType(GITHUB_ACCOUNT_TYPE);
        if (accounts.length > 0) {
            return accounts[0]; // at some point, support more than one github
            // account, ie vanilla and fi
        }
        return null;
    }

    @Provides
    GitHubClient client(Account account, AccountManager accountManager) {
        GitHubClient client = new GitHubClient();
        client.setUserAgent("GitHubAndroid/1.0");
        if (account != null)
            client.setCredentials(account.name, accountManager.getPassword(account));
        return client;
    }

    @Provides
    IssueService issueService(GitHubClient client) {
        return new IssueService(client) {

            protected GitHubRequest createRequest() {
                GitHubRequest request = super.createRequest();
                request.setResponseContentType(ACCEPT_HTML);
                return request;
            }

            protected <V> PagedRequest<V> createPagedRequest(int start, int size) {
                PagedRequest<V> request = super.createPagedRequest(start, size);
                request.setResponseContentType(ACCEPT_HTML);
                return request;
            }
        };
    }

    @Provides
    PullRequestService pullRequestService(GitHubClient client) {
        return new PullRequestService(client);
    }

    @Provides
    UserService userService(GitHubClient client) {
        return new UserService(client);
    }

    @Provides
    GistService gistService(GitHubClient client) {
        return new GistService(client) {

            protected GitHubRequest createRequest() {
                GitHubRequest request = super.createRequest();
                request.setResponseContentType(ACCEPT_HTML);
                return request;
            }

            protected <V> PagedRequest<V> createPagedRequest(int start, int size) {
                PagedRequest<V> request = super.createPagedRequest(start, size);
                request.setResponseContentType(ACCEPT_HTML);
                return request;
            }
        };
    }

    @Provides
    OrganizationService orgService(GitHubClient client) {
        return new OrganizationService(client);
    }

    @Provides
    RepositoryService repoService(GitHubClient client) {
        return new RepositoryService(client);
    }

    @Provides
    User currentUser(UserService userService) throws IOException {
        return userService.getUser(); // actually, probably better to cache this
    }

    @Provides
    CollaboratorService collaboratorService(GitHubClient client) {
        return new CollaboratorService(client);
    }

    @Provides
    AccountDataManager dataManager(Context context, UserService users, OrganizationService orgs,
            RepositoryService repos, IssueService issues) {
        File cache = new File(context.getFilesDir(), "cache");
        return new AccountDataManager(context, cache, users, orgs, repos, issues);
    }
}

@@ -782,32 +782,94 @@
 e.IssueService;%0A
+import org.eclipse.egit.github.core.service.MilestoneService;%0A
 import org.eclip
@@ -4023,32 +4023,159 @@
 client);%0A    %7D%0A%0A
+    @Provides%0A    MilestoneService milestoneService(GitHubClient client) %7B%0A        return new MilestoneService(client);%0A    %7D%0A%0A
     @Provides%0A  

Add milestone service support to GitHubModule