// ignore: file_names
import 'package:dart_framework/subtitle/parser_base.dart';

class ParserSRT extends ParserBase {
  @override
  RegExp get regExp => RegExp(
        r'((\d{2}):(\d{2}):(\d{2})\,(\d+)) +--> +((\d{2}):(\d{2}):(\d{2})\,(\d{3})).*[\r\n]+\s*((?:(?!\r?\n\r?).)*(\r\n|\r|\n)(?:.*))',
        caseSensitive: false,
        multiLine: true,
      );

  @override
  Subtitles getSubtitlesData(String subtitlesContent) {
    final matches = regExp.allMatches(subtitlesContent).toList();
    final subtitleList = <Subtitle>[];

    for (final regExpMatch in matches) {
      final startTimeHours = int.parse(regExpMatch.group(2)!);
      final startTimeMinutes = int.parse(regExpMatch.group(3)!);
      final startTimeSeconds = int.parse(regExpMatch.group(4)!);
      final startTimeMilliseconds = int.parse(regExpMatch.group(5)!);

      final endTimeHours = int.parse(regExpMatch.group(7)!);
      final endTimeMinutes = int.parse(regExpMatch.group(8)!);
      final endTimeSeconds = int.parse(regExpMatch.group(9)!);
      final endTimeMilliseconds = int.parse(regExpMatch.group(10)!);
      final text = removeAllHtmlTags(regExpMatch.group(11)!);

      final startTime = Time(Duration(
        hours: startTimeHours,
        minutes: startTimeMinutes,
        seconds: startTimeSeconds,
        milliseconds: startTimeMilliseconds,
      ));
      final endTime = Time(Duration(
        hours: endTimeHours,
        minutes: endTimeMinutes,
        seconds: endTimeSeconds,
        milliseconds: endTimeMilliseconds,
      ));

      subtitleList.add(
        Subtitle(startTime: startTime, endTime: endTime, text: text.trim()),
      );
    }

    return Subtitles(subtitles: subtitleList);
  }
}
