content: /*
   * Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
   * 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
   *
   *     https://mindorks.com/license/apache-v2
   *
   * 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 <%= appPackage %>.ui.main;
  
  import com.androidnetworking.error.ANError;
  import <%= appPackage %>.data.db.model.Question;
  import <%= appPackage %>.data.network.model.LogoutResponse;
  import <%= appPackage %>.ui.base.BasePresenter;
  import <%= appPackage %>.utils.rx.SchedulerProvider;
  
  import java.util.List;
  
  import javax.inject.Inject;
  
  import io.reactivex.disposables.CompositeDisposable;
  import io.reactivex.functions.Consumer;
  
  
  /**
   * Created by janisharali on 27/01/17.
   */
  
  public class MainPresenter<V extends MainMvpView, I extends MainMvpInteractor>
          extends BasePresenter<V, I> implements MainMvpPresenter<V, I> {
  
      private static final String TAG = "MainPresenter";
  
      @Inject
      public MainPresenter(I mvpInteractor,
                           SchedulerProvider schedulerProvider,
                           CompositeDisposable compositeDisposable) {
          super(mvpInteractor, schedulerProvider, compositeDisposable);
      }
  
      @Override
      public void onDrawerOptionAboutClick() {
          getMvpView().closeNavigationDrawer();
          getMvpView().showAboutFragment();
      }
  
      @Override
      public void onDrawerOptionLogoutClick() {
          getMvpView().showLoading();
  
          getCompositeDisposable().add(getInteractor().doLogoutApiCall()
                  .subscribeOn(getSchedulerProvider().io())
                  .observeOn(getSchedulerProvider().ui())
                  .subscribe(new Consumer<LogoutResponse>() {
                      @Override
                      public void accept(LogoutResponse response) throws Exception {
                          if (!isViewAttached()) {
                              return;
                          }
  
                          getInteractor().setUserAsLoggedOut();
                          getMvpView().hideLoading();
                          getMvpView().openLoginActivity();
                      }
                  }, new Consumer<Throwable>() {
                      @Override
                      public void accept(Throwable throwable) throws Exception {
                          if (!isViewAttached()) {
                              return;
                          }
  
                          getMvpView().hideLoading();
  
                          // handle the login error here
                          if (throwable instanceof ANError) {
                              ANError anError = (ANError) throwable;
                              handleApiError(anError);
                          }
                      }
                  }));
  
      }
  
      @Override
      public void onViewInitialized() {
          getCompositeDisposable().add(getInteractor()
                  .getAllQuestions()
                  .subscribeOn(getSchedulerProvider().io())
                  .observeOn(getSchedulerProvider().ui())
                  .subscribe(new Consumer<List<Question>>() {
                      @Override
                      public void accept(List<Question> questionList) throws Exception {
                          if (!isViewAttached()) {
                              return;
                          }
  
                          if (questionList != null) {
                              getMvpView().refreshQuestionnaire(questionList);
                          }
                      }
                  }));
      }
  
      @Override
      public void onCardExhausted() {
          getCompositeDisposable().add(getInteractor()
                  .getAllQuestions()
                  .subscribeOn(getSchedulerProvider().io())
                  .observeOn(getSchedulerProvider().ui())
                  .subscribe(new Consumer<List<Question>>() {
                      @Override
                      public void accept(List<Question> questionList) throws Exception {
                          if (!isViewAttached()) {
                              return;
                          }
  
                          if (questionList != null) {
                              getMvpView().reloadQuestionnaire(questionList);
                          }
                      }
                  }));
      }
  
      @Override
      public void onNavMenuCreated() {
          if (!isViewAttached()) {
              return;
          }
          getMvpView().updateAppVersion();
  
          final String currentUserName = getInteractor().getCurrentUserName();
          if (currentUserName != null && !currentUserName.isEmpty()) {
              getMvpView().updateUserName(currentUserName);
          }
  
          final String currentUserEmail = getInteractor().getCurrentUserEmail();
          if (currentUserEmail != null && !currentUserEmail.isEmpty()) {
              getMvpView().updateUserEmail(currentUserEmail);
          }
  
          final String profilePicUrl = getInteractor().getCurrentUserProfilePicUrl();
          if (profilePicUrl != null && !profilePicUrl.isEmpty()) {
              getMvpView().updateUserProfilePic(profilePicUrl);
          }
      }
  
      @Override
      public void onDrawerRateUsClick() {
          getMvpView().closeNavigationDrawer();
          getMvpView().showRateUsDialog();
      }
  
      @Override
      public void onDrawerMyFeedClick() {
          getMvpView().closeNavigationDrawer();
          getMvpView().openMyFeedActivity();
      }
  }
  