package com.convenient.android.common.base

import android.app.Activity
import android.os.Build
import com.convenient.android.common.base.view.BaseActivity
import com.orhanobut.logger.Logger
import kotlin.system.exitProcess


/**
 *    @author : hubowen
 *    @date   : 2019-12-23  15:23
 *    @description：
 */
object ActivitysUtils {
    private val mLock = Object()
    private val activitys = mutableListOf<Activity>()

    fun size() = activitys.size

    fun current() = synchronized(mLock) {
        clearNotAliveActivitys().elementAtOrNull(0)
    }

    fun add(activity: Activity?) {
        synchronized(mLock) {
            if (isActivityAlive(activity)) clearNotAliveActivitys().add(0, activity!!)
        }
    }

    fun remove(activity: Activity?) {
        synchronized(mLock) {
            clearNotAliveActivitys()
                .filter { !isEqual(it.javaClass, activity?.javaClass) }
                .run {
                    activitys.clear()
                    activitys.addAll(this)
                }
        }
    }

    fun findActivity(cls: Class<*>) = synchronized(mLock) {
        clearNotAliveActivitys()
            .filter { isEqual(it.javaClass, cls) }
            .elementAtOrNull(0)
    }

    fun finish(vararg cls: Class<*>) {
        synchronized(mLock) {
            val others = mutableListOf<Activity>()
            clearNotAliveActivitys()
                .apply {
                    forEach {
                        when {
                            cls.any { c -> isEqual(it.javaClass, c) } -> it.finish()
                            else -> others.add(it)
                        }
                    }
                    clear()
                    addAll(others)
                }
        }
    }

    fun finishOtherActivities(cls: Class<*>) {
        synchronized(mLock) {
            val current = mutableListOf<Activity>()
            clearNotAliveActivitys()
                .apply {
                    forEach {
                        when {
                            isEqual(it.javaClass, cls) && (0 == current.size) -> current.add(it)
                            else -> it.finish()
                        }
                    }
                    clear()
                    addAll(current)
                }
        }
    }

    fun finishAllActivitys() {
        synchronized(mLock) {
            clearNotAliveActivitys().apply {
                forEach { it.finish() }
                clear()
            }
            exitProcess(0)
        }
    }

    private fun isEqual(old: Class<*>?, new: Class<*>?) = when {
        (null == old) || (null == new) -> false
        else -> old.simpleName == new.simpleName
    }

    private fun clearNotAliveActivitys() = activitys.filter {
        isActivityAlive(it)
    }.run {
        activitys.clear()
        activitys.addAll(this)
        activitys
    }

    fun isActivityAlive(activity: Activity?) = when (activity) {
        null -> false
        else -> !activity.isFinishing && (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 || !activity.isDestroyed)
    }

    fun BaseActivity.refreshApp() {
        //杀掉以前进程
        Logger.t("ActivityUtils").e("refreshApp")
        finishAllActivitys()
    }
}