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

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import com.adealink.frame.base.fastLazy
import com.adealink.frame.util.ActivityLifecycleCallbacksExt
import com.adealink.frame.util.AppUtil

object WindowManagerProxy : ActivityLifecycleCallbacksExt {

    private val MODE_WINDOW_MANAGER: BaseWindowManager by fastLazy { ModeWindowManagerProxy() }

    private val SCREEN_RECEIVER = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            when (intent?.action) {
                Intent.ACTION_SCREEN_OFF -> {
                    notifyScreenOff()
                }
                Intent.ACTION_SCREEN_ON -> {
                    notifyScreenOn()
                }
            }
        }
    }

    fun getWindowManager(): BaseWindowManager = MODE_WINDOW_MANAGER

    fun init() {
        registerApplicationLifeCycle()
    }

    private fun registerApplicationLifeCycle() {
        AppUtil.registerActivityLifecycleCallbacks(this)

        val intentFilter = IntentFilter()
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
        intentFilter.addAction(Intent.ACTION_SCREEN_ON)
        AppUtil.appContext.registerReceiver(SCREEN_RECEIVER, intentFilter)
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        super.onActivityCreated(activity, savedInstanceState)
        MODE_WINDOW_MANAGER.notifyWhenActivityChanged(activity)
        MODE_WINDOW_MANAGER.onCreate(activity)
    }

    override fun onActivityStarted(activity: Activity) {
        super.onActivityStarted(activity)
        MODE_WINDOW_MANAGER.notifyWhenActivityChanged(activity)
        MODE_WINDOW_MANAGER.onStart(activity)
    }

    override fun onActivityResumed(activity: Activity) {
        super.onActivityResumed(activity)
        MODE_WINDOW_MANAGER.notifyWhenActivityChanged(activity)
        MODE_WINDOW_MANAGER.onResume(activity)
    }

    override fun onActivityPaused(activity: Activity) {
        super.onActivityPaused(activity)
        MODE_WINDOW_MANAGER.onPause(activity)
    }

    override fun onActivityStopped(activity: Activity) {
        super.onActivityStopped(activity)
        MODE_WINDOW_MANAGER.onStop(activity)
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        super.onActivitySaveInstanceState(activity, outState)
        MODE_WINDOW_MANAGER.onSaveInstanceState(activity)
    }

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

    override fun onEnterForeGround() {
        super.onEnterForeGround()
        MODE_WINDOW_MANAGER.onEnterForeground()
    }

    override fun onEnterBackGround() {
        super.onEnterBackGround()
        MODE_WINDOW_MANAGER.onEnterBackground()
    }

    private fun notifyScreenOn() {
        MODE_WINDOW_MANAGER.onScreenOn()
    }

    private fun notifyScreenOff() {
        MODE_WINDOW_MANAGER.onScreenOff()
    }

}