import 'package:flutter_map/flutter_map.dart';
import 'package:location/data/bean/atmob_track_point.dart';

class TrackUtil {
  static List<TraceLocation> points2TraceLocation(
      List<AtmobTrackPoint>? pointsList) {
    if (pointsList == null) {
      return [];
    }
    List<TraceLocation> traceLocations = [];
    for (var value in pointsList) {
      TraceLocation traceLocation = TraceLocation(
        latitude: value.latitude,
        longitude: value.longitude,
        time: value.time,
        speed: value.speed,
        bearing: value.bearing,
      );
      traceLocations.add(traceLocation);
    }
    return traceLocations;
  }

  static List<LatLng> traceLocation2LatLng(
      List<TraceLocation>? traceLocations) {
    if (traceLocations == null) {
      return [];
    }
    return traceLocations
        .map((e) => LatLng(latitude: e.latitude, longitude: e.longitude))
        .toList();
  }

  static String formatDurationFromMillis(int milliseconds) {
    final totalMinutes = milliseconds ~/ 60000; // 毫秒转分钟
    final hours = totalMinutes ~/ 60;
    final minutes = totalMinutes % 60;

    if (hours > 0 && minutes > 0) {
      return '${hours}h${minutes}min';
    } else if (hours > 0) {
      return '${hours}h';
    } else {
      return '${minutes}min';
    }
  }

  static List<LatLng> getTrackMovePoints(
      List<AtmobTrackPoint>? pointsList, int startTime, int endTime) {
    if (pointsList == null || pointsList.isEmpty) {
      return [];
    }
    List<LatLng> movePoints = [];
    for (var point in pointsList) {
      if (point.time >= startTime && point.time <= endTime) {
        movePoints
            .add(LatLng(latitude: point.latitude, longitude: point.longitude));
      }
    }
    return movePoints;
  }

  static LatLng? getTrackStartPoint(
      List<AtmobTrackPoint>? pointsList, int startTime) {
    if (pointsList == null || pointsList.isEmpty) {
      return null;
    }
    for (var point in pointsList) {
      if (point.time >= startTime) {
        return LatLng(latitude: point.latitude, longitude: point.longitude);
      }
    }
    return null;
  }
}
