/*
 * Copyright (C) 2016 Appflate.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package io.appflate.droidvmp.androidsample.ui.presenters;

import io.appflate.droidmvp.SimpleDroidMVPPresenter;
import io.appflate.droidvmp.androidsample.domain.GithubApi;
import io.appflate.droidvmp.androidsample.model.Repository;
import io.appflate.droidvmp.androidsample.model.presentation.RepositoriesPresentationModel;
import io.appflate.droidvmp.androidsample.ui.mvpviews.RepositoriesView;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by andrzejchm on 22/06/16.
 */
public class RepositoriesPresenter
    extends SimpleDroidMVPPresenter<RepositoriesView, RepositoriesPresentationModel> {
    private GithubApi githubApi;

    public RepositoriesPresenter(GithubApi githubApi) {
        this.githubApi = githubApi;
    }

    @Override public void attachView(RepositoriesView mvpView,
        RepositoriesPresentationModel presentationModel) {
        super.attachView(mvpView, presentationModel);
        if (presentationModel.shouldFetchRepositories()) {
            getUserRepositories(presentationModel);
        } else {
            mvpView.showRepositoriesList(presentationModel.getRepositories());
        }
        mvpView.showTitle(presentationModel.getUsername());
    }

    private void getUserRepositories(RepositoriesPresentationModel presentationModel) {
        if (getMvpView() != null) {
            getMvpView().showLoadingProgress();
        }
        githubApi.getUserRepositories(presentationModel.getUsername())
                 .enqueue(new Callback<List<Repository>>() {
                     public void onResponse(Call<List<Repository>> call,
                         Response<List<Repository>> response) {
                         if (response.isSuccessful()) {
                             onRepositoriesFetched(response.body());
                         } else {
                             onRepositoriesFetchError();
                         }
                     }

                     public void onFailure(Call<List<Repository>> call, Throwable t) {
                         onRepositoriesFetchError();
                     }
                 });
    }

    private void onRepositoriesFetchError() {
        if (getMvpView() != null) {
            getMvpView().showRepositoriesFetchError();
        }
    }

    private void onRepositoriesFetched(List<Repository> repositories) {
        getPresentationModel().setRepositories(repositories);
        if (getMvpView() != null) {
            getMvpView().showRepositoriesList(repositories);
        }
    }
}
