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

import android.app.Activity
import android.view.WindowManager
import androidx.annotation.CallSuper
import com.adealink.weparty.commonui.widget.floatview.data.IFloatData
import com.adealink.weparty.commonui.widget.floatview.view.BaseFloatView
import java.lang.ref.WeakReference

abstract class BaseWindowManager : IWindowManager<BaseFloatView<out IFloatData>> {
    var currentActivity: WeakReference<Activity?>? = null

    fun getCurrentWindowManager(): WindowManager? {
        if (currentActivity != null && currentActivity?.get()?.isFinishing == false) {
            return currentActivity?.get()?.windowManager
        }
        return null
    }

    open fun onCreate(activity: Activity) {
    }

    open fun onStart(activity: Activity) {
    }

    open fun onResume(activity: Activity) {
    }

    open fun onPause(activity: Activity) {
    }

    open fun onStop(activity: Activity) {

    }

    @CallSuper
    open fun onDestroy(activity: Activity) {
        if (activity == currentActivity?.get()) {
            currentActivity = null
        }
    }

    open fun onSaveInstanceState(activity: Activity) {

    }

    open fun onScreenOn() {

    }

    open fun onScreenOff() {

    }

    open fun onEnterBackground() {}
    open fun onEnterForeground() {}


    open fun notifyWhenActivityChanged(activity: Activity) {
        if (currentActivity?.get() != activity) {
            currentActivity = WeakReference(activity)
            onActivityChange(activity)
        }
    }

    open fun onActivityChange(activity: Activity) {}

}