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

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.view.fragment.factory.FragmentFactory;

/**
 * 화면 전환을 관리하는 클래스
 * <pre>
 * - Fragment 전환
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 5.]   [최초 작성]
 * @since 2018. 9. 5.
 */
public class FragmentChanger extends ScreenChanger{

  @IdRes
  private int containerId;
  private AppCompatActivity activity;
  private FragmentFactory fragmentFactory;

  public FragmentChanger(AppCompatActivity activity, @IdRes int containerId, FragmentFactory fragmentFactory) {
    this.activity = activity;
    this.containerId = containerId;
    this.fragmentFactory = fragmentFactory;
  }

  /**
   * Fragment 를 전환한다.
   * - 전달되는 FragmentTransaction 에 따라서 화면 전환 효과를 달리할 수 있음
   *
   * @param id                  ScreenID.LOGIN
   * @param fragmentTransaction FragmentTransaction
   */
  private void changeFragment(@ScreenID.FragmentID int id, FragmentTransaction fragmentTransaction) {
    Log.i("APP# FragmentChanger | changeFragment", "| screen id: " + id);

    Fragment fragment = fragmentFactory.getFragment(id);
    fragmentTransaction.replace(containerId, fragment);

    fragmentTransaction.commitAllowingStateLoss();
  }

  /**
   * 스크린 전환을 효과 없이 실행
   *
   * @param id ScreenID.LOGIN
   */
  public void changeTo(@ScreenID.FragmentID int id) {

    FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.none, R.anim.none);

    changeFragment(id, fragmentTransaction);
  }

  /**
   * 스크린 전환을 다음으로 진행 효과를 주면서 실행
   *
   * @param id ScreenID.LOGIN
   */
  public void changeToNext(@ScreenID.FragmentID int id) {

    FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit);

    changeFragment(id, fragmentTransaction);

  }

  /**
   * 스크린 전환을 이전으로 진행 효과를 주면서 실행
   *
   * @param id ScreenID.LOGIN
   */
  public void changeToBack(@ScreenID.FragmentID int id) {

    FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.back_enter, R.anim.back_exit);

    changeFragment(id, fragmentTransaction);

  }

}
