package com.app.baselibrary.base.app;

import android.app.Activity;
import android.content.Context;

import java.util.Iterator;
import java.util.Stack;


/**
 * Desc: 用于全局管理Activity；
 * Author: pl
 * Date: 2017-04-14 17:05
 */

public class AppManager {

    private static AppManager mAppManager;

    private Stack<Activity> mActivityStack;

    public static AppManager getInstance() {
        if (null == mAppManager) {
            synchronized (AppManager.class) {
                if (null == mAppManager)
                    mAppManager = new AppManager();
            }
        }
        return mAppManager;
    }


    private AppManager() {
        mActivityStack = new Stack<>();
    }


    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (!mActivityStack.contains(activity)) {
            mActivityStack.add(activity);
        }
    }

    /**
     * 获取栈顶Activity
     */
    public Activity getTopActivity() {
        Activity activity = mActivityStack.lastElement();
        return activity;
    }

    /**
     * 结束栈顶Activity
     */
    public void killTopActivity() {
        Activity activity = mActivityStack.lastElement();
        killActivity(activity);
    }

    /**
     * 结束指定的Activity  结束某个activity时当前界面不能用 finish()
     */
    public void killActivity(Activity activity) {
        if (activity != null && mActivityStack.contains(activity)) {
            mActivityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 移除栈内空间
     */
    public void removeActivity(Activity activity) {
        if (activity != null && mActivityStack.contains(activity)) {
            mActivityStack.remove(activity);
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity  结束某个activity时当前界面不能用 finish()
     */
    public void killActivity(Class<?> cls) {
        try {
            Iterator<Activity> li = mActivityStack.iterator();
            while (li.hasNext()) {
                Activity activity = li.next();
                if (activity != null && activity.getClass().equals(cls)) {
                    li.remove();
                    activity.finish();
                    activity = null;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 结束所有Activity
     */
    public void killAllActivity() {
        Iterator<Activity> li = mActivityStack.iterator();
        while (li.hasNext()) {
            Activity activity = li.next();
            if (activity != null) {
                li.remove();
                activity.finish();
                activity = null;
            }
        }
        mActivityStack.clear();
    }

    /**
     * 结束除指定Activity的其他所有Activity
     */
    public void killOtherActivity(Class<?> cls) {
        Iterator<Activity> li = mActivityStack.iterator();
        while (li.hasNext()) {
            Activity activity = li.next();
            if (activity != null && !activity.getClass().equals(cls)) {
                li.remove();
                activity.finish();
                activity = null;
            }
        }
    }

    /**
     * 退出应用程序
     */
    @SuppressWarnings("deprecation")
    public void AppExit(Context context) {
        try {
            killAllActivity();
            android.app.ActivityManager activityMgr = (android.app.ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {

        }
    }
}
