package blog.wiwi.hoops

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.ComponentActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat

class MainActivity : ComponentActivity() {

    private lateinit var webView: WebView

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        // 必須在 super.onCreate 之前呼叫
        // 在 Android 12+ 接管系統 splash；舊版會用 backport 模擬同樣行為
        installSplashScreen()

        super.onCreate(savedInstanceState)

        // 沉浸式全螢幕：把狀態列和導航列隱藏，內容填滿整個螢幕
        WindowCompat.setDecorFitsSystemWindows(window, false)
        WindowInsetsControllerCompat(window, window.decorView).apply {
            hide(WindowInsetsCompat.Type.systemBars())
            systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }

        // 玩遊戲時不要讓螢幕睡著
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        // 處理瀏海（劉海螢幕）：讓內容延伸到瀏海下方
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            window.attributes = window.attributes.apply {
                layoutInDisplayCutoutMode =
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            }
        }

        webView = WebView(this).apply {
            // 禁用長按選單、禁用文字選取
            isLongClickable = false
            setOnLongClickListener { true }
            isHapticFeedbackEnabled = false

            // 不要捲動條、不要過捲動效果
            isVerticalScrollBarEnabled = false
            isHorizontalScrollBarEnabled = false
            overScrollMode = View.OVER_SCROLL_NEVER

            settings.apply {
                javaScriptEnabled = true
                domStorageEnabled = true
                // 媒體（音訊）不需要使用者手勢就能播放
                mediaPlaybackRequiresUserGesture = false
                // 只用 app 內的本地檔案，不允許讀取外部檔案系統
                allowFileAccess = false
                allowContentAccess = false
                cacheMode = WebSettings.LOAD_NO_CACHE
                // 縮放：完全關閉
                setSupportZoom(false)
                builtInZoomControls = false
                displayZoomControls = false
                // 雜七雜八的關閉
                javaScriptCanOpenWindowsAutomatically = false
                setSupportMultipleWindows(false)
                // 字型大小不要被系統字級設定影響（避免 UI 跑掉）
                textZoom = 100
            }

            webViewClient = WebViewClient()
        }

        setContentView(webView)
        webView.loadUrl("file:///android_asset/index.html")
    }

    override fun onPause() {
        super.onPause()
        webView.onPause()
    }

    override fun onResume() {
        super.onResume()
        webView.onResume()
    }

    override fun onDestroy() {
        webView.destroy()
        super.onDestroy()
    }
}
