import 'dart:convert';
import 'dart:typed_data';
import 'package:syncrow_web/utils/constants/assets.dart';

class ScenesModel {
  final String id;
  final String? sceneTuyaId;
  final String name;
  final String status;
  final String type;
  final String? icon;
  final String spaceName;
  final String spaceId;
  final String communityId;

  ScenesModel({
    required this.id,
    this.sceneTuyaId,
    required this.name,
    required this.status,
    required this.type,
    required this.spaceName,
    required this.spaceId,
    required this.communityId,
    this.icon,
  });

  factory ScenesModel.fromRawJson(String str) =>
      ScenesModel.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  Uint8List? get iconInBytes {
    if (icon == null || icon?.isEmpty == true) return null;
    try {
      return base64Decode(icon!);
    } catch (e) {
      return null;
    }
  }

  factory ScenesModel.fromJson(Map<String, dynamic> json,
      {bool? isAutomation}) {
    return ScenesModel(
      id: json["id"] ?? json["uuid"] ?? '',
      sceneTuyaId: json["sceneTuyaId"] as String?,
      name: json["name"] ?? '',
      status: json["status"] ?? '',
      type: json["type"] ?? '',
      spaceName: json["spaceName"] ?? '',
      spaceId: json["spaceId"] ?? '',
      communityId: json["communityId"] ?? '',
      icon:
          isAutomation == true ? Assets.automation : (json["icon"] as String?),
    );
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "sceneTuyaId": sceneTuyaId ?? '',
        "name": name,
        "status": status,
        "type": type,
        "spaceName": spaceName,
        "spaceId": spaceId,
        "communityId": communityId,
      };
}
