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

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.WindowManager;

import io.reactivex.disposables.CompositeDisposable;
import kr.co.zumo.app.lifeplus.manager.ActionBarManager;
import kr.co.zumo.app.lifeplus.manager.IActionBarListener;
import kr.co.zumo.app.lifeplus.model.Model;
import kr.co.zumo.app.lifeplus.model.ModelProvider;
import kr.co.zumo.app.lifeplus.network.NetworkReceiver;
import kr.co.zumo.app.lifeplus.supervisor.DialogHelper;
import kr.co.zumo.app.lifeplus.view.IView;
import kr.co.zumo.app.lifeplus.view.presenter.Presenter;

/**
 * ActivityBase
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 5.]   [최초 작성]
 * @since 2018. 9. 5.
 */
public abstract class ActivityBase<P extends Presenter> extends AppCompatActivity implements IView, IActionBarListener {

  NetworkReceiver networkReceiver;
  CompositeDisposable disposable = new CompositeDisposable();
  P presenter;
  Model model;

  /***********************************
   * Activity
   ***********************************/

  @Override
  protected final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w("APP# ActivityBase | onCreate", "| >>>>>>>>>>>>>> " + this.getClass().getSimpleName());
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    DialogHelper.getInstance().setFragmentManager(getSupportFragmentManager());

    ActionBarManager.getInstance().init(this, this.getSupportActionBar());
    ActionBarManager.getInstance().setListener(this);
    defineActionBar();

    presenter = definePresenter();
    if (null != presenter) {
      model = presenter.getModel();
    }
    onAfterCreate(savedInstanceState);

  }

  @CallSuper
  @Override
  protected void onDestroy() {
    Log.w("APP# ActivityBase | onDestroy", "| <<<<<<<<<<<<< " + this.getClass().getSimpleName());

    if (null != networkReceiver) {
      networkReceiver.dispose();
      networkReceiver = null;
    }

    if (null != disposable) {
      disposable.dispose();
      disposable = null;
    }

    if (null != presenter) {
      presenter.destroy();
      presenter = null;
    }

    super.onDestroy();
  }

  @Override
  protected void onPause() {
//    Log.e("APP#  ActivityBase | onPause", "|" + this.getClass().getSimpleName());
    if (null != presenter) {
      presenter.pause();
    }
    if (null != model) {
      model.pause();
    }

    super.onPause();
  }

  @Override
  protected void onStart() {
//    Log.e("APP#  ActivityBase | onStart", "|" + this.getClass().getSimpleName());
    if (null != presenter) {
      presenter.start();
    }
    if (null != model) {
      model.start();
    }

    super.onStart();
  }

  @Override
  protected void onResume() {
//    Log.e("APP#  ActivityBase | onResume", "|" + this.getClass().getSimpleName());
    if (null != presenter) {
      presenter.resume();
    }
    if (null != model) {
      model.resume();
    }

    super.onResume();
  }

  @Override
  protected void onStop() {
//    Log.e("APP#  ActivityBase | onStop", "|" + this.getClass().getSimpleName());
    if (null != presenter) {
      presenter.stop();
    }
    if (null != model) {
      model.stop();
    }

    super.onStop();
  }

  private Presenter getModelPresenter() {
    Presenter presenter = null;
    if (null != model) {
      presenter = model.getPresenter();
    }
    return presenter;
  }

  private boolean exitToBack() {
    Presenter presenter = getModelPresenter();
    if (null != presenter) {
      return presenter.onBackPressed();
    }
    return false;
  }

  /**
   * Device back key
   */
  @Override
  public void onBackPressed() {
    if (exitToBack() == false) {
      super.onBackPressed();
    }
  }

  /**
   * actionbar item 클릭 시 이벤트 처리
   *
   * @param item
   * @return
   */
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        onBackPressed();
        break;
      default:
        return false;
    }

    return true;
  }


  /***********************************
   * ActivityBase
   ***********************************/

  /**
   * onCreate() 처리 후 호출된다
   *
   * @param savedInstanceState
   */
  abstract void onAfterCreate(Bundle savedInstanceState);

  /**
   * ActionBar 를 정의 해준다.
   */
  abstract void defineActionBar();

  /**
   * presenter 를 지정한다.
   */
  abstract P definePresenter();

  /**
   * Android life-cycle 에 귀속되는 모델을 반환한다.
   * - 사실 MVVM 사용되는 ViewModel 이지만 MVP 의 Model 로 사용한다.
   * - Activity 의 종속성을 이용하여 Model 을 참조하기 위함
   *
   * @param modelClass MainModel.class
   * @return new MainModel()
   */
  protected <T extends Model> T getModel(Class<T> modelClass) {
    return ModelProvider.getModel(this, modelClass);
  }

  /**
   * 해당 모델 클리어.
   * - Model.onCleared() 호출됨
   *
   * @param modelClass
   */
  protected void clearModel(Class modelClass) {
    ModelProvider.clearModel(this, modelClass);
  }

  /***********************************
   * IView
   ***********************************/
  @Override
  public final Activity getActivity() {
    return this;
  }

  /***********************************
   * IActionBarListener
   ***********************************/
  @Override
  public void onClickBack(ActionBar actionBar) {
    Presenter presenter = getModelPresenter();
    if (null != presenter) {
      presenter.onClickBack(actionBar);
    }
  }

  @Override
  public void onClickTitle(ActionBar actionBar) {
    Presenter presenter = getModelPresenter();
    if (null != presenter) {
      presenter.onClickTitle(actionBar);
    }
  }

  @Override
  public void onClickSearch(ActionBar actionBar) {
    Log.i("APP# ActivityBase | onClickSearch", "|" + "=====================================");
    Presenter presenter = getModelPresenter();
    if (null != presenter) {
      presenter.onClickSearch(actionBar);
    }
  }

  @Override
  public void onClickMenu(ActionBar actionBar) {
    Log.i("APP# ActivityBase | onClickMenu", "|" + "=====================================");
    Presenter presenter = getModelPresenter();
    if (null != presenter) {
      presenter.onClickMenu(actionBar);
    }
  }

  @Override
  public void onClickLike(ActionBar actionBar) {
    Presenter presenter = getModelPresenter();
    if (null != presenter) {
      presenter.onClickLike(actionBar);
    }
  }

  @Override
  public void onClickBookmark(ActionBar actionBar) {
    Presenter presenter = getModelPresenter();
    if (null != presenter) {
      presenter.onClickBookmark(actionBar);
    }
  }

  @Override
  public void onClickShare(ActionBar actionBar) {
    Presenter presenter = getModelPresenter();
    if (null != presenter) {
      presenter.onClickShare(actionBar);
    }
  }
}

