package com.doverfuelingsolutions.issp.utils

import android.app.Activity
import android.view.View
import android.view.Window
import androidx.core.os.HandlerCompat

class WindowUtil {

    companion object {

        @Suppress("DEPRECATION")
        fun setFullscreen(activity: Activity) {
            makeFullscreen(activity.window)

            activity.window.decorView.setOnSystemUiVisibilityChangeListener {
                makeFullscreen(activity.window)
            }
        }

        @Suppress("DEPRECATION")
        fun setFullscreen(window: Window) {
            makeFullscreen(window)

            window.decorView.setOnSystemUiVisibilityChangeListener {
                makeFullscreen(window)
            }
        }

        @Suppress("DEPRECATION")
        private fun makeFullscreen(window: Window) {
            val systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            window.decorView.systemUiVisibility = systemUiVisibility
            HandlerCompat.createAsync(window.context.mainLooper).postDelayed({
                if (window.isActive)
                    window.decorView.systemUiVisibility = systemUiVisibility
            }, 1000)
        }
    }
}