import '../../contracts/danmaku.dart';
import '../../domain/ports/danmaku_gateway.dart';

class DispatchingDanmakuGateway implements DanmakuGateway {
  final DanmakuGateway httpGateway;
  final DanmakuGateway scriptWidgetGateway;

  const DispatchingDanmakuGateway({
    required this.httpGateway,
    required this.scriptWidgetGateway,
  });

  DanmakuGateway _gatewayFor(String sourceUrl) {
    return sourceUrl.startsWith('jsw://') ? scriptWidgetGateway : httpGateway;
  }

  @override
  Future<List<DanmakuMatch>> match(
    String baseUrl,
    String fileName,
    int durationSec,
  ) {
    return _gatewayFor(baseUrl).match(baseUrl, fileName, durationSec);
  }

  @override
  Future<List<DanmakuMatch>> searchEpisodes(
    String baseUrl,
    String anime,
    int? episode,
  ) {
    return _gatewayFor(baseUrl).searchEpisodes(baseUrl, anime, episode);
  }

  @override
  Future<List<DanmakuComment>> fetchComments(
    String baseUrl,
    int episodeId, {
    int chConvert = 0,
  }) {
    return _gatewayFor(
      baseUrl,
    ).fetchComments(baseUrl, episodeId, chConvert: chConvert);
  }

  @override
  Future<List<DanmakuBangumiEpisode>> getBangumiEpisodes(
    String baseUrl,
    int animeId,
  ) {
    return _gatewayFor(baseUrl).getBangumiEpisodes(baseUrl, animeId);
  }
}
