import "package:flutter/foundation.dart" show debugPrint, kDebugMode;

import "package:client/data.dart";
import "package:shared/graph.dart";

import "api_client.dart";
import "service.dart";

class ApiService extends Service {
  static const usingDocker = !kDebugMode;
  final _client = ApiClient();

  @override
  Future<void> init() async {
    debugPrint("Running with Docker? $usingDocker");
  }

  Uri get _base => usingDocker
    ? Uri(path: "api/")
    : Uri(scheme: "http", host: "localhost", port: 8001);

  Future<List<StopState>?> getPath({
    required Coordinates start,
    required Coordinates end,
  }) => _client.getJsonList(
    _base.resolve("/path").replace(
      queryParameters: {
        "start_lat": start.lat.toString(),
        "start_lon": start.long.toString(),
        "end_lat": end.lat.toString(),
        "end_lon": end.long.toString(),
      },
    ),
    StopState.fromJson,
  );

  Future<List<Route>?> getRoutes() => _client.getJsonList(
    _base.resolve("/routes"),
    Route.fromJson,
  );

  Future<List<Stop>?> getStops() => _client.getJsonList(
    _base.resolve("/stops"),
    Stop.fromJson,
  );
}
