import 'package:flutter_map/src/entity/map_location.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:location/data/consts/constants.dart';

part 'location_info.g.dart';

@JsonSerializable()
class LocationInfo {
  @JsonKey(name: 'userId')
  final String? userId;

  @JsonKey(name: 'lng')
  final double? longitude;

  @JsonKey(name: 'lat')
  final double? latitude;

  @JsonKey(name: 'addr')
  final String? address;

  @JsonKey(name: 'timestamp')
  final int? lastUpdateTime;

  LocationInfo({
    this.userId,
    this.longitude,
    this.latitude,
    this.address,
    this.lastUpdateTime,
  });

  factory LocationInfo.fromJson(Map<String, dynamic> json) =>
      _$LocationInfoFromJson(json);

  Map<String, dynamic> toJson() => _$LocationInfoToJson(this);

  static LocationInfo fromMapLocation(MapLocation location) {
    return LocationInfo(
      longitude: location.longitude,
      latitude: location.latitude,
      address: location.address,
      lastUpdateTime: location.time,
    );
  }
}
