package com.syaroful.agrilinkvocpro.presentation.screen.splash

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.BlurredEdgeTreatment
import androidx.compose.ui.draw.blur
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.R
import com.syaroful.agrilinkvocpro.data.UserPreferences
import com.syaroful.agrilinkvocpro.presentation.theme.DarkGreen
import com.syaroful.agrilinkvocpro.presentation.theme.MainGreen
import kotlinx.coroutines.delay

@Composable
fun SplashScreen(
    userPreferences: UserPreferences,
    onNavigate: (String) -> Unit,
) {
    val isLoggedIn by userPreferences.isUserLoggedIn()
        .collectAsState(initial = false)

    LaunchedEffect(isLoggedIn) {
        delay(1000)
        if (isLoggedIn) {
            onNavigate("home")
        } else {
            onNavigate("login")
        }
    }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(DarkGreen)
    ) {
        Box(
            modifier = Modifier
                .align(Alignment.TopStart)
                .padding(top = 100.dp)
                .blur(radius = 150.dp, edgeTreatment = BlurredEdgeTreatment.Unbounded)
                .width(120.dp)
                .height(120.dp)
                .background(color = MainGreen)
        )
        Box(
            modifier = Modifier
                .align(Alignment.BottomEnd)
                .padding(bottom = 100.dp)
                .blur(radius = 150.dp, edgeTreatment = BlurredEdgeTreatment.Unbounded)
                .width(180.dp)
                .height(180.dp)
                .background(color = MainGreen)

        )
        Image(
            modifier = Modifier
                .align(Alignment.Center)
                .size(120.dp),
            painter = painterResource(id = R.drawable.app_logo),
            contentDescription = "App Logo"
        )
    }
}