package kr.co.hanwha.hifive;

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

import kr.co.hanwha.hifive.enums.ApplicationStatus;
import kr.co.hanwha.hifive.screen.intro.AuthNumberActivity;
import kr.co.hanwha.hifive.screen.intro.CheckAuthNumberActivity;
import kr.co.hanwha.hifive.screen.intro.IntroActivity;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;

/**
 * Application<br>
 * 서버 설정(PRD, DEV, LOCAL)<br>
 * 화면 잠금(간편 비밀번호 확인) 처리 로직<br>
 * Created by sanghoonkwon on 2018. 1. 10..
 */

public class HFApplication extends Application {
    /**
     * 서버 설정
     */
    public enum SERVER_TYPE {
        /**
         * PRD(실서버)
         */
        REAL,
        /**
         * 개발 서버
         */
        DEV,
        /**
         * 테스트 Local URL
         */
        LOCAL
    }

    /**
     * 개발 로그 옵션(UI 화면내에 표시)
     */
    public static boolean DEV_LOG = false;

    public static SERVER_TYPE SERVER = SERVER_TYPE.REAL;

    /**************************************************
     * 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;

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

        @Override
        public void onActivityStarted(Activity 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) {
            mRunningActivity = activity;

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

                /**
                 * 시작할때 암호창 띄운다
                 */
                try {
                    // Check Login
                    // Check Simple Login
                    if (HFInfoUtil.checkAuthTime(getApplicationContext())) {
                        HFActivityUtil.startCheckAuthNumberActivity(activity);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

        @Override
        public void onActivityPaused(Activity activity) {
            mLastActivity = activity;
            HFInfoUtil.setLastCheckAuthTime(getApplicationContext());
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            if (--running == 0) {
                // no active activity
                // app goes to background
                mAppStatus = ApplicationStatus.BACKGROUND;
            }
        }

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