package com.github.cjnosal.yats.slideshow;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.github.cjnosal.yats.R;
import com.github.cjnosal.yats.YATSApplication;
import com.github.cjnosal.yats.modules.ApplicationComponent;
import com.github.cjnosal.yats.network.AuthManager;
import com.github.cjnosal.yats.network.services.RedditService;
import com.github.cjnosal.yats.slideshow.modules.DaggerSlideshowComponent;
import com.github.cjnosal.yats.slideshow.modules.SlideshowComponent;
import com.squareup.picasso.Picasso;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SlideshowActivity extends RxAppCompatActivity implements SlideshowContract.View {

    @Bind(R.id.slide_pager)
    ViewPager slidePager;

    @Inject
    RedditService redditService;

    @Inject
    AuthManager authManager;

    SlideshowContract.UserActionListener listener;
    SlideAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slideshow);
        ButterKnife.bind(this);

        ApplicationComponent applicationComponent = ((YATSApplication) getApplication()).getApplicationComponent();
        SlideshowComponent slideshowComponent = DaggerSlideshowComponent.builder().applicationComponent(applicationComponent).build();
        slideshowComponent.inject(this);

        adapter = new SlideAdapter();
        slidePager.setAdapter(adapter);

        slidePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position > (adapter.getCount() - 3)) {
                    listener.fetchImages(); // FIXME fetch next 10 images
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        listener = new SlideshowPresenter(redditService, authManager, this);
        listener.fetchImages();
    }

    @Override
    public void displayImages(List<String> urls) {
        List<String> adapterImages = adapter.getImages();
        adapterImages.addAll(urls);
        adapter.setImages(adapterImages);
    }
}

@@ -98,121 +98,17 @@
 iew.
+View
 Pager
-Adapter;%0Aimport android.support.v4.view.ViewPager;%0Aimport android.view.View;%0Aimport android.widget.ImageView
 ;%0A%0Ai
@@ -512,45 +512,8 @@
 nt;%0A
-import com.squareup.picasso.Picasso;%0A
 impo
@@ -579,16 +579,44 @@
 ivity;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -810,24 +810,139 @@
 act.View %7B%0A%0A
+    public static final String CURRENT_SLIDE = %22current_slide%22;%0A    public static final String SLIDES = %22slides%22;%0A%0A
     @Bind(R.
@@ -2374,24 +2374,253 @@
 ger, this);%0A
+        if (savedInstanceState != null) %7B%0A            adapter.setImages(savedInstanceState.getStringArrayList(SLIDES));%0A            slidePager.setCurrentItem(savedInstanceState.getInt(CURRENT_SLIDE), false);%0A        %7D else %7B%0A    
         list
@@ -2635,24 +2635,34 @@
 chImages();%0A
+        %7D%0A
     %7D%0A%0A    @
@@ -2862,11 +2862,287 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A        outState.putInt(CURRENT_SLIDE, slidePager.getCurrentItem());%0A        outState.putStringArrayList(SLIDES, new ArrayList%3C%3E(adapter.getImages()));%0A    %7D
 %0A%7D%0A

Save user's position when recreating SlideshowActivity