import "provider.dart";
import "stop.dart";
import "utils.dart";

extension type RouteID._(String id) {
  RouteID(Provider provider, Object json) :
    id = "${provider.id}_$json";

  RouteID.fromJson(dynamic json) : id = json.toString();
  factory RouteID.fromBcCsv(CsvRow csv) => RouteID(Provider.bc, csv[0]);

  RouteID withDirection(String direction) => RouteID._("${id}_$direction");
}

class Route with Encodable {
  final RouteID id;
  final Provider provider;
  final String fullName;
  final String shortName;
  final List<StopID> stops;

  const Route({
    required this.id,
    required this.provider,
    required this.stops,
    required this.fullName,
    required this.shortName,
  });

  Route.fromOcctJson(Json json) :
    id = RouteID(Provider.occt, json["id"]),
    provider = Provider.occt,
    shortName = json["abbr"],
    fullName = json["name"],
    stops = [
      for (final stopID in (json["stops"] as List).cast<int>())
        StopID(stopID.toString()),
    ];

  Route.fromBcCsv(CsvRow csv) :
    id = RouteID.fromBcCsv(csv),
    provider = Provider.bc,
    shortName = csv[2],
    fullName = "${csv[2]}) ${csv[3]}",
    stops = [];

  @override
  Json toJson() => {
    "id": id,
    "provider": provider.id,
    "full_name": fullName,
    "short_name": shortName,
    "stops": stops,
  };
}
