package de.arxsilex.maptools.converter.desktop

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import de.arxsilex.maptools.converter.core.pbf.PbfDecompression

@Composable
internal fun ProgramInfoDialog(
    onDismiss: () -> Unit,
    onOpenExternalLink: (String) -> Unit,
) {
    val strings = LocalAppStrings.current
    val acceleration = remember { PbfDecompression.accelerationStatus() }
    AlertDialog(
        onDismissRequest = onDismiss,
        title = { Text(strings["about.title"]) },
        text = {
            Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                Text("Version ${AppMetadata.version}", fontWeight = FontWeight.SemiBold)
                Text(
                    strings["about.description"],
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
                Text(
                    strings["about.export_formats"],
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
                InfoLink(strings("about.developer", AppMetadata.DEVELOPER), AppMetadata.DEVELOPER_URL, onOpenExternalLink)
                InfoLink(strings["about.android_app"], AppMetadata.ANDROID_APP_URL, onOpenExternalLink)
                InfoLink(strings("about.contact", AppMetadata.SUPPORT_EMAIL), AppMetadata.SUPPORT_MAIL_URL, onOpenExternalLink)
                InfoLink(
                    strings("about.software_license", AppMetadata.SOFTWARE_LICENSE),
                    AppMetadata.SOFTWARE_LICENSE_URL,
                    onOpenExternalLink,
                )
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(4.dp),
                ) {
                    InfoLink(strings["about.imprint"], AppMetadata.IMPRINT_URL, onOpenExternalLink)
                    InfoLink(strings["about.privacy"], AppMetadata.PRIVACY_URL, onOpenExternalLink)
                    InfoLink(strings["about.licenses"], AppMetadata.LICENSES_URL, onOpenExternalLink)
                }
                Text(
                    strings["about.map_data"],
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
                Text(
                    buildString {
                        append(
                            strings(
                                "about.acceleration",
                                strings[if (acceleration.accelerated) "about.accelerated" else "about.cpu"],
                                acceleration.activeProvider,
                            ),
                        )
                        if (!acceleration.accelerated && acceleration.cudaRuntimeCandidateFound) {
                            append(" · ${strings["about.cuda_hint"]}")
                        }
                    },
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        },
        confirmButton = {
            TextButton(onClick = onDismiss) { Text(strings["common.close"]) }
        },
    )
}

@Composable
private fun InfoLink(
    label: String,
    url: String,
    onOpenExternalLink: (String) -> Unit,
) {
    TextButton(
        onClick = { onOpenExternalLink(url) },
        modifier = Modifier.padding(horizontal = 0.dp),
    ) {
        Text(label)
    }
}
