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';

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

void main() {
  const embedded = [
    EmbySubtitleTrack(index: 2, language: 'chi', title: '简体中文'),
    EmbySubtitleTrack(index: 3, language: 'chi', title: '繁體中文'),
    EmbySubtitleTrack(index: 4, language: 'eng', title: 'English'),
    EmbySubtitleTrack(index: 5, title: 'Signs & Songs'),
  ];

  test('lang + title 精确命中（大小写不敏感）', () {
    final r = _remembered(language: 'CHI', title: '繁體中文');
    expect(matchRememberedSubtitle(r, embedded), 3);
  });

  test('language 为 null 时按 title 命中', () {
    final r = _remembered(streamIndex: 99, title: 'signs & songs');
    expect(matchRememberedSubtitle(r, embedded), 5);
  });

  test('title 不匹配时回落仅 lang 宽松匹配', () {
    final r = _remembered(language: 'chi', title: '不存在的标题');
    expect(matchRememberedSubtitle(r, embedded), 2);
  });

  test('lang/title 全空时按 streamIndex 直配', () {
    final r = _remembered(streamIndex: 4);
    expect(matchRememberedSubtitle(r, embedded), 4);
  });

  test('全部 miss 返回 null', () {
    final r = _remembered(streamIndex: 99, language: 'jpn', title: '日本語');
    expect(matchRememberedSubtitle(r, embedded), isNull);
  });

  test('空轨道列表返回 null', () {
    expect(matchRememberedSubtitle(_remembered(), const []), isNull);
  });
}
