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

import android.util.Log;

import kr.co.zumo.app.lifeplus.model.Model;
import kr.co.zumo.app.lifeplus.supervisor.FragmentChanger;
import kr.co.zumo.app.lifeplus.supervisor.ScreenChanger;
import kr.co.zumo.app.lifeplus.supervisor.ScreenChangerHelper;
import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
import kr.co.zumo.app.lifeplus.view.IView;
import kr.co.zumo.app.lifeplus.view.fragment.factory.FragmentFactory;
import kr.co.zumo.app.lifeplus.view.presenter.Presenter;

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

  int fragmentId;
  int direction;

  public FragmentChangeCommand(@ScreenID.FragmentID int fragmentId, @ScreenID.Direction int direction) {
    this.fragmentId = fragmentId;
    this.direction = direction;
  }

  @Override
  public void preExecute(Model model, IView view, Presenter presenter) {

  }

  @Override
  public void execute(Model model, IView view, Presenter presenter) {
    FragmentFactory factory = model.getFragmentFactory();
    ScreenChangerHelper helper = ScreenChangerHelper.getInstance();
    ScreenChanger screenChanger = new FragmentChanger(helper.getAppCompatActivity(), helper.getContainerId(), factory);
    Log.i("APP# FragmentChangeCommand | execute", "| fragment factory: " + factory.getClass().getSimpleName());

    switch (direction) {
      case ScreenID.DIRECTION_NEXT:
        presenter.destroy();
        screenChanger.changeToNext(fragmentId);
        break;
      case ScreenID.DIRECTION_BACK:
        presenter.destroy();
        screenChanger.changeToBack(fragmentId);
        break;
      case ScreenID.DIRECTION_NONE:
        presenter.destroy();
        screenChanger.changeTo(fragmentId);
        break;
      default:
        throw new RuntimeException("unsupported direction");
    }
  }

  @Override
  public void postExecute(Model model, IView view, Presenter presenter) {

  }

  @Override
  public void dispose() {
  }
}
