import 'package:flutter/material.dart';

import '../src/rust/api.dart' as rust;

/// Shared compact style for voice settings segmented buttons.
ButtonStyle voiceSegmentedButtonStyle(ThemeData theme) {
  return SegmentedButton.styleFrom(
    textStyle: theme.textTheme.labelSmall,
    visualDensity: VisualDensity.compact,
  );
}

/// Transmit mode selector segments.
const transmitModeSegments = [
  ButtonSegment(
    value: rust.BridgeTransmitMode.ptt,
    label: Text('PTT'),
    icon: Icon(Icons.radio_button_checked, size: 14),
  ),
  ButtonSegment(
    value: rust.BridgeTransmitMode.continuous,
    label: Text('Always'),
    icon: Icon(Icons.podcasts, size: 14),
  ),
  ButtonSegment(
    value: rust.BridgeTransmitMode.voiceActivity,
    label: Text('VAD'),
    icon: Icon(Icons.graphic_eq, size: 14),
  ),
];

/// Android hardware/WebRTC selector segments.
const androidProcessingSegments = [
  ButtonSegment(
    value: true,
    label: Text('Hardware'),
    icon: Icon(Icons.phone_android, size: 14),
  ),
  ButtonSegment(
    value: false,
    label: Text('WebRTC'),
    icon: Icon(Icons.science_outlined, size: 14),
  ),
];

/// Voice activity detector selector segments.
const vadBackendSegments = [
  ButtonSegment(
    value: rust.BridgeVadBackend.webrtcVad,
    label: Text('WebRTC'),
    icon: Icon(Icons.speed, size: 14),
  ),
  ButtonSegment(
    value: rust.BridgeVadBackend.sileroOnnx,
    label: Text('Silero'),
    icon: Icon(Icons.psychology, size: 14),
  ),
];

/// Desktop VAD selector segments.
///
/// Windows and Linux use Silero as the primary VAD. WebRTC remains an
/// internal runtime fallback when the model/runtime is unavailable.
const desktopVadBackendSegments = [
  ButtonSegment(
    value: rust.BridgeVadBackend.sileroOnnx,
    label: Text('Silero'),
    icon: Icon(Icons.psychology, size: 14),
  ),
];

/// Section subheader used by both voice settings surfaces.
class VoiceSubHeader extends StatelessWidget {
  /// Construct a voice settings subheader.
  const VoiceSubHeader(this.text, {super.key});

  /// Header text.
  final String text;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Padding(
      padding: const EdgeInsets.only(top: 8, bottom: 2),
      child: Text(
        text,
        style: theme.textTheme.labelSmall?.copyWith(
          color: theme.colorScheme.primary,
          letterSpacing: 0.5,
        ),
      ),
    );
  }
}

/// Section header used by the full voice settings dialog.
class VoiceSectionHeader extends StatelessWidget {
  /// Construct a voice settings section header.
  const VoiceSectionHeader(this.text, {super.key});

  /// Header text.
  final String text;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Padding(
      padding: const EdgeInsets.only(bottom: 4),
      child: Text(text, style: theme.textTheme.titleSmall),
    );
  }
}

/// Shared audio-processing switch row.
class AudioProcessingToggleRow extends StatelessWidget {
  /// Construct an audio-processing switch row.
  const AudioProcessingToggleRow({
    super.key,
    required this.label,
    required this.subtitle,
    required this.value,
    required this.onChanged,
    this.dense = false,
  });

  /// Primary row label.
  final String label;

  /// Secondary row detail.
  final String subtitle;

  /// Current switch value.
  final bool value;

  /// Called when the switch changes. Null disables the row.
  final ValueChanged<bool>? onChanged;

  /// Use the compact inline row layout used by the mobile sheet.
  final bool dense;

  @override
  Widget build(BuildContext context) {
    if (dense) return _buildDense(context);
    return SwitchListTile(
      dense: true,
      title: Text(label),
      subtitle: Text(subtitle, style: const TextStyle(fontSize: 11)),
      value: value,
      onChanged: onChanged,
    );
  }

  Widget _buildDense(BuildContext context) {
    final theme = Theme.of(context);
    final disabled = onChanged == null;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 2),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  label,
                  style: theme.textTheme.bodyMedium?.copyWith(
                    color: disabled
                        ? theme.colorScheme.onSurfaceVariant.withAlpha(120)
                        : null,
                  ),
                ),
                Text(
                  subtitle,
                  style: theme.textTheme.bodySmall?.copyWith(
                    color: theme.colorScheme.onSurfaceVariant.withAlpha(
                      disabled ? 80 : 160,
                    ),
                  ),
                ),
              ],
            ),
          ),
          Switch(value: value, onChanged: onChanged),
        ],
      ),
    );
  }
}
