package com.radiola.ui.equalizer

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Slider
import androidx.compose.material3.SliderDefaults
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.composables.icons.lucide.ChevronLeft
import com.composables.icons.lucide.Lucide
import com.radiola.service.EqBand
import com.radiola.ui.theme.RadiolaTheme

@Composable
fun EqualizerScreen(
    onNavigateBack: () -> Unit,
    viewModel: EqualizerViewModel = hiltViewModel()
) {
    val colors = RadiolaTheme.colors
    val state by viewModel.state.collectAsState()
    val on = state.enabled

    Column(modifier = Modifier.fillMaxSize()) {
        // Шапка с кнопкой «назад»
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 12.dp, vertical = 12.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                Lucide.ChevronLeft,
                contentDescription = "Назад",
                tint = colors.textPrimary,
                modifier = Modifier
                    .clip(RoundedCornerShape(12.dp))
                    .clickable { onNavigateBack() }
                    .padding(8.dp)
                    .size(24.dp)
            )
            Spacer(Modifier.width(4.dp))
            Text(
                text = "Эквалайзер",
                style = MaterialTheme.typography.headlineSmall,
                color = colors.textPrimary,
                fontWeight = FontWeight.Bold
            )
        }

        if (!state.available) {
            Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                Text(
                    "Эквалайзер недоступен на этом устройстве",
                    color = colors.textSecondary,
                    style = MaterialTheme.typography.bodyMedium,
                    modifier = Modifier.padding(32.dp)
                )
            }
            return
        }

        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(horizontal = 20.dp)
                .padding(bottom = 32.dp),
            verticalArrangement = Arrangement.spacedBy(20.dp)
        ) {
            // Главный тумблер
            Card(colors) {
                Row(
                    modifier = Modifier.fillMaxWidth().padding(16.dp),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Column(Modifier.weight(1f)) {
                        Text(
                            "Эквалайзер",
                            style = MaterialTheme.typography.titleMedium,
                            color = colors.textPrimary
                        )
                        Text(
                            "Тонкая настройка звука и улучшайзеры",
                            style = MaterialTheme.typography.labelMedium,
                            color = colors.textSecondary
                        )
                    }
                    Switch(
                        checked = on,
                        onCheckedChange = { viewModel.setEnabled(it) },
                        colors = SwitchDefaults.colors(
                            checkedThumbColor = colors.bgBase,
                            checkedTrackColor = colors.accent,
                            uncheckedThumbColor = colors.textMuted,
                            uncheckedTrackColor = colors.surface2
                        )
                    )
                }
            }

            // Пресеты
            if (state.presets.isNotEmpty()) {
                Label("ПРЕСЕТЫ")
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .horizontalScroll(rememberScrollState()),
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    state.presets.forEachIndexed { index, name ->
                        val selected = state.currentPreset == index
                        Box(
                            modifier = Modifier
                                .clip(RoundedCornerShape(10.dp))
                                .background(if (selected) colors.accent else colors.surface2)
                                .clickable(enabled = on) { viewModel.selectPreset(index) }
                                .padding(horizontal = 14.dp, vertical = 8.dp)
                        ) {
                            Text(
                                text = name,
                                style = MaterialTheme.typography.labelLarge,
                                color = if (selected) colors.bgBase else colors.textSecondary,
                                fontWeight = if (selected) FontWeight.SemiBold else FontWeight.Normal
                            )
                        }
                    }
                    // «Свой» — активен, когда полосы правились вручную
                    val custom = state.currentPreset == -1
                    Box(
                        modifier = Modifier
                            .clip(RoundedCornerShape(10.dp))
                            .background(if (custom) colors.accent else colors.surface2)
                            .padding(horizontal = 14.dp, vertical = 8.dp)
                    ) {
                        Text(
                            "Свой",
                            style = MaterialTheme.typography.labelLarge,
                            color = if (custom) colors.bgBase else colors.textSecondary,
                            fontWeight = if (custom) FontWeight.SemiBold else FontWeight.Normal
                        )
                    }
                }
            }

            // Полосы эквалайзера
            Label("ЧАСТОТЫ")
            Card(colors) {
                Column(
                    modifier = Modifier.fillMaxWidth().padding(16.dp),
                    verticalArrangement = Arrangement.spacedBy(6.dp)
                ) {
                    state.bands.forEach { band ->
                        BandRow(
                            band = band,
                            enabled = on,
                            colors = colors,
                            onChange = { viewModel.setBand(band.index, it) },
                            onCommit = { viewModel.commit() }
                        )
                    }
                }
            }

            // Улучшайзеры
            Label("УЛУЧШАЙЗЕРЫ")
            Card(colors) {
                Column(
                    modifier = Modifier.fillMaxWidth().padding(16.dp),
                    verticalArrangement = Arrangement.spacedBy(16.dp)
                ) {
                    if (state.hasBass) {
                        EnhancerSlider("Bass Boost", "Усиление баса", state.bass, on, colors,
                            { viewModel.setBass(it) }, { viewModel.commit() })
                    }
                    if (state.hasVirtualizer) {
                        EnhancerSlider("Virtualizer", "Объём / ширина стерео", state.virtualizer, on, colors,
                            { viewModel.setVirtualizer(it) }, { viewModel.commit() })
                    }
                    if (state.hasLoudness) {
                        EnhancerSlider("Громкость", "Подъём тихих станций (до +12 дБ)", state.loudness, on, colors,
                            { viewModel.setLoudness(it) }, { viewModel.commit() })
                    }
                }
            }
        }
    }
}

@Composable
private fun Card(colors: com.radiola.ui.theme.RadiolaColors, content: @Composable () -> Unit) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(16.dp))
            .background(colors.surface)
            .border(1.dp, colors.border, RoundedCornerShape(16.dp))
    ) { content() }
}

@Composable
private fun Label(text: String) {
    Text(
        text = text,
        style = MaterialTheme.typography.labelSmall,
        color = RadiolaTheme.colors.textMuted,
        letterSpacing = 1.sp
    )
}

@Composable
private fun BandRow(
    band: EqBand,
    enabled: Boolean,
    colors: com.radiola.ui.theme.RadiolaColors,
    onChange: (Int) -> Unit,
    onCommit: () -> Unit
) {
    Row(verticalAlignment = Alignment.CenterVertically) {
        Text(
            text = freqLabel(band.centerHz),
            style = MaterialTheme.typography.labelMedium,
            color = colors.textSecondary,
            modifier = Modifier.width(60.dp)
        )
        Slider(
            value = band.levelMb.toFloat(),
            onValueChange = { onChange(it.toInt()) },
            onValueChangeFinished = onCommit,
            valueRange = band.minMb.toFloat()..band.maxMb.toFloat(),
            enabled = enabled,
            colors = SliderDefaults.colors(
                thumbColor = colors.accent,
                activeTrackColor = colors.accent,
                inactiveTrackColor = colors.surface2
            ),
            modifier = Modifier.weight(1f)
        )
        Text(
            text = "%+d dB".format(band.levelMb / 100),
            style = MaterialTheme.typography.labelMedium,
            color = colors.accent,
            modifier = Modifier.width(52.dp),
            fontWeight = FontWeight.SemiBold
        )
    }
}

@Composable
private fun EnhancerSlider(
    title: String,
    subtitle: String,
    value: Int,
    enabled: Boolean,
    colors: com.radiola.ui.theme.RadiolaColors,
    onChange: (Int) -> Unit,
    onCommit: () -> Unit
) {
    Column {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Column(Modifier.weight(1f)) {
                Text(title, style = MaterialTheme.typography.titleMedium, color = colors.textPrimary)
                Text(subtitle, style = MaterialTheme.typography.labelSmall, color = colors.textSecondary)
            }
            Text(
                "$value%",
                style = MaterialTheme.typography.titleMedium,
                color = colors.accent,
                fontWeight = FontWeight.SemiBold
            )
        }
        Slider(
            value = value.toFloat(),
            onValueChange = { onChange(it.toInt()) },
            onValueChangeFinished = onCommit,
            valueRange = 0f..100f,
            enabled = enabled,
            colors = SliderDefaults.colors(
                thumbColor = colors.accent,
                activeTrackColor = colors.accent,
                inactiveTrackColor = colors.surface2
            )
        )
    }
}

private fun freqLabel(hz: Int): String =
    if (hz >= 1000) "%.1f kHz".format(hz / 1000f) else "$hz Hz"
