package com.rdiot.yx485.base

import androidx.appcompat.app.AppCompatActivity
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.system.exitProcess

/**
 *
 * @author mR2hao
 * @date 2022/3/15
 */
@Singleton
class AppManager @Inject constructor() {
    init {
        instance = this
    }

    /**
     * 添加Activity到堆栈
     */
    fun addActivity(activity: AppCompatActivity?) {
        activityStack.add(activity)
    }

    /**
     * 获取栈顶Activity（堆栈中最后一个压入的）
     */
    fun getTopActivity(): AppCompatActivity {
        return activityStack.lastElement()
    }

    /**
     * 结束栈顶Activity（堆栈中最后一个压入的）
     */
    fun finishTopActivity() {
        val activity: AppCompatActivity = activityStack.lastElement()
        finishActivity(activity)
    }

    /**
     * 结束指定类名的Activity
     *
     * @param cls
     */
    fun finishActivity(cls: Class<*>?) {
        val iterator: MutableIterator<*> = activityStack.iterator()
        while (iterator.hasNext()) {
            val activity: AppCompatActivity = iterator.next() as AppCompatActivity
            if (activity::class.java == cls) {
                iterator.remove()
                activity.finish()
            }
        }
    }

    /**
     * 结束到指定类名的Activity
     *
     * @param cls
     */
    fun finishToActivity(cls: Class<*>?) {
        var i = 0
        val size: Int = activityStack.size
        while (i < size) {
            val lastActivity = activityStack.lastElement()
            if (lastActivity::class.java != cls) {
                activityStack.remove(lastActivity)
                lastActivity.finish()
            } else {
                return
            }
            i++
        }
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        var i = 0
        val size: Int = activityStack.size
        while (i < size) {
            if (null != activityStack[i]) {
                activityStack[i].finish()
            }
            i++
        }
        activityStack.clear()
    }

    /**
     * 退出应用程序
     */
    fun appExit() {
        try {
            finishAllActivity()
            exitProcess(0)
        } catch (e: Exception) {
        }
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(activity: AppCompatActivity?) {
        activity?.apply {
            activityStack.remove(this)
            finish()
            overridePendingTransition(0, 0)
        }
    }

    /**
     * 得到指定类名的Activity
     */
    fun getActivity(cls: Class<*>?): AppCompatActivity? {
        for (activity in activityStack) {
            if (activity::class.java == cls) {
                return activity
            }
        }
        return null
    }

    fun recreateAllOtherActivity(activity: AppCompatActivity) {
        var i = 0
        val size: Int = activityStack.size
        while (i < size) {
            if (null != activityStack[i] && activityStack[i] !== activity) {
                activityStack[i].recreate()
            }
            i++
        }
    }

    companion object {
        private var activityStack: Stack<AppCompatActivity> = Stack()

        fun getActivityStack(): Stack<AppCompatActivity> {
            return activityStack
        }


        /**
         * 单一实例
         */
        lateinit var instance: AppManager

    }
}