package com.iscs.bozzys.ui.base

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.iscs.bozzys.ui.theme.BozzysTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch

/**
 * 基础活动，需所有活动继承
 */
abstract class PageBase(
    // 是否处理布局边界
    val needEdge: Boolean = true,
    // 是否隐藏底部导航按钮
    val hideNavigationBar: Boolean = false
) : ComponentActivity() {

    var loading: VMLoading? = null

    /**
     * 页面创建
     *
     * @param savedInstanceState 页面实例
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (hideNavigationBar) hideNavigationBar()
        if (needEdge) enableEdgeToEdge(
            navigationBarStyle = SystemBarStyle.auto(
                lightScrim = Color.Transparent.toArgb(),
                darkScrim = Color.Transparent.toArgb()
            )
        )
        setContent {
            // 全局使用黑主题色，主要原因顶部状态栏黑色和设计冲突
            BozzysTheme {
                loading = viewModel()
                Scaffold(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(Color.White)
                ) { innerPadding ->
                    GetViews(innerPadding)
                    // 全局loading弹窗
                    if (loading != null) LoadingCompose(loading!!)
                }
            }
        }
    }

    /**
     * 提供外部 用于构建页面视图使用
     *
     * @param pv 页面顶部预留间隙
     */
    @Composable
    abstract fun GetViews(pv: PaddingValues)

    /**
     * 页面显示后的处理逻辑
     */
    override fun onResume() {
        super.onResume()
        if (hideNavigationBar) hideNavigationBar()
    }

    /**
     * 销毁当前活动
     */
    fun destroy() {
        finish()
    }

    /**
     * 销毁当前活动，延时{value}ms后
     *
     * @param value 延时时间 单位：ms
     */
    fun destroyDelay(value: Long) {
        lifecycleScope.launch {
            delay(value)
            finish()
        }
    }

    /**
     * 隐藏底部导航栏
     */
    fun hideNavigationBar() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        val wic = WindowInsetsControllerCompat(window, window.decorView)
        wic.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        wic.hide(WindowInsetsCompat.Type.navigationBars())
    }

    /**
     * 显示Toast，通过字符串
     */
    fun Any.showToast() {
        lifecycleScope.launch(Dispatchers.Main) {
            when (this@showToast) {
                // 显示字符串Toast
                is String -> {
                    Toast.makeText(this@PageBase, this@showToast, Toast.LENGTH_LONG).show()
                }
                // 显示资源id Toast
                is Int -> {
                    Toast.makeText(this@PageBase, this@showToast, Toast.LENGTH_LONG).show()
                }
            }
        }
    }

    /**
     * 显示toast，Flow方式
     */
    suspend fun MutableSharedFlow<Any>.showToast() {
        this.collect { msg -> msg.showToast() }
    }

    /**
     * 显示全局Loading
     */
    fun showLoading() {
        loading?.show()
    }

    /**
     * 隐藏全局loading
     */
    fun hideLoading() {
        loading?.hide()
    }

    /**
     * 显示toast，Flow方式
     */
    suspend fun MutableSharedFlow<StateLoading>.loading() {
        this.collect { stateLoading ->
            if (stateLoading.show) {
                loading?.show(stateLoading)
            } else {
                loading?.hide()
            }
        }
    }

}