package de.arxsilex.maptools.converter.desktop

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.ScrollbarStyle
import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import kotlin.math.ln
import kotlin.math.pow

@Composable
internal fun ScreenTitle(title: String, description: String) {
    Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {
        Text(
            text = title,
            style = MaterialTheme.typography.headlineMedium,
            fontWeight = FontWeight.SemiBold,
        )
        Text(description, color = MaterialTheme.colorScheme.onSurfaceVariant)
    }
}

@Composable
internal fun WorkflowFooter(
    status: String,
    onBack: (() -> Unit)? = null,
    onNext: (() -> Unit)? = null,
    nextLabel: String? = null,
    nextEnabled: Boolean = true,
) {
    val strings = LocalAppStrings.current
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Text(
            text = status,
            modifier = Modifier.weight(1f),
            color = MaterialTheme.colorScheme.onSurfaceVariant,
        )
        Spacer(Modifier.width(16.dp))
        Row(verticalAlignment = Alignment.CenterVertically) {
            if (onBack != null) {
                OutlinedButton(onClick = onBack) { Text(strings["common.back"]) }
                Spacer(Modifier.width(12.dp))
            }
            if (onNext != null) {
                Button(onClick = onNext, enabled = nextEnabled) { Text(nextLabel ?: strings["common.next"]) }
            }
        }
    }
}

@Composable
internal fun ScrollableWorkflowPage(
    footer: @Composable () -> Unit,
    content: @Composable ColumnScope.() -> Unit,
) {
    val scrollState = rememberScrollState()
    Column(modifier = Modifier.fillMaxSize()) {
        Box(modifier = Modifier.fillMaxWidth().weight(1f)) {
            Box(modifier = Modifier.fillMaxSize()) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .verticalScroll(scrollState)
                        .padding(start = 32.dp, top = 32.dp, end = 50.dp, bottom = 20.dp),
                    verticalArrangement = Arrangement.spacedBy(20.dp),
                    content = content,
                )
                if (scrollState.maxValue > 0) {
                    Box(
                        modifier = Modifier
                            .align(Alignment.CenterEnd)
                            .padding(vertical = 10.dp)
                            .width(10.dp)
                            .fillMaxHeight()
                            .background(MaterialTheme.colorScheme.outlineVariant, RoundedCornerShape(5.dp)),
                    )
                    VerticalScrollbar(
                        adapter = rememberScrollbarAdapter(scrollState),
                        modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight().padding(vertical = 10.dp),
                        style = ScrollbarStyle(
                            minimalHeight = 36.dp,
                            thickness = 10.dp,
                            shape = RoundedCornerShape(5.dp),
                            hoverDurationMillis = 180,
                            unhoverColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.62f),
                            hoverColor = MaterialTheme.colorScheme.tertiary,
                        ),
                    )
                }
            }
        }
        Box(modifier = Modifier.fillMaxWidth().padding(horizontal = 32.dp, vertical = 16.dp)) {
            footer()
        }
    }
}

@Composable
internal fun CardContainer(
    modifier: Modifier = Modifier,
    content: @Composable ColumnScope.() -> Unit,
) {
    Column(
        modifier = modifier
            .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.58f), RoundedCornerShape(16.dp))
            .border(1.dp, MaterialTheme.colorScheme.outlineVariant, RoundedCornerShape(16.dp))
            .padding(22.dp),
        content = content,
    )
}

internal fun formatFileSize(sizeBytes: Long): String {
    if (sizeBytes < 1024) return "$sizeBytes B"
    val unitIndex = (ln(sizeBytes.toDouble()) / ln(1024.0)).toInt().coerceAtMost(4)
    val units = listOf("B", "KiB", "MiB", "GiB", "TiB")
    val value = sizeBytes / 1024.0.pow(unitIndex)
    return "%.1f %s".format(value, units[unitIndex])
}

internal fun formatPercent(fraction: Float): String = "%.1f %%".format(fraction * 100f)

internal fun formatCoordinate(value: Double): String = "%.5f°".format(value)
