package ch.dissem.yaep.ui.common

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.BlurEffect
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.jetbrains.compose.resources.stringResource
import yaep.commonui.generated.resources.Res
import yaep.commonui.generated.resources.restart
import yaep.commonui.generated.resources.solved_congrats
import yaep.commonui.generated.resources.solved_time

@Composable
fun EndOfGame(
    modifier: Modifier = Modifier,
    isSolved: Boolean,
    time: String,
    onRestart: () -> Unit
) {
    val focusRequester = remember { FocusRequester() }
    LaunchedEffect(isSolved) {
        if (isSolved) {
            focusRequester.requestFocus()
        }
    }
    AnimatedVisibility(
        visible = isSolved,
        modifier = modifier,
        enter = fadeIn(),
        exit = fadeOut()
    ) {
        val background = MaterialTheme.colorScheme.background
        Box(
            modifier = Modifier
                .fillMaxSize()
                .drawBehind {
                    drawRect(color = background.copy(alpha = 0.8f))
                }
        ) {
            Column(
                modifier = Modifier
                    .align(Alignment.Center)
            ) {
                Text(
                    text = stringResource(Res.string.solved_congrats),
                    fontSize = 128.sp,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.fillMaxWidth()
                        .testTag("EndOfGame.solved_congrats")
                )
                Spacer(modifier = Modifier.height(16.dp))
                Text(
                    text = stringResource(Res.string.solved_time, time),
                    fontSize = 64.sp,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.fillMaxWidth()
                        .testTag("EndOfGame.solved_time")
                )
                Spacer(modifier = Modifier.height(32.dp))
                Button(
                    modifier = Modifier.align(CenterHorizontally)
                        .focusRequester(focusRequester)
                        .testTag("EndOfGame.restart"),
                    onClick = onRestart
                ) {
                    Text(stringResource(Res.string.restart))
                }
            }
        }
    }
}

fun Modifier.blurOnFinished(isSolved: Boolean): Modifier = graphicsLayer {
    if (isSolved) {
        this.renderEffect = BlurEffect(8f, 8f)
    }
}