import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

// class Device {
//   int? offset;
//   List<Item>? items;

//   Device({
//     this.offset,
//     this.items,
//   });
//   Device.fromJson(Map<String, dynamic> json) {
//     offset = json['offset'];
//     if (json['items'] != null) {
//       items = [];
//       json['items'].forEach((v) {
//         items!.add(Item.fromJson(v));
//       });
//     }
//   }
// }

class Device with ClusterItem {
  String? extId;
  String? thingId;
  String? thingKey;
  List<String?>? channels;
  String? areaPath;
  String? fvers;
  String? name;
  HardwareInfo? hardwareInfo;
  Settings? settings;
  Status? status;
  DateTime? connectionTime;
  int? state;
  String? visibility;
  DateTime? createdAt;
  DateTime? updatedAt;

  Device({
    this.extId,
    this.thingId,
    this.thingKey,
    this.channels,
    this.areaPath,
    this.fvers,
    this.name,
    this.hardwareInfo,
    this.settings,
    this.status,
    this.connectionTime,
    this.state,
    this.visibility,
    this.createdAt,
    this.updatedAt,
  });
  Device.fromJson(Map<String, dynamic> json) {
    extId = json['ext_id'];
    thingId = json['thing_id'];
    thingKey = json['thing_key'];
    if (json['channels'] != null) {
      channels = [];
      json['channels'].forEach((v) {
        channels!.add(v);
      });
    }
    areaPath = json['area_path'];
    fvers = json['fvers'];
    name = json['name'];
    hardwareInfo = json['hardware_info'] != null
        ? HardwareInfo.fromJson(json['hardware_info'])
        : null;
    settings =
        json['settings'] != null ? Settings.fromJson(json['settings']) : null;
    status = json['status'] != null ? Status.fromJson(json['status']) : null;
    connectionTime = DateTime.parse(json['connection_time']);
    state = json['state'];
    visibility = json['visibility'];
    createdAt = DateTime.parse(json['created_at']);
    updatedAt = DateTime.parse(json['updated_at']);
  }

  static List<Device> fromJsonDynamicList(List<dynamic> list) {
    return list.map((e) => Device.fromJson(e)).toList();
  }

  @override
  LatLng get location {
    double latitude = double.tryParse(settings?.latitude ?? '') ?? 34.639971;
    double longitude = double.tryParse(settings?.longitude ?? '') ?? -39.664027;
    return LatLng(latitude, longitude);
  }
}

class HardwareInfo {
  String? gsm;
  String? imei;
  String? imsi;
  String? phone;
  DateTime? updatedAt;

  HardwareInfo({
    this.gsm,
    this.imei,
    this.imsi,
    this.phone,
    this.updatedAt,
  });
  HardwareInfo.fromJson(Map<String, dynamic> json) {
    gsm = json['gsm'];
    imei = json['imei'];
    imsi = json['imsi'];
    phone = json['phone'];
    updatedAt = DateTime.parse(json['updated_at']);
  }
}

class Settings {
  String? latitude;
  String? longitude;
  DateTime? updatedAt;

  Settings({
    this.latitude,
    this.longitude,
    this.updatedAt,
  });
  Settings.fromJson(Map<String, dynamic> json) {
    latitude = json['latitude'];
    longitude = json['longitude'];
    updatedAt = DateTime.parse(json['updated_at']);
  }
}

class Status {
  int? readOffset;
  List<Sensor>? sensors;
  DateTime? updatedAt;

  Status({
    this.readOffset,
    this.sensors,
    this.updatedAt,
  });

  Status.fromJson(Map<String, dynamic> json) {
    readOffset = json['read_offset'];
    if (json['sensors'] != null) {
      sensors = [];
      json['sensors'].forEach((v) {
        sensors!.add(Sensor.fromJson(v));
      });
    }
    updatedAt = DateTime.parse(json['updated_at']);
  }
}

class Sensor {
  String? name;
  int? value;
  int? time;
  dynamic x;

  Sensor({this.name, this.value, this.time, this.x});
  Sensor.fromJson(Map<String, dynamic> json) {
    name = json['n'];
    value = json['v'];
    time = json['t'];
    x = json['x'];
  }
}
