package kr.co.hanwha.hifive;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import kr.co.hanwha.hifive.enums.ApplicationStatus;

/**
 * Created by sanghoonkwon on 2018. 1. 10..
 */

public class HFApplication extends Application {
    /**************************************************
     * Activity
     **************************************************/
    private ApplicationStatus mAppStatus = ApplicationStatus.FOREGROUND;

    /**
     * 실행중인 Activity Count
     */
    private static int mActivityCount;

    /**
     * 실행중인 Activity
     */
    private static Activity mRunningActivity;
    /**
     * 마지막으로 Pause된 Activity
     */
    private static Activity mLastActivity;
    /**
     * 액티비티 감시객체
     */
    private ActivityLifecycleCallbacks activityLifecycleCallbacks;

    public static int getActivityCount() {
        return mActivityCount;
    }

    public static Activity getRunningActivity() {
        return mRunningActivity;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        //엑티비티 감시 객체
        activityLifecycleCallbacks = new MyActivityLifecycleCallbacks();
        registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    /**
     * Activity Lifecycle 감시용
     */
    private class MyActivityLifecycleCallbacks implements
            ActivityLifecycleCallbacks {

        // running activity count
        private int running = 0;

        private void log(String methodName, Activity activity) {
            final String activityName = activity.getClass().getSimpleName();
            final int taskId = activity.getTaskId();
        }

        @Override
        public void onActivityCreated(Activity activity,
                                      Bundle savedInstanceState) {
            log("onActivityCreated", activity);
            mActivityCount++;
        }

        @Override
        public void onActivityStarted(Activity activity) {
            log("onActivityStarted", activity);
            mRunningActivity = activity;

            if (++running == 1) {
                // running activity is 1,
                // app must be returned from background just now (or first launch)
                mAppStatus = ApplicationStatus.RETURNED_TO_FOREGROUND;
            } else if (running > 1) {
                // 2 or more running activities,
                // should be foreground already.
                mAppStatus = ApplicationStatus.FOREGROUND;
            }
        }

        @Override
        public void onActivityResumed(Activity activity) {
            log("onActivityResumed", activity);
            mRunningActivity = activity;

            if (activity.equals(mLastActivity)) {
//                if (activity instanceof IntroActivity ||
//                        activity instanceof LoginActivity ||
//                        activity instanceof AuthNumberActivity ||
//                        activity instanceof CheckAuthNumberActivity) {
//                    //  ...Add... 체크 할필요 없는 Activity 추가
//                    return;
//                }

                /**
                 * 시작할때 암호창 띄운다
                 */
                try {
//                    GWActivityUtil.startCheckAuthNumberActivity(activity);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

        @Override
        public void onActivityPaused(Activity activity) {
            log("onActivityPaused", activity);
            mLastActivity = activity;
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity,
                                                Bundle outState) {
            log("onActivitySaveInstanceState", activity);
        }

        @Override
        public void onActivityStopped(Activity activity) {
            log("onActivityStopped", activity);

            if (--running == 0) {
                // no active activity
                // app goes to background
                mAppStatus = ApplicationStatus.BACKGROUND;
            }
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            log("onActivityDestroyed", activity);
            mActivityCount--;
            if (mActivityCount == 0) {
                mRunningActivity = null;
            }
        }
    }
}
