package com.github.cjnosal.yats.slideshow;

import android.text.TextUtils;

import com.github.cjnosal.yats.network.AuthManager;
import com.github.cjnosal.yats.network.models.AuthResponse;
import com.github.cjnosal.yats.network.models.subreddit.Child;
import com.github.cjnosal.yats.network.models.subreddit.SubredditSearchResponse;
import com.github.cjnosal.yats.network.services.RedditService;
import com.github.cjnosal.yats.rxjava.RxUtils;

import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import retrofit2.http.Url;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;
import timber.log.Timber;

public class SlideshowPresenter implements SlideshowContract.UserActionListener {

    private static final String DEFAULT_SUB = "pics";
    private static final int NUM_IMAGES = 10;

    RedditService redditService;
    AuthManager authManager;
    SlideshowContract.View view;

    public SlideshowPresenter(RedditService redditService, AuthManager authManager, SlideshowContract.View view) {
        this.redditService = redditService;
        this.authManager = authManager;
        this.view = view;
    }

    public void fetchImages() {

        String query = getTimeQuery();

        Observable<List<String>> observable = redditService.searchSubReddit(DEFAULT_SUB, NUM_IMAGES, query)
                .map(new Func1<SubredditSearchResponse, List<Child>>() {
                    @Override
                    public List<Child> call(SubredditSearchResponse subredditSearchResponse) {
                        return subredditSearchResponse.data.children;
                    }
                })
                .map(new Func1<List<Child>, List<String>>() {
                    @Override
                    public List<String> call(List<Child> children) {
                        // TODO flatmap/filter observables
                        List<String> urls = new ArrayList<String>(25);
                        for (Child child : children) {
                            String url = child.data.url;
                            if (!TextUtils.isEmpty(url)) {

                                // TODO more extensions, check data.media.url
                                boolean directLink = url.endsWith("png") || url.endsWith("jpg");
                                if (directLink) {
                                    urls.add(url);
                                }
                            }
                        }
                        return urls;
                    }
                });

        RxUtils.subscribeIO(observable, new Subscriber<List<String>>() {
            @Override
            public void onCompleted() {
                Timber.d("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Timber.e(e, "onError");
            }

            @Override
            public void onNext(List<String> urls) {
                Timber.d("onNext");

                for (String url : urls) {
                    Timber.d(url);
                }
                view.displayImages(urls);
            }
        });
    }

    private String getTimeQuery() {
        Calendar calendar = Calendar.getInstance();

        // move forward to midnight
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        // subtract a year
        calendar.add(Calendar.YEAR, -1);
        long end = calendar.getTimeInMillis() / 1000;

        // subtract a day
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        long start = calendar.getTimeInMillis() / 1000;

        return String.format("timestamp:%1$s..%2$s", start, end);
    }
}

@@ -557,16 +557,67 @@
 .List;%0A%0A
+import okhttp3.Headers;%0Aimport retrofit2.Response;%0A
 import r
@@ -675,24 +675,51 @@
 Subscriber;%0A
+import rx.functions.Func0;%0A
 import rx.fu
@@ -1378,17 +1378,21 @@
 tring%3E%3E 
-o
+urlsO
 bservabl
@@ -1466,33 +1466,37 @@
                .
-m
+flatM
 ap(new Func1%3CSub
@@ -1517,26 +1517,33 @@
 sponse, 
-List%3CChild
+Observable%3CString
 %3E%3E() %7B%0A 
@@ -1598,26 +1598,33 @@
  public 
-List%3CChild
+Observable%3CString
 %3E call(S
@@ -1701,429 +1701,169 @@
     
-return subredditSearchResponse.data.children;%0A                    %7D%0A                %7D)%0A                .map(new Func1%3CList%3CChild%3E, List%3CString%3E%3E() %7B%0A                    @Override%0A                    public List%3CString%3E call(List%3C
+List%3CString%3E urls = new ArrayList%3CString%3E(subredditSearchResponse.data.children.size());%0A                        for (
 Child
-%3E
  child
-ren) %7B%0A                        // TODO flatmap/filter observables%0A                        List%3CString%3E urls = new ArrayList%3CString%3E(25);%0A                        for (Child child : 
+ : subredditSearchResponse.data.
 chil
@@ -1989,87 +1989,8 @@
 )) %7B
-%0A%0A                                // TODO more extensions, check data.media.url
 %0A   
@@ -2129,24 +2129,75 @@
 rectLink) %7B%0A
+                                    Timber.d(url);%0A
             
@@ -2356,20 +2356,37 @@
  return 
+Observable.from(
 urls
+)
 ;%0A      
@@ -2419,16 +2419,42 @@
       %7D)
+%0A                .toList()
 ;%0A%0A     
@@ -2476,17 +2476,21 @@
 cribeIO(
-o
+urlsO
 bservabl
@@ -2880,104 +2880,8 @@
 t%22);
-%0A%0A                for (String url : urls) %7B%0A                    Timber.d(url);%0A                %7D
 %0A   

Simplify url observable