import "dart:convert";
import "dart:io";

import "package:shared/generator.dart";

class Generator<T extends Encodable> extends Parser<T> {
  final Parser<T> bc;
  final Parser<T> occt;
  final File outputFile;

  Generator({
    required this.bc,
    required this.occt,
    required this.outputFile,
  });

  @override
  Future<List<T>> parse() async => [
    ...await bc.parse(),
    ...await occt.parse(),
  ];

  Future<void> generate() async {
    final result = [
      for (final data in await parse())
        data.toJson(),
    ];
    const encoder = JsonEncoder.withIndent("  ");
    final contents = encoder.convert(result);
    await outputFile.writeAsString(contents);
  }

  static final stops = Generator(
    bc: BcStopParser(),
    occt: OcctStopParser(),
    outputFile: File(serverDir / "GET_STOPS.json"),
  );

  static final routes = Generator(
    bc: BcRoutesParser(),
    occt: OcctRoutesParser(),
    outputFile: File(serverDir / "GET_routes.json"),
  );
}
