import 'package:syncrow_web/utils/constants/app_enum.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/enum/device_types.dart';

class DeviceModel {
  dynamic productUuid;
  dynamic productType;
  dynamic activeTime;
  dynamic category;
  dynamic categoryName;
  dynamic createTime;
  dynamic gatewayId;
  dynamic icon;
  dynamic ip;
  dynamic lat;
  dynamic localKey;
  dynamic lon;
  dynamic model;
  dynamic name;
  DeviseStatus online;
  dynamic ownerId;
  dynamic sub;
  dynamic timeZone;
  dynamic updateTime;
  dynamic uuid;
  dynamic spaceName;

  DeviceModel({
    required this.productUuid,
    required this.productType,
    required this.activeTime,
    required this.category,
    required this.categoryName,
    required this.createTime,
    required this.gatewayId,
    required this.icon,
    required this.ip,
    required this.lat,
    required this.localKey,
    required this.lon,
    required this.model,
    required this.name,
    required this.online,
    required this.ownerId,
    required this.sub,
    required this.timeZone,
    required this.updateTime,
    required this.uuid,
    required this.spaceName,
  });

  // Deserialize from JSON
  factory DeviceModel.fromJson(Map<String, dynamic> json) {
    String tempIcon = '';
    DeviceType type = devicesTypesMap[json['productType']] ?? DeviceType.Other;
    if (type == DeviceType.LightBulb) {
      tempIcon = Assets.lightBulb;
    } else if (type == DeviceType.CeilingSensor ||
        type == DeviceType.WallSensor) {
      tempIcon = Assets.sensors;
    } else if (type == DeviceType.AC) {
      tempIcon = Assets.ac;
    } else if (type == DeviceType.DoorLock) {
      tempIcon = Assets.doorLock;
    } else if (type == DeviceType.Curtain) {
      tempIcon = Assets.curtain;
    } else if (type == DeviceType.ThreeGang) {
      tempIcon = Assets.gangSwitch;
    } else if (type == DeviceType.Gateway) {
      tempIcon = Assets.gateway;
    } else if (type == DeviceType.OneGang) {
      tempIcon = Assets.oneGang;
    } else if (type == DeviceType.TwoGang) {
      tempIcon = Assets.twoGang;
    } else if (type == DeviceType.WH) {
      tempIcon = Assets.waterHeater;
    } else if (type == DeviceType.DoorSensor) {
      tempIcon = Assets.openCloseDoor;
    } else if (type == DeviceType.GarageDoor) {
      tempIcon = Assets.openedDoor;
    } else if (type == DeviceType.WaterLeak) {
      tempIcon = Assets.waterLeakNormal;
    } else if (type == DeviceType.Curtain2) {
      tempIcon = Assets.curtainIcon;
    } else if (type == DeviceType.Curtain) {
      tempIcon = Assets.curtainIcon;
    } else {
      tempIcon = Assets.blackLogo;
    }

    return DeviceModel(
      productUuid: json['productUuid'],
      productType: json['productType'],
      activeTime: json['activeTime'],
      category: json['category'],
      categoryName: json['categoryName'],
      createTime: json['createTime'],
      gatewayId: json['gatewayId'],
      icon: tempIcon,
      ip: json['ip'],
      lat: json['lat'],
      localKey: json['localKey'],
      lon: json['lon'],
      model: json['model'],
      name: json['name'],
      online: OnlineTypeExtension.fromString(json['online']),
      ownerId: json['ownerId'],
      sub: json['sub'],
      timeZone: json['timeZone'],
      updateTime: json['updateTime'],
      uuid: json['uuid'],
      spaceName: json['spaceName'],
    );
  }

  // Serialize to JSON
  Map<String, dynamic> toJson() {
    return {
      'productUuid': productUuid,
      'productType': productType,
      'activeTime': activeTime,
      'category': category,
      'categoryName': categoryName,
      'createTime': createTime,
      'gatewayId': gatewayId,
      'icon': icon,
      'ip': ip,
      'lat': lat,
      'localKey': localKey,
      'lon': lon,
      'model': model,
      'name': name,
      'online': online,
      'ownerId': ownerId,
      'sub': sub,
      'timeZone': timeZone,
      'updateTime': updateTime,
      'uuid': uuid,
      'spaceName': spaceName
    };
  }
}
