/*
 * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
 */
package kr.co.zumo.app.lifeplus.view.presenter;

import android.support.v7.app.ActionBar;
import android.util.Log;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import kr.co.zumo.app.lifeplus.activity.ActivityStarter;
import kr.co.zumo.app.lifeplus.application.App;
import kr.co.zumo.app.lifeplus.model.BlankModel;
import kr.co.zumo.app.lifeplus.network.NetworkWatcher;
import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
import kr.co.zumo.app.lifeplus.view.Event;
import kr.co.zumo.app.lifeplus.view.INetworkErrorView;
import kr.co.zumo.app.lifeplus.view.command.ActivityChangeCommand;
import kr.co.zumo.app.lifeplus.view.command.ExitCommand;

/**
 * NetworkErrorPresenter
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 21.]   [최초 작성]
 * @since 2018. 9. 21.
 */
public class NetworkErrorPresenter extends Presenter<BlankModel, INetworkErrorView> {

  Disposable disposable;
  ActivityStarter starter;

  public NetworkErrorPresenter(BlankModel model, INetworkErrorView view) {
    super(model, view);
  }

  @Override
  protected void startInternal() {

  }

  @Override
  protected void stopInternal() {

  }

  @Override
  protected void resumeInternal() {

  }

  @Override
  protected void pauseInternal() {

  }

  @Override
  protected void destroyInternal() {
    if (null != disposable) {
      disposable.dispose();
      disposable = null;
    }
    if (null != starter) {
      starter.dispose();
      starter = null;
    }
  }

  @Override
  public boolean onBackPressed() {
    onCommand(new ExitCommand());
    return true;
  }

  @Override
  protected void onEventInternal(Event event) {
    switch (event.getEventId()) {
      case Event.RETRY:
        retry();
        break;
      default:
        break;
    }
  }

  private void retry() {
    /**
     * 네트워크 연결 다시 시도
     * - 연결됐다면 메인으로 이동
     */
    disposable = NetworkWatcher.check(App.getInstance().getContext())
      .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
      .subscribe(isConnected -> {
      Log.i("APP# NetworkDisconnectedFragmentPresenter | checkNetworkConnection", "| isConnected: " + isConnected);
      if (isConnected) {
        // 메인으로 이동
        starter = new ActivityStarter(view.getActivity());
        starter.launch(true);
      }
      else {
        // 그냥 그대로 있음;
      }
    }, Throwable::printStackTrace);
  }

  /***********************************
   * listener
   ***********************************/
  @Override
  public void onChangedConnection(boolean isConnected, int type) {
    Log.i("APP# Presenter | onChangedConnection", "| " + this.getClass().getSimpleName() + ", isConnected:" + isConnected);

    if (true == isConnected) {
      onCommand(new ActivityChangeCommand(ScreenID.ACTIVITY_MAIN, ScreenID.DIRECTION_NEXT));
    }
  }

  @Override
  public void onResult(Event event) {

  }

  @Override
  public void onClickTitle(ActionBar actionBar) {

  }

  @Override
  public void onClickSearch(ActionBar actionBar) {

  }

  @Override
  public void onClickMenu(ActionBar actionBar) {

  }

  @Override
  public void onClickLike(ActionBar actionBar) {

  }

  @Override
  public void onClickBookmark(ActionBar actionBar) {

  }

  @Override
  public void onClickShare(ActionBar actionBar) {

  }
}
