package com.pixelized.desktop.lwa.ui.overlay.portrait

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.pixelized.desktop.lwa.ui.composable.image.LwaAsyncImage
import com.pixelized.desktop.lwa.ui.theme.color.component.LwaButtonColors
import com.pixelized.desktop.lwa.ui.theme.lwa
import kotlinx.coroutines.launch
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_cancel_24dp
import lwacharactersheet.composeapp.generated.resources.ic_link_24dp
import lwacharactersheet.composeapp.generated.resources.portrait_overlay__close
import lwacharactersheet.composeapp.generated.resources.portrait_overlay__external_link
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel

@Stable
data class PortraitOptionUio(
    val isBrowserAvailable: Boolean,
    val isGameMaster: Boolean,
)

@Composable
fun PortraitOverlay(
    modifier: Modifier = Modifier,
    viewModel: PortraitOverlayViewModel = koinViewModel(),
) {
    val scope = rememberCoroutineScope()
    val portrait = viewModel.portrait.collectAsState()
    val options = viewModel.options.collectAsState()

    PortraitContent(
        modifier = modifier,
        portrait = portrait,
        options = options,
        onDownload = { url ->
            viewModel.browse(url)
        },
        onClose = {
            scope.launch {
                viewModel.dismissPortrait()
            }
        }
    )
}

@Composable
private fun PortraitContent(
    modifier: Modifier = Modifier,
    portrait: State<String?>,
    options: State<PortraitOptionUio>,
    onDownload: (url: String) -> Unit,
    onClose: () -> Unit,
) {
    AnimatedContent(
        modifier = Modifier
            .clip(shape = MaterialTheme.lwa.shapes.portrait)
            .then(other = modifier),
        targetState = portrait.value,
        transitionSpec = {
            fadeIn() togetherWith fadeOut()
        }
    ) {
        when (it) {
            null -> Box(
                modifier = Modifier.fillMaxSize(),
            )

            else -> Box(
                modifier = Modifier.fillMaxSize(),
            ) {
                LwaAsyncImage(
                    modifier = Modifier.matchParentSize(),
                    model = it,
                    filterQuality = FilterQuality.High,
                    contentScale = ContentScale.Inside,
                    alignment = Alignment.Center,
                    contentDescription = null
                )

                Row(
                    modifier = Modifier
                        .align(alignment = Alignment.BottomCenter)
                        .animateContentSize(),
                    horizontalArrangement = Arrangement.spacedBy(space = 8.dp),
                ) {
                    AnimatedVisibility(
                        visible = options.value.isBrowserAvailable,
                        enter = fadeIn(),
                        exit = fadeOut(),
                    ) {
                        Button(
                            colors = LwaButtonColors(),
                            shape = CircleShape,
                            onClick = { onDownload(it) },
                        ) {
                            Icon(
                                painter = painterResource(Res.drawable.ic_link_24dp),
                                tint = MaterialTheme.lwa.colorScheme.base.primary,
                                contentDescription = null
                            )
                            Text(
                                modifier = Modifier.padding(start = 4.dp),
                                text = stringResource(Res.string.portrait_overlay__external_link),
                            )
                        }
                    }
                    AnimatedVisibility(
                        visible = options.value.isGameMaster,
                        enter = fadeIn(),
                        exit = fadeOut(),
                    ) {
                        Button(
                            colors = LwaButtonColors(),
                            shape = CircleShape,
                            onClick = onClose,
                        ) {
                            Icon(
                                painter = painterResource(Res.drawable.ic_cancel_24dp),
                                tint = MaterialTheme.lwa.colorScheme.base.primary,
                                contentDescription = null
                            )
                            Text(
                                modifier = Modifier.padding(start = 4.dp),
                                text = stringResource(Res.string.portrait_overlay__close),
                            )
                        }
                    }
                }
            }
        }
    }
}