package net.kwmt27.codesearch.presenter.repolist;

import android.os.Bundle;
import android.view.View;

import net.kwmt27.codesearch.ModelLocator;
import net.kwmt27.codesearch.entity.GithubRepoEntity;
import net.kwmt27.codesearch.model.SortType;
import net.kwmt27.codesearch.model.rx.ApiSubscriber;
import net.kwmt27.codesearch.view.repolist.RepositoryListFragment;

import java.util.List;

public class RepositoryListPresenter implements IRepositoryListPresenter {

    private IRepositoryListView mMainView;

    public RepositoryListPresenter(IRepositoryListView mainView) {
        mMainView = mainView;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mMainView.setupComponents(view, savedInstanceState);
        fetchRepositoryList(null, true);
    }

    @Override
    public void onStop() {
        ModelLocator.getSearchRepositoryModel().unsubscribe();
        //ModelLocator.getSearchRepositoryModel().clear();
    }


    @Override
    public void onClickReloadButton() {
        fetchRepositoryList(null, true);
    }

    @Override
    public void onScrollToBottom() {
        if(ModelLocator.getSearchRepositoryModel().hasNextPage()) {
            fetchRepositoryListOnScroll(ModelLocator.getSearchRepositoryModel().getNextPage());
        }
    }

    @Override
    public void onRefresh() {
        ModelLocator.getSearchRepositoryModel().clear();
        fetchRepositoryList(null, false);
    }

    private void fetchRepositoryList(Integer page, boolean show) {
        if(ModelLocator.getSearchRepositoryModel().hasGitHubRepoEntityList()){
            mMainView.updateGitHubRepoListView(ModelLocator.getSearchRepositoryModel().getGitHubRepoEntityList());
            return;
        }

        if(show) {
            mMainView.showProgress();
        }
        ModelLocator.getSearchRepositoryModel().fetchUserRepository(page, SortType.Pushed, new ApiSubscriber<List<GithubRepoEntity>>(((RepositoryListFragment)mMainView).getActivity()) {
            @Override
            public void onCompleted() {
                mMainView.hideProgress();
                mMainView.hideSwipeRefreshLayout();
            }

            @Override
            public void onError(Throwable throwable) {
                super.onError(throwable);
                mMainView.hideProgress();
                mMainView.hideSwipeRefreshLayout();
                mMainView.showError();
            }

            @Override
            public void onNext(List<GithubRepoEntity> githubRepoEntities) {
                mMainView.updateGitHubRepoListView(githubRepoEntities);
            }
        });
    }

    private void fetchRepositoryListOnScroll(Integer page) {
        mMainView.showProgressOnScroll();
        ModelLocator.getSearchRepositoryModel().fetchUserRepository(page, SortType.Pushed, new ApiSubscriber<List<GithubRepoEntity>>(((RepositoryListFragment)mMainView).getActivity()) {
            @Override
            public void onCompleted() {
                mMainView.hideProgressOnScroll();
            }

            @Override
            public void onError(Throwable throwable) {
                super.onError(throwable);
                mMainView.hideProgressOnScroll();
                mMainView.showErrorOnScroll();
            }

            @Override
            public void onNext(List<GithubRepoEntity> githubRepoEntities) {
                mMainView.updateGitHubRepoListView(githubRepoEntities);
            }
        });

    }

    public interface IRepositoryListView {
        void setupComponents(View view, Bundle savedInstanceState);

        void updateGitHubRepoListView(List<GithubRepoEntity> githubRepoEntities);

        void showProgress();

        void hideProgress();

        void showError();

        void showProgressOnScroll();

        void hideProgressOnScroll();

        void showErrorOnScroll();

        void hideSwipeRefreshLayout();
    }

}

@@ -2068,102 +2068,8 @@
 ) %7B%0A
-                mMainView.hideProgress();%0A                mMainView.hideSwipeRefreshLayout();%0A
     
@@ -2070,32 +2070,32 @@
 %7B%0A            %7D%0A
+
 %0A            @Ov
@@ -2436,32 +2436,126 @@
 RepoEntities) %7B%0A
+                mMainView.hideProgress();%0A                mMainView.hideSwipeRefreshLayout();%0A
                 
@@ -2998,58 +2998,8 @@
 ) %7B%0A
-                mMainView.hideProgressOnScroll();%0A
     
@@ -3254,32 +3254,32 @@
       @Override%0A
-
             publ
@@ -3330,32 +3330,82 @@
 RepoEntities) %7B%0A
+                mMainView.hideProgressOnScroll();%0A
                 

move hideProgress method to onNext