import 'package:flutter_test/flutter_test.dart';

import 'package:chanora_flutter/l10n/generated/app_localizations_en.dart';
import 'package:chanora_flutter/src/rust/api.dart' as rust;
import 'package:chanora_flutter/widgets/voice_status_summary.dart';

void main() {
  final l10n = AppL10nEn();

  test('voice mode labels follow localization', () {
    expect(
      voiceModeLabel(l10n, rust.BridgeTransmitMode.ptt),
      l10n.voiceModePtt,
    );
    expect(
      voiceModeLabel(l10n, rust.BridgeTransmitMode.continuous),
      l10n.voiceModeContinuous,
    );
    expect(
      voiceModeLabel(l10n, rust.BridgeTransmitMode.voiceActivity),
      l10n.voiceModeVoiceActivity,
    );
  });

  test('PTT summary shows touch hold hint and release tail', () {
    final summary = voiceStatusSummary(
      l10n: l10n,
      transmitMode: rust.BridgeTransmitMode.ptt,
      releaseTailMs: 200,
      pttBoundKeyLabel: '',
      isTouchOnly: true,
      inputMuted: false,
      outputMuted: false,
      pttActive: false,
    );

    expect(summary.line1, '${l10n.voiceModePtt} · ${l10n.voicePttHoldHint}');
    expect(summary.line2, '200${l10n.voiceReleaseTailHint} release tail · off');
    expect(summary.statusText, l10n.voiceMicOff);
    expect(summary.micOn, isFalse);
  });

  test('PTT summary shows bound key on hardware hosts', () {
    final summary = voiceStatusSummary(
      l10n: l10n,
      transmitMode: rust.BridgeTransmitMode.ptt,
      releaseTailMs: 120,
      pttBoundKeyLabel: 'Space',
      isTouchOnly: false,
      inputMuted: false,
      outputMuted: false,
      pttActive: true,
    );

    expect(summary.line1, '${l10n.voiceModePtt} · Space');
    expect(summary.micOn, isTrue);
    expect(summary.statusText, l10n.voiceMicOn);
  });

  test('continuous mode is active unless muted or talk power blocked', () {
    final active = voiceStatusSummary(
      l10n: l10n,
      transmitMode: rust.BridgeTransmitMode.continuous,
      releaseTailMs: 0,
      pttBoundKeyLabel: '',
      isTouchOnly: false,
      inputMuted: false,
      outputMuted: false,
      pttActive: false,
    );
    final muted = voiceStatusSummary(
      l10n: l10n,
      transmitMode: rust.BridgeTransmitMode.continuous,
      releaseTailMs: 0,
      pttBoundKeyLabel: '',
      isTouchOnly: false,
      inputMuted: true,
      outputMuted: false,
      pttActive: false,
    );

    expect(active.micOn, isTrue);
    expect(muted.micOn, isFalse);
    expect(muted.statusText, '${l10n.voiceMicOff} (muted)');
  });

  test('talk power block overrides active mic state', () {
    final summary = voiceStatusSummary(
      l10n: l10n,
      transmitMode: rust.BridgeTransmitMode.continuous,
      releaseTailMs: 0,
      pttBoundKeyLabel: '',
      isTouchOnly: false,
      inputMuted: false,
      outputMuted: false,
      pttActive: true,
      talkPower: 5,
      neededTalkPower: 10,
      talkPowerGranted: false,
    );

    expect(summary.talkPowerBlocked, isTrue);
    expect(summary.micOn, isFalse);
    expect(summary.statusText, 'Insufficient permission');
  });
}
