package com.radiola.ui.update

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.radiola.ui.theme.RadiolaTheme

/** Стадия процесса обновления. */
enum class UpdateState { IDLE, DOWNLOADING, DOWNLOADED, ERROR }

/**
 * Диалог «доступно обновление». При force_update нельзя закрыть («Позже» скрыта,
 * тап мимо игнорируется). Кнопка ведёт по стадиям: Обновить → (прогресс) → Установить.
 */
@Composable
fun UpdateDialog(
    versionName: String,
    notes: String?,
    isForce: Boolean,
    state: UpdateState,
    progress: Int,
    errorMsg: String?,
    onPrimary: () -> Unit,
    onDismiss: () -> Unit
) {
    val colors = RadiolaTheme.colors
    val dismissable = !isForce && state != UpdateState.DOWNLOADING
    AlertDialog(
        onDismissRequest = { if (dismissable) onDismiss() },
        containerColor = colors.elevated,
        title = {
            Text(
                "Доступно обновление",
                color = colors.textPrimary,
                style = MaterialTheme.typography.titleLarge
            )
        },
        text = {
            Column {
                Text(
                    "Версия $versionName",
                    color = colors.accent,
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.SemiBold
                )
                if (!notes.isNullOrBlank()) {
                    Spacer(Modifier.height(8.dp))
                    Text(
                        notes,
                        color = colors.textSecondary,
                        style = MaterialTheme.typography.bodyMedium
                    )
                }
                when (state) {
                    UpdateState.DOWNLOADING -> {
                        Spacer(Modifier.height(14.dp))
                        LinearProgressIndicator(
                            progress = { progress / 100f },
                            modifier = Modifier.fillMaxWidth(),
                            color = colors.accent,
                            trackColor = colors.surface2
                        )
                        Spacer(Modifier.height(6.dp))
                        Text(
                            "Загрузка… $progress%",
                            color = colors.textMuted,
                            style = MaterialTheme.typography.labelMedium
                        )
                    }
                    UpdateState.ERROR -> {
                        Spacer(Modifier.height(10.dp))
                        Text(
                            errorMsg ?: "Ошибка обновления",
                            color = colors.live,
                            style = MaterialTheme.typography.bodyMedium
                        )
                    }
                    else -> {}
                }
            }
        },
        confirmButton = {
            Button(
                onClick = onPrimary,
                enabled = state != UpdateState.DOWNLOADING,
                colors = ButtonDefaults.buttonColors(
                    containerColor = colors.accent,
                    contentColor = colors.bgBase
                )
            ) {
                Text(
                    when (state) {
                        UpdateState.DOWNLOADED -> "Установить"
                        UpdateState.DOWNLOADING -> "Загрузка…"
                        UpdateState.ERROR -> "Повторить"
                        UpdateState.IDLE -> "Обновить"
                    },
                    fontWeight = FontWeight.SemiBold
                )
            }
        },
        dismissButton = {
            if (!isForce) {
                TextButton(
                    onClick = onDismiss,
                    colors = ButtonDefaults.textButtonColors(contentColor = colors.textSecondary)
                ) {
                    Text("Позже")
                }
            }
        }
    )
}
