package com.sobot.widget.myappliction;

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

import com.sobot.utils.SobotLogUtils;

import java.lang.ref.WeakReference;

/**
 * app 管理类
 * 可判断 app被系统回收(强杀)状态；app是否前台
 */
public class SobotActivityLifeCycle implements Application.ActivityLifecycleCallbacks {
    //默认被初始化状态，被系统回收(强杀)状态
    public int mAppStatus = SobotAppStatusConstant.STATUS_FORCE_KILLED;
    private boolean mPaused = true;
    private boolean mForeground = false;
    private static SobotActivityLifeCycle sInstance;
    //当前Activity的弱引用
    private WeakReference<Activity> mActivityReference;

    protected final String TAG = "SobotActivityLifeCycle";

    public static final int ACTIVITY_ON_RESUME = 0;
    public static final int ACTIVITY_ON_PAUSE = 1;

    //打开的Activity数量统计
    private int activityStartCount = 0;


    private SobotActivityLifeCycle() {
    }

    public static synchronized SobotActivityLifeCycle getInstance() {
        if (sInstance == null) {
            sInstance = new SobotActivityLifeCycle();
        }
        return sInstance;
    }

    /**
     * 获取APP状态
     *
     * @return
     */
    public int getAppStatus() {
        return mAppStatus;
    }

    /**
     * 设置APP状态
     *
     * @param appStatus
     */
    public void setAppStatus(int appStatus) {
        this.mAppStatus = appStatus;
    }

    public Activity getCurrentActivity() {
        if (mActivityReference != null) {
            return mActivityReference.get();
        }
        return null;
    }

    public boolean isForground() {
        return mForeground;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        Log.d(TAG, activity.getLocalClassName() + " " + "onActivityCreated");
        mActivityReference = new WeakReference<Activity>(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        Log.d(TAG, activity.getLocalClassName() + " " + "onActivityStarted");
        activityStartCount++;
        //数值从0变到1说明是从后台切到前台
        if (activityStartCount == 1) {
            //从后台切到前台
            SobotLogUtils.i("从后台切到前台");
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
        String activityName = activity.getClass().getName();
        mPaused = false;
        mForeground = true;
        mActivityReference = new WeakReference<Activity>(activity);
    }

    @Override
    public void onActivityPaused(Activity activity) {
        //pause事件后是否在前台要分情况判断
        mPaused = true;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        Log.d(TAG, activity.getLocalClassName() + " " + "onActivityStopped");
        activityStartCount--;
        //数值从1到0说明是从前台切到后台
        if (activityStartCount == 0) {
            //从前台切到后台
            SobotLogUtils.i("从前台切到后台");
        }

    }

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

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        Log.d(TAG, activity.getLocalClassName() + " " + "onActivityDestroyed");
    }
}
