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

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;

import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.activity.factory.ActivityFactory;

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

  protected ActivityFactory factory;
  protected Activity activity;

  public ActivityChanger(Activity activity, ActivityFactory factory) {
    this.factory = factory;
    this.activity = activity;
  }

  private void finishAfterTransition() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      activity.finishAfterTransition();
    }
  }

  private TaskStackBuilder getTaskStackBuilder(int id) {
    /**
     * activity back stack 에 현재 액티비티와 다음 액티비티를 세팅한다.
     */
    Class<? extends Activity> className = factory.getActivityClass(id);
    Intent intent = new Intent(activity, className);
    Log.i("APP# ActivityChanger | changeToNext", "| from: " + activity.getClass().getSimpleName() + ", to: " + className.getSimpleName());

    return TaskStackBuilder.create(activity).addNextIntent(new Intent(activity, activity.getClass())).addNextIntent(intent);
  }

  @Override
  public void changeTo(int id) {
    startActivity(id);
    activity.overridePendingTransition(R.anim.none, R.anim.none);
    finishAfterTransition();

//    getTaskStackBuilder(id).startActivities();
//    activity.overridePendingTransition(R.anim.none, R.anim.none);
//    finishAfterTransition();
  }

  @Override
  public void changeToNext(int id) {
    startActivity(id);
    activity.overridePendingTransition(R.anim.enter, R.anim.exit);
    finishAfterTransition();

//    getTaskStackBuilder(id).startActivities();
//    activity.overridePendingTransition(R.anim.enter, R.anim.exit);
//    finishAfterTransition();
  }

  @Override
  public void changeToBack(int id) {
    startActivity(id);
    activity.overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
    finishAfterTransition();

//    activity.finish();
//    activity.overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
  }

  private void startActivity(int id) {
    Intent intent = new Intent(activity, factory.getActivityClass(id));
//    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    activity.startActivity(intent);
  }
}
