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

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import io.reactivex.disposables.CompositeDisposable;
import kr.co.zumo.app.lifeplus.model.Model;
import kr.co.zumo.app.lifeplus.model.ModelProvider;
import kr.co.zumo.app.lifeplus.view.IView;
import kr.co.zumo.app.lifeplus.view.presenter.Presenter;

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

  protected CompositeDisposable disposable = new CompositeDisposable();

  protected P presenter;

  /***********************************
   * Fragment
   ***********************************/

  @Nullable
  @Override
  public abstract View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

  @Override
  public final void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    presenter = definePresenter();
    onAfterActivityCreated(savedInstanceState);

  }

  @Override
  public void onStart() {
    presenter.start();
    super.onStart();
  }

  @Override
  public void onResume() {
    presenter.resume();
    super.onResume();
  }

  @Override
  public void onPause() {
    presenter.pause();
    super.onPause();
  }

  @Override
  public void onStop() {
    presenter.stop();
    super.onStop();
  }

  @CallSuper
  @Override
  public void onDestroyView() {
    Log.w("APP# FragmentBase | onDestroyView", "| <<<<<<<<<<<<< " + this.getClass().getSimpleName());

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

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

    super.onDestroyView();
  }

  /***********************************
   * FragmentBase
   ***********************************/

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

  /**
   * presenter 를 지정한다.
   */
  protected 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(getActivity(), modelClass);
  }

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

  /***********************************
   * IView
   ***********************************/
}
