package com.gh.gamecenter.setting.compose.base

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.view.WindowCompat
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.DarkModeUtils
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.setting.compose.R
import com.gh.gamecenter.setting.compose.ui.theme.AssistantandroidTheme

abstract class ComposeBaseActivity : AppCompatActivity() {
    private var mOldUiMode = 0

    protected var mEntrance = ""
    protected var mIsDarkModeOn by mutableStateOf(false)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mIsDarkModeOn = DarkModeUtils.isDarkModeOn(this)
        mOldUiMode = resources.configuration.uiMode

        mEntrance = intent.getStringExtra(EntranceConsts.KEY_ENTRANCE) ?: ""
        WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars = !mIsDarkModeOn
        setContent {
            AssistantandroidTheme(mIsDarkModeOn) {
                Content()
            }
        }
    }

    @Composable
    abstract fun Content()

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (mOldUiMode == newConfig.uiMode) {
            super.onConfigurationChanged(newConfig)
        } else {
            mOldUiMode = newConfig.uiMode
            mIsDarkModeOn = DarkModeUtils.isDarkModeOn(this)
            WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars = !mIsDarkModeOn
            window.statusBarColor = R.color.background_white.toColor(this)
        }
    }
}