import 'package:flutter/services.dart';
import '../../flutter_map.dart';
import 'map_platform.dart';

class FlutterMap {
  FlutterMap._();

  static MapPlatform? _mapPlatform;

  static Future<void> init() async {
    if (_mapPlatform != null) {
      return;
    }
    MethodChannel channel = const MethodChannel(MapConstants.mapMethodChannel);

    MapPlatform mapPlatform = MapPlatform(channel);
    if (!await mapPlatform.init()) {
      throw Exception("地图初始化失败，未找到适配的地图，请检查是否添加地图库");
    }
    _mapPlatform = mapPlatform;
  }

  static Future<String?> getPlatformName() {
    assert(_mapPlatform != null, "请先初始化地图");
    return _mapPlatform!.getPlatformName();
  }

  static void receiveLocationStream({required MapLocationListener listener}) {
    assert(_mapPlatform != null, "请先初始化地图");
    _mapPlatform!.receiveLocationStream(listener: listener);
  }

  static Future<void> startLocation() {
    assert(_mapPlatform != null, "请先初始化地图");
    return _mapPlatform!.startLocation();
  }

  static Future<List<LatLng>?> queryProcessedTrace(
      {required int lineID, required List<TraceLocation> locations}) {
    assert(_mapPlatform != null, "请先初始化地图");
    return _mapPlatform!
        .queryProcessedTrace(lineID: lineID, locations: locations);
  }
}
