package com.github.k24.qiita4jv2;

import com.github.k24.deferred.Deferred;
import com.github.k24.deferred.RxJava2DeferredFactory;
import com.github.k24.qiita4jv2.api.AccessTokensApi;
import com.github.k24.qiita4jv2.model.Item;
import com.github.k24.qiita4jv2.model.team.Team;
import com.github.k24.qiita4jv2.util.Success;
import com.github.k24.retrofit2.converter.jsonic.JsonicConverterFactory;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

import java.util.List;

/**
 * Created by k24 on 2017/03/11.
 */
public class Qiita4jv2Sample {
    public static void main(String[] args) {
        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {
                System.out.println(throwable.toString());
            }
        });

        String accessToken = args.length > 0 ? args[0] : "";
        if (accessToken.isEmpty()) {
            accessToken = System.getenv("QIITA_ACCESS_TOKEN");
            if (accessToken == null || accessToken.isEmpty()) {
                System.out.println("Set AccessToken with args[0] or QIITA_ACCESS_TOKEN");
                return;
            }
        }

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        OkHttpClient okHttpClient = QiitaApiAgent.okHttpClientBuilder(accessToken)
                .addInterceptor(loggingInterceptor)
                .build();

        QiitaApiAgent qiitaApiAgent = QiitaApiAgent.create(
                new QiitaApiAgent.Config(JsonicConverterFactory.create(),
                        new RxJava2DeferredFactory(Schedulers.io())
                ),
                okHttpClient,
                accessToken);

        System.out.println("items:");
        try {
            qiitaApiAgent.itemsApi().items(null, null, "tag:android")
                    .then(QiitaPagination.<List<Item>>mapPaginate())
                    .then(new Deferred.OnResolved<QiitaPagination<List<Item>>, Success>() {
                        @Override
                        public Success onResolved(QiitaPagination<List<Item>> listQiitaPagination) throws Exception {
                            System.out.println("totalCount: " + listQiitaPagination.totalCount());
                            for (Item item : listQiitaPagination.data()) {
                                System.out.println(item.title);
                            }
                            return Success.SUCCESS;
                        }
                    }).waitForCompletion();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("------");

        String teamId = args.length > 1 ? args[1] : null;
        if (teamId == null) {
            teamId = System.getenv("QIITA_TEAM_ID");
            if (teamId == null || teamId.isEmpty()) {
                System.out.println("Set Team ID with args[1] or QIITA_TEAM_ID");
                return;
            }
        }

        QiitaTeamApiAgent qiitaTeamApiAgent = QiitaTeamApiAgent.create(teamId, qiitaApiAgent);

        System.out.println("teams:");

        try {
            qiitaTeamApiAgent.teamsApi().teams()
                    .then(new Deferred.OnResolved<List<Team>, Success>() {
                        @Override
                        public Success onResolved(List<Team> teams) throws Exception {
                            for (Team team : teams) {
                                System.out.println(team.name);
                            }
                            return Success.SUCCESS;
                        }
                    }).waitForCompletion();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@@ -221,16 +221,68 @@
 l.Item;%0A
+import com.github.k24.qiita4jv2.model.team.Project;%0A
 import c
@@ -683,16 +683,43 @@
 rceptor;
+%0Aimport retrofit2.Response;
 %0A%0Aimport
@@ -3469,20 +3469,23 @@
 rintln(%22
-team
+project
 s:%22);%0A%0A 
@@ -3531,25 +3531,387 @@
 ent.
-teamsApi().teams(
+projectsApi().projects(null, null)%0A                    .then(new Deferred.OnResolved%3CResponse%3CList%3CProject%3E%3E, List%3CProject%3E%3E() %7B%0A                        @Override%0A                        public List%3CProject%3E onResolved(Response%3CList%3CProject%3E%3E listResponse) throws Exception %7B%0A                            return listResponse.body();%0A                        %7D%0A                    %7D
 )%0A  
@@ -3963,20 +3963,23 @@
 ed%3CList%3C
-Team
+Project
 %3E, Succe
@@ -4079,18 +4079,24 @@
 ist%3C
-Team%3E team
+Project%3E project
 s) t
@@ -4150,24 +4150,33 @@
 or (
-Team team : team
+Project project : project
 s) %7B
@@ -4227,20 +4227,23 @@
 println(
-team
+project
 .name);%0A

Update Sample to retrieve projects in Team