package com.adealink.weparty.commonui.widget.floatview.mode.fix

import android.app.Activity
import com.adealink.frame.log.Log
import com.adealink.weparty.commonui.widget.floatview.BaseWindowManager
import com.adealink.weparty.commonui.widget.floatview.data.FloatWindowType
import com.adealink.weparty.commonui.widget.floatview.data.IFloatData
import com.adealink.weparty.commonui.widget.floatview.data.TAG_FLOAT_VIEW
import com.adealink.weparty.commonui.widget.floatview.view.BaseFloatView
import java.util.concurrent.CopyOnWriteArrayList

/**
 * Android10透明主题有个异常生命周期回调：A Activity启动透明主题Activity B会执行以下生命周期
 * 1、B-onResume  ->  A-onResume -> A-onPause，
 *
 * [注意] BaseWindowManager有新增方法时,一定要记得重写方法
 */
class Android10FixApplicationModeWindowManager(
    private var base: BaseWindowManager
) : BaseWindowManager() {

    private var currentResumeActivity: Activity? = null
    private var currentErrorActivityList = ArrayList<String>()

    override fun removeView(view: BaseFloatView<out IFloatData>, reason: String) {
        base.removeView(view, reason)
    }

    override fun addView(view: BaseFloatView<out IFloatData>) {
        base.addView(view)
    }

    override fun removeFloatViewByType(type: FloatWindowType, reason: String) {
        base.removeFloatViewByType(type, reason)
    }

    override fun setFloatViewVisibility(type: FloatWindowType, visibility: Int) {
        base.setFloatViewVisibility(type, visibility)
    }

    override fun findFloatViewByType(type: FloatWindowType): BaseFloatView<out IFloatData>? {
        return base.findFloatViewByType(type)
    }

    override fun isFloatViewAdded(type: FloatWindowType): Boolean {
        return base.isFloatViewAdded(type)
    }

    override fun onCreate(activity: Activity) {
        base.onCreate(activity)
    }

    override fun onStart(activity: Activity) {
        base.onStart(activity)
    }

    override fun onResume(activity: Activity) {
        if (currentResumeActivity == null) {
            currentResumeActivity = activity
            base.onResume(activity)
        } else {
            currentErrorActivityList.add(activity.toString())
            Log.i(
                TAG_FLOAT_VIEW,
                "error onResume ignore base onResume : ${activity.javaClass.name}"
            )
        }
    }

    override fun onPause(activity: Activity) {
        if (currentResumeActivity != null && currentResumeActivity != activity) {
            if (currentErrorActivityList.contains(activity.toString())) {
                Log.i(
                    TAG_FLOAT_VIEW,
                    "error onPause ignore base onPause : ${activity.javaClass.name}"
                )
            }
        } else if (currentResumeActivity != null && currentResumeActivity == activity) {
            currentResumeActivity = null
            currentErrorActivityList.clear()
            base.onPause(activity)
        } else {
            Log.i(
                TAG_FLOAT_VIEW,
                "unknown onPause activity: ${activity.javaClass.name} : $currentResumeActivity"
            )
            base.onPause(activity)
        }
    }

    override fun onStop(activity: Activity) {
        base.onStop(activity)
    }

    override fun onDestroy(activity: Activity) {
        super.onDestroy(activity)
        base.onDestroy(activity)
    }

    override fun onSaveInstanceState(activity: Activity) {
        base.onSaveInstanceState(activity)
    }

    override fun onScreenOn() {
        base.onScreenOn()
    }

    override fun onScreenOff() {
        base.onScreenOff()
    }

    override fun onEnterBackground() {
        base.onEnterBackground()
    }

    override fun onEnterForeground() {
        base.onEnterForeground()
    }

    override fun onActivityChange(activity: Activity) {
        base.onActivityChange(activity)
    }

    override fun notifyWhenActivityChanged(activity: Activity) {
        base.notifyWhenActivityChanged(activity)
    }

    override fun removeAllFloatViews(type: FloatWindowType, reason: String) {
        base.removeFloatViewByType(type, reason)
    }

    override fun findAllFloatViews(type: FloatWindowType): CopyOnWriteArrayList<BaseFloatView<out IFloatData>> {
        return base.findAllFloatViews(type)
    }

    override fun remove(predicate: (BaseFloatView<out IFloatData>) -> Boolean, reason: String) {
        base.remove(predicate,reason)
    }
}