package com.ys.bdtp.adm.mvvm.ui.screen.flash

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.ys.bdtp.adm.R
import com.ys.bdtp.adm.app.theme.textBlackColor
import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
import com.ys.bdtp.adm.mvvm.ui.screen.main.Route
import com.ys.bdtp.adm.mvvm.vm.flash.FlashViewModel
import org.kodein.di.compose.rememberViewModel

/**
 * 闪屏页面
 */
@Composable
fun Flash() {
    val nav = LocalNavHostController.current!!
    val vm by rememberViewModel<FlashViewModel>()

    LaunchedEffect(Unit) {
        val user = vm.currentUser()
        if (user != null) {
            nav.navigate(Route.Project) {
                launchSingleTop = true
                popUpTo(Route.Flash) {
                    inclusive = true
                }
            }
        } else {
            nav.navigate(Route.Login) {
                launchSingleTop = true
                popUpTo(Route.Flash) {
                    inclusive = true
                }
            }
        }
    }

    Box(
        modifier = Modifier.fillMaxSize().background(Color.White),
        contentAlignment = Alignment.Center
    ) {
        Image(painter = painterResource(R.drawable.flash_bg), contentDescription = "")
        Column(
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(painter = painterResource(R.drawable.flash_logo), contentDescription = "")
            Spacer(modifier = Modifier.height(32.dp).fillMaxWidth())
            Text("智慧建筑运维平台", fontSize = 48.sp, color = textBlackColor)
        }
    }
}

@Preview
@Composable
fun PreviewFlashScreen() {
    Flash()
}