package com.laseroptek.raman.ui.screens.splash


import android.content.res.Configuration
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.laseroptek.raman.R
import com.laseroptek.raman.const.preHeatingMillis
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.px
import kotlinx.coroutines.delay

@Composable
fun AnimatedProgressBar(
    onFinish: () -> Unit = {},
    durationMillis: Int = preHeatingMillis.toInt()
) {
    var targetProgress by remember { mutableStateOf(0.1f) } // Target progress value

    val animatedProgress by animateFloatAsState(
        targetValue = targetProgress,
        animationSpec = tween(durationMillis = 1000), // Animation duration
        label = "progressAnimation"
    )

    Column(modifier = Modifier
        .size(width = 150.px.dp, height = 33.px.dp)
        , horizontalAlignment = Alignment.CenterHorizontally
        , verticalArrangement = Arrangement.Center) {
        //Text(text = "Progress: ${(animatedProgress * 100).toInt()}%")
        Text(
            text = stringResource(R.string.loading),
            style = RobotoTypography.bodySmall,
            fontWeight = FontWeight.Normal,
            fontSize = 12.sp,
            color = Color(222,173,11),
            textAlign = TextAlign.Center
        )

        Spacer(Modifier.weight(1f))

        LinearProgressIndicator(
            progress = { animatedProgress },
            color = Color(222,173,11),
            trackColor= Color(222,173,11).copy(alpha = 0.3f),
        )
    }

    // Simulate progress updates
    LaunchedEffect(key1 = true) {
        while (targetProgress < durationMillis) {
            delay(100) // Update every 0.1 second
            targetProgress += 100f
        }

        onFinish.invoke()
    }
}

@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=1920dp,height=1080dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun AnimatedProgressBarPreview() {
    AnimatedProgressBar()
}