import 'package:flutter_test/flutter_test.dart';
import 'package:smplayer/core/contracts/library.dart';
import 'package:smplayer/features/detail/widgets/stream_selector_actions.dart';

void main() {
  const chineseSubtitle = EmbyRawMediaStream(
    Type: 'Subtitle',
    Language: 'chi',
    DisplayTitle: 'Chinese Simplified (SRT)',
  );
  const untitledSubtitle = EmbyRawMediaStream(Type: 'Subtitle');
  const dtsAudio = EmbyRawMediaStream(
    Type: 'Audio',
    Language: 'eng',
    DisplayTitle: 'DTS 5.1',
  );
  const untitledAudio = EmbyRawMediaStream(Type: 'Audio');

  group('subtitleSummaryLabel', () {
    test('returns 关闭 when no subtitle selected', () {
      expect(subtitleSummaryLabel(const [chineseSubtitle], null), '关闭');
    });

    test('returns 关闭 when index is out of range', () {
      expect(subtitleSummaryLabel(const [chineseSubtitle], 5), '关闭');
    });

    test('prefers Language over DisplayTitle for the compact button', () {
      expect(subtitleSummaryLabel(const [chineseSubtitle], 0), 'chi');
    });

    test('falls back to positional label when stream has no metadata', () {
      expect(
        subtitleSummaryLabel(const [chineseSubtitle, untitledSubtitle], 1),
        '字幕 2',
      );
    });
  });

  group('audioSummaryLabel', () {
    test('returns 默认 when index is out of range', () {
      expect(audioSummaryLabel(const [dtsAudio], 3), '默认');
    });

    test('prefers Language over DisplayTitle for the compact button', () {
      expect(audioSummaryLabel(const [dtsAudio], 0), 'eng');
    });

    test('falls back to positional label when stream has no metadata', () {
      expect(audioSummaryLabel(const [dtsAudio, untitledAudio], 1), '音轨 2');
    });
  });

  group('versionSummaryLabel', () {
    const namedSource = EmbyRawMediaSource(Id: 'a', Name: '4K HDR 版本');
    const unnamedSource = EmbyRawMediaSource(Id: 'b');

    test('uses the source name when present', () {
      expect(
        versionSummaryLabel(const [namedSource, unnamedSource], 0),
        '4K HDR 版本',
      );
    });

    test('falls back to positional label when the source has no name', () {
      expect(
        versionSummaryLabel(const [namedSource, unnamedSource], 1),
        '版本 2',
      );
    });

    test('falls back to 版本 when index is out of range', () {
      expect(versionSummaryLabel(const [namedSource], 9), '版本');
    });
  });
}
