import 'tts_service.dart';
import 'stt_service.dart';

enum VoiceCommandKey {
  openWalkguide, startWalkguide, stopWalkguide,
  callGuardian, openNotification, readAllNotif,
  openSos, sendSos, whereAmI,
  openActivity, openNavigation, openSettings,
  repeatLast, stopTts,
}

class VoiceCommand {
  final VoiceCommandKey key;
  final String phrase;
  final bool enabled;
  const VoiceCommand({required this.key, required this.phrase, required this.enabled});
}

/// Callback yang dipanggil saat command terdeteksi
/// Registered oleh router/screen yang relevan
typedef CommandCallback = void Function(VoiceCommandKey key);

class VoiceCommandHandler {
  final SttService _stt;
  final TtsService _tts;

  List<VoiceCommand> _commands = [];
  CommandCallback? onCommand;

  VoiceCommandHandler(this._stt, this._tts);

  void loadCommands(List<VoiceCommand> commands) {
    _commands = commands;
    _stt.onResult = _processText;
  }

  void loadDefaultCommands() {
    _commands = const [
      VoiceCommand(key: VoiceCommandKey.openWalkguide,    phrase: 'open walkguide',            enabled: true),
      VoiceCommand(key: VoiceCommandKey.startWalkguide,   phrase: 'start walkguide',            enabled: true),
      VoiceCommand(key: VoiceCommandKey.stopWalkguide,    phrase: 'stop walkguide',             enabled: true),
      VoiceCommand(key: VoiceCommandKey.callGuardian,     phrase: 'call guardian',              enabled: true),
      VoiceCommand(key: VoiceCommandKey.openNotification, phrase: 'open notifications',         enabled: true),
      VoiceCommand(key: VoiceCommandKey.readAllNotif,     phrase: 'read all my notifications', enabled: true),
      VoiceCommand(key: VoiceCommandKey.openSos,         phrase: 'open sos',                   enabled: true),
      VoiceCommand(key: VoiceCommandKey.sendSos,         phrase: 'send sos',                   enabled: true),
      VoiceCommand(key: VoiceCommandKey.whereAmI,        phrase: 'where am i',                 enabled: true),
      VoiceCommand(key: VoiceCommandKey.openActivity,    phrase: 'open activity log',           enabled: true),
      VoiceCommand(key: VoiceCommandKey.openNavigation,  phrase: 'open navigation',            enabled: true),
      VoiceCommand(key: VoiceCommandKey.openSettings,    phrase: 'open settings',              enabled: true),
      VoiceCommand(key: VoiceCommandKey.repeatLast,      phrase: 'repeat',                     enabled: true),
      VoiceCommand(key: VoiceCommandKey.stopTts,         phrase: 'stop',                       enabled: true),
    ];
    _stt.onResult = _processText;
  }

  void _processText(String text) {
    final lower = text.toLowerCase().trim();
    for (final cmd in _commands) {
      if (!cmd.enabled) continue;
      if (lower.contains(cmd.phrase.toLowerCase())) {
        _handleCommand(cmd.key);
        return;
      }
    }
  }

  void _handleCommand(VoiceCommandKey key) {
    onCommand?.call(key);
    // Built-in actions for TTS-only commands
    if (key == VoiceCommandKey.repeatLast) _tts.repeatLast();
    if (key == VoiceCommandKey.stopTts) _tts.stop();
  }
}
