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() {
            }

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

            @Override
            public void onNext(List<GithubRepoEntity> githubRepoEntities) {
                mMainView.hideProgress();
                mMainView.hideSwipeRefreshLayout();
                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() {
            }

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

            @Override
            public void onNext(List<GithubRepoEntity> githubRepoEntities) {
                mMainView.hideProgressOnScroll();
                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();
    }

}

@@ -493,20 +493,30 @@
 stView m
-Main
+RepositoryList
 View;%0A%0A 
@@ -569,20 +569,30 @@
 istView 
-main
+repositoryList
 View) %7B%0A
@@ -604,23 +604,43 @@
    m
-MainView = main
+RepositoryListView = repositoryList
 View
@@ -738,28 +738,38 @@
  %7B%0A        m
-Main
+RepositoryList
 View.setupCo
@@ -1658,36 +1658,46 @@
 )%7B%0A            m
-Main
+RepositoryList
 View.updateGitHu
@@ -1833,36 +1833,46 @@
  %7B%0A            m
-Main
+RepositoryList
 View.showProgres
@@ -2036,37 +2036,48 @@
 oryListFragment)
-mMain
+ mRepositoryList
 View).getActivit
@@ -2288,36 +2288,46 @@
                m
-Main
+RepositoryList
 View.hideProgres
@@ -2340,36 +2340,46 @@
                m
-Main
+RepositoryList
 View.hideSwipeRe
@@ -2402,36 +2402,46 @@
                m
-Main
+RepositoryList
 View.showError()
@@ -2564,36 +2564,46 @@
                m
-Main
+RepositoryList
 View.hideProgres
@@ -2616,36 +2616,46 @@
                m
-Main
+RepositoryList
 View.hideSwipeRe
@@ -2678,36 +2678,46 @@
                m
-Main
+RepositoryList
 View.updateGitHu
@@ -2846,36 +2846,46 @@
 age) %7B%0A        m
-Main
+RepositoryList
 View.showProgres
@@ -3055,21 +3055,32 @@
 ragment)
-mMain
+ mRepositoryList
 View).ge
@@ -3299,36 +3299,46 @@
                m
-Main
+RepositoryList
 View.hideProgres
@@ -3363,28 +3363,38 @@
            m
-Main
+RepositoryList
 View.showErr
@@ -3533,28 +3533,38 @@
            m
-Main
+RepositoryList
 View.hidePro
@@ -3601,12 +3601,22 @@
    m
-Main
+RepositoryList
 View

change variable name