package com.zaojiao.app

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.graphics.Color
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.zaojiao.app.feat.design.BackgroundTheme
import com.zaojiao.app.feat.design.LocalBackgroundTheme
import com.zaojiao.app.feat.design.LocalOnFinishDispatcher
import com.zaojiao.app.ui.App
import dagger.hilt.android.AndroidEntryPoint

private val LightBackgroundTheme = BackgroundTheme(color = Color.White)

private val DarkBackgroundTheme = BackgroundTheme(color = Color.Black)

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private lateinit var windowInsetsController: WindowInsetsControllerCompat
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        configureSystemBar()
        setContent {
            val systemUiController = rememberSystemUiController()

            val backgroundTheme = if (isSystemInDarkTheme()) {
                DarkBackgroundTheme
            } else {
                LightBackgroundTheme
            }

            CompositionLocalProvider(
                LocalBackgroundTheme provides backgroundTheme,
                LocalOnFinishDispatcher provides { finish() }
            ) {
                MaterialTheme { App() }
            }
        }
    }

    private fun configureSystemBar() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        windowInsetsController = WindowInsetsControllerCompat(window, window.decorView)

        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = android.graphics.Color.TRANSPARENT
        window.navigationBarColor = android.graphics.Color.TRANSPARENT

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            window.addFlags(WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES)
        }
        windowInsetsController.apply {
            show(WindowInsetsCompat.Type.systemBars())
        }
    }
}