import 'package:flutter/material.dart';

import '../modelos/preset_ecualizador.dart';
import '../tema/pluriwave_theme.dart';
import 'pluri_glass_surface.dart';

class EcualizadorWidget extends StatefulWidget {
  final PresetEcualizador preset;
  final void Function(PresetEcualizador) onCambio;

  const EcualizadorWidget({super.key, required this.preset, required this.onCambio});

  @override
  State<EcualizadorWidget> createState() => _EcualizadorWidgetState();
}

class _EcualizadorWidgetState extends State<EcualizadorWidget> {
  late List<double> _bandas;
  final List<String> _etiquetas = ['60Hz', '250Hz', '1kHz', '4kHz', '16kHz'];

  @override
  void initState() {
    super.initState();
    _bandas = List.from(widget.preset.bandas);
  }

  @override
  void didUpdateWidget(EcualizadorWidget old) {
    super.didUpdateWidget(old);
    if (old.preset.nombre != widget.preset.nombre) {
      setState(() => _bandas = List.from(widget.preset.bandas));
    }
  }

  void _actualizarBanda(int index, double valor) {
    setState(() => _bandas[index] = valor);
    widget.onCambio(PresetEcualizador(nombre: 'Personalizado', bandas: List.from(_bandas)));
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final tokens = context.pluriTokens;

    return PluriGlassSurface(
      borderRadius: BorderRadius.circular(tokens.radiusLg),
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Row(
            children: [
              Text('Ecualizador', style: theme.textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w700)),
              const Spacer(),
              Chip(
                label: Text(widget.preset.nombre, style: theme.textTheme.labelMedium),
                backgroundColor: theme.colorScheme.secondaryContainer.withValues(alpha: 0.75),
              ),
            ],
          ),
          const SizedBox(height: 14),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              for (int i = 0; i < 5; i++)
                Expanded(
                  child: Card(
                    color: theme.colorScheme.surfaceContainerHighest.withValues(alpha: 0.35),
                    margin: const EdgeInsets.symmetric(horizontal: 4),
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
                    child: Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 4),
                      child: Column(
                        children: [
                          SizedBox(
                            height: 152,
                            child: Semantics(
                              slider: true,
                              label: 'Banda ${_etiquetas[i]}',
                              value: '${_bandas[i].toStringAsFixed(1)} decibelios',
                              child: RotatedBox(
                                quarterTurns: 3,
                                child: Slider(
                                  value: _bandas[i],
                                  min: -12.0,
                                  max: 12.0,
                                  divisions: 24,
                                  onChanged: (v) => _actualizarBanda(i, v),
                                ),
                              ),
                            ),
                          ),
                          Text('${_bandas[i].toStringAsFixed(1)}dB', style: theme.textTheme.labelSmall),
                          Text(
                            _etiquetas[i],
                            style: theme.textTheme.bodySmall?.copyWith(color: theme.colorScheme.onSurfaceVariant),
                            textAlign: TextAlign.center,
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
            ],
          ),
        ],
      ),
    );
  }
}

class PresetsEcualizadorWidget extends StatelessWidget {
  final PresetEcualizador presetActual;
  final void Function(PresetEcualizador) onSeleccionar;

  const PresetsEcualizadorWidget({
    super.key,
    required this.presetActual,
    required this.onSeleccionar,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Wrap(
      spacing: 8,
      runSpacing: 6,
      children: PresetEcualizador.presets.map((p) {
        final selected = p.nombre == presetActual.nombre;
        return ChoiceChip(
          label: Text(p.nombre),
          selected: selected,
          showCheckmark: false,
          selectedColor: theme.colorScheme.primaryContainer,
          backgroundColor: theme.colorScheme.surfaceContainerHighest.withValues(alpha: 0.32),
          onSelected: (_) => onSeleccionar(p),
        );
      }).toList(),
    );
  }
}
