abstract class ParserBase {
  static final RegExp _htmlRegExp = RegExp(
    '(<[^>]*>)',
    multiLine: true,
  );
  RegExp get regExp;
  Subtitles getSubtitlesData(String subtitlesContent);

  String removeAllHtmlTags(String htmlText) {
    var newHtmlText = htmlText;
    _htmlRegExp.allMatches(htmlText).toList().forEach(
      (RegExpMatch regExpMatch) {
        newHtmlText = regExpMatch.group(0) == '<br>'
            ? newHtmlText.replaceAll(regExpMatch.group(0)!, '\n')
            : newHtmlText.replaceAll(regExpMatch.group(0)!, '');
      },
    );

    return newHtmlText;
  }
}

class Subtitles {
  Subtitles({
    required this.subtitles,
  });
  late String languageCode;
  final List<Subtitle> subtitles;
}

class Subtitle {
  Subtitle({
    required this.startTime,
    required this.endTime,
    required this.text,
  });
  Time startTime;
  Time endTime;
  String text;

  List<Object?> get props => [startTime, endTime, text];
}

class Time {
  Time(Duration duration) {
    this.duration = duration;
  }

  late Duration _duration;
  Duration get duration => _duration;
  set duration(Duration value) {
    _duration = value;
    var timeStr = duration.toString();
    var timeArr = timeStr.substring(0, timeStr.lastIndexOf('.') + 4).split(':');
    hour = int.parse(timeArr[0]);
    minutes = int.parse(timeArr[1]);
    var secArr = timeArr[2].split('.');
    seconds = int.parse(secArr[0]);
    milliseconds = int.parse(secArr[1]);
  }

  late int hour;
  late int minutes;
  late int seconds;
  late int milliseconds;

  Duration get before {
    return Duration(milliseconds: duration.inMilliseconds - 1);
  }

  @override
  String toString() {
    return '${hour.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}.${milliseconds.toString().padLeft(3, '0')}';
  }
}
