import 'package:flutter_test/flutter_test.dart';
import 'package:smplayer/core/contracts/playback.dart';
import 'package:smplayer/features/player/session/initial_subtitle_resolver.dart';
import 'package:smplayer/providers/subtitle_settings_provider.dart';

const subtitles = <EmbySubtitleTrack>[
  EmbySubtitleTrack(index: 2, language: 'chi', title: '简体中文'),
  EmbySubtitleTrack(index: 3, language: 'eng', title: 'English'),
];

RememberedSubtitleTrack rememberedSubtitle({
  int streamIndex = 2,
  String? language,
  String? title,
}) {
  return RememberedSubtitleTrack(
    streamIndex: streamIndex,
    language: language,
    title: title,
    updatedAt: DateTime(2026),
  );
}

InitialSubtitleSelection resolveSelection({
  List<EmbySubtitleTrack> availableSubtitles = subtitles,
  int? preferredSubtitleStreamIndex,
  int? defaultSubtitleStreamIndex,
  RememberedSubtitleTrack? remembered,
}) {
  return resolveInitialSubtitleSelection(
    subtitles: availableSubtitles,
    preferredSubtitleStreamIndex: preferredSubtitleStreamIndex,
    defaultSubtitleStreamIndex: defaultSubtitleStreamIndex,
    rememberedSubtitle: remembered,
  );
}

void main() {
  test('显式 route 轨优先于记忆与服务器默认轨', () {
    final selection = resolveSelection(
      preferredSubtitleStreamIndex: 3,
      defaultSubtitleStreamIndex: 2,
      remembered: rememberedSubtitle(streamIndex: 2),
    );

    expect(selection.type, InitialSubtitleSelectionType.track);
    expect(selection.track?.index, 3);
  });

  test('route -1 且没有记忆时禁用字幕', () {
    final selection = resolveSelection(
      preferredSubtitleStreamIndex: -1,
      defaultSubtitleStreamIndex: 2,
    );

    expect(selection.type, InitialSubtitleSelectionType.disabled);
    expect(selection.track, isNull);
  });

  test('禁用字幕记忆优先于 route -1 与服务器默认轨', () {
    final selection = resolveSelection(
      preferredSubtitleStreamIndex: -1,
      defaultSubtitleStreamIndex: 2,
      remembered: RememberedSubtitleTrack(
        streamIndex: -1,
        updatedAt: DateTime(2026),
      ),
    );

    expect(selection.type, InitialSubtitleSelectionType.disabled);
  });

  test('跨集记忆按语言与标题匹配可用轨', () {
    final selection = resolveSelection(
      defaultSubtitleStreamIndex: 3,
      remembered: rememberedSubtitle(
        streamIndex: 99,
        language: 'CHI',
        title: '简体中文',
      ),
    );

    expect(selection.type, InitialSubtitleSelectionType.track);
    expect(selection.track?.index, 2);
  });

  test('记忆未命中时回落服务器默认轨', () {
    final selection = resolveSelection(
      defaultSubtitleStreamIndex: 3,
      remembered: rememberedSubtitle(
        streamIndex: 99,
        language: 'jpn',
        title: '日本語',
      ),
    );

    expect(selection.type, InitialSubtitleSelectionType.track);
    expect(selection.track?.index, 3);
  });

  test('没有 route、记忆或服务器默认轨时保持引擎状态', () {
    final selection = resolveSelection(availableSubtitles: const []);

    expect(selection.type, InitialSubtitleSelectionType.unchanged);
    expect(selection.track, isNull);
  });
}
