import 'package:equatable/equatable.dart';

int _parseInt(dynamic value, {int fallback = 0}) {
  if (value == null) return fallback;
  if (value is int) return value;
  if (value is double) return value.toInt();
  if (value is String) {
    final parsed = int.tryParse(value.trim());
    return parsed ?? fallback;
  }
  return fallback;
}

String _parseString(dynamic value, {String fallback = ''}) {
  if (value == null) return fallback;
  if (value is String) return value;
  return value.toString();
}

// ==================== 客房服务看板-客房状态总览-楼层 ====================

class FloorAreaBO extends Equatable {
  final int floorId;
  final List<String> areas;
  final List<FloorRoomSimpleBO> rooms;

  const FloorAreaBO({
    required this.floorId,
    required this.areas,
    required this.rooms,
  });

  factory FloorAreaBO.fromJson(Map<String, dynamic> json) {
    return FloorAreaBO(
      floorId: _parseInt(json['floorId']),
      areas: (json['areas'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      rooms: (json['rooms'] as List<dynamic>?)
              ?.map((e) =>
                  FloorRoomSimpleBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  @override
  List<Object?> get props => [floorId, areas, rooms];

  String get floorName => '${floorId}楼';
}

class FloorRoomSimpleBO extends Equatable {
  final int roomId;
  final String roomName;

  const FloorRoomSimpleBO({
    required this.roomId,
    required this.roomName,
  });

  factory FloorRoomSimpleBO.fromJson(Map<String, dynamic> json) {
    return FloorRoomSimpleBO(
      roomId: _parseInt(json['roomId']),
      roomName: json['roomName'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [roomId, roomName];
}

// ==================== 非客房-相关楼层区域设备 ====================

class AreaDeviceBO extends Equatable {
  final String deviceName;
  final String deviceCode;
  final String runStatus; // normal=正常 warning=预警 fault=故障 offline=离线

  const AreaDeviceBO({
    required this.deviceName,
    required this.deviceCode,
    required this.runStatus,
  });

  factory AreaDeviceBO.fromJson(Map<String, dynamic> json) {
    return AreaDeviceBO(
      deviceName: _parseString(json['deviceName']),
      deviceCode: _parseString(json['deviceCode']),
      runStatus: _parseString(json['runStatus']),
    );
  }

  @override
  List<Object?> get props => [deviceName, deviceCode, runStatus];
}

// ==================== 客房服务看板-客房状态总览-房间list ====================

class RoomStatusBO extends Equatable {
  final int roomId;
  final String roomNumber;
  final String occupancyStatus;
  final String roomStatus;
  final String cleanStatus;
  final String devicePowerStatus;

  const RoomStatusBO({
    required this.roomId,
    required this.roomNumber,
    required this.occupancyStatus,
    required this.roomStatus,
    required this.cleanStatus,
    required this.devicePowerStatus,
  });

  factory RoomStatusBO.fromJson(Map<String, dynamic> json) {
    return RoomStatusBO(
      roomId: _parseInt(json['roomId']),
      roomNumber: json['roomNumber'] as String? ?? '',
      occupancyStatus: _parseString(json['occupancyStatus']),
      roomStatus: _parseString(json['roomStatus']),
      cleanStatus: _parseString(json['cleanStatus']),
      devicePowerStatus: _parseString(json['devicePowerStatus']),
    );
  }

  @override
  List<Object?> get props =>
      [roomId, roomNumber, occupancyStatus, roomStatus, cleanStatus, devicePowerStatus];
}

// ==================== 客房详情 ====================

class RoomDetailBO extends Equatable {
  final RoomBasicInfoBO roomBasicInfo;
  final List<DeviceStatusBO> deviceStatusList;
  final RoomDeviceControlBO deviceControl;

  const RoomDetailBO({
    required this.roomBasicInfo,
    required this.deviceStatusList,
    required this.deviceControl,
  });

  factory RoomDetailBO.fromJson(Map<String, dynamic> json) {
    return RoomDetailBO(
      roomBasicInfo: RoomBasicInfoBO.fromJson(
          json['roomBasicInfo'] as Map<String, dynamic>? ?? {}),
      deviceStatusList: (json['deviceStatusList'] as List<dynamic>?)
              ?.map((e) =>
                  DeviceStatusBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      deviceControl: RoomDeviceControlBO.fromJson(
          json['deviceControl'] as Map<String, dynamic>? ?? {}),
    );
  }

  @override
  List<Object?> get props => [roomBasicInfo, deviceStatusList, deviceControl];
}

class RoomBasicInfoBO extends Equatable {
  final int roomId;
  final String roomNumber;
  final String occupancyStatus;
  final bool powerSupplyStatus;
  final String wifiOnlineStatus;

  const RoomBasicInfoBO({
    required this.roomId,
    required this.roomNumber,
    required this.occupancyStatus,
    required this.powerSupplyStatus,
    required this.wifiOnlineStatus,
  });

  factory RoomBasicInfoBO.fromJson(Map<String, dynamic> json) {
    return RoomBasicInfoBO(
      roomId: _parseInt(json['roomId']),
      roomNumber: _parseString(json['roomNumber']),
      occupancyStatus: _parseString(json['occupancyStatus']),
      powerSupplyStatus: json['powerSupplyStatus'] as bool? ?? false,
      wifiOnlineStatus: _parseString(json['wifiOnlineStatus']),
    );
  }

  @override
  List<Object?> get props =>
      [roomId, roomNumber, occupancyStatus, powerSupplyStatus, wifiOnlineStatus];
}

class DeviceStatusBO extends Equatable {
  final int deviceId;
  final String deviceName;
  final String deviceTypeName;
  final String deviceTypeIcon;
  final String powerStatus;
  final String powerStatusText;
  final String runStatus;
  final String runStatusText;
  final DeviceParamsBO? deviceParams;

  const DeviceStatusBO({
    required this.deviceId,
    required this.deviceName,
    required this.deviceTypeName,
    required this.deviceTypeIcon,
    required this.powerStatus,
    required this.powerStatusText,
    required this.runStatus,
    required this.runStatusText,
    this.deviceParams,
  });

  factory DeviceStatusBO.fromJson(Map<String, dynamic> json) {
    return DeviceStatusBO(
      deviceId: _parseInt(json['deviceId']),
      deviceName: _parseString(json['deviceName']),
      deviceTypeName: _parseString(json['deviceTypeName']),
      deviceTypeIcon: _parseString(json['deviceTypeIcon']),
      powerStatus: _parseString(json['powerStatus']),
      powerStatusText: _parseString(json['powerStatusText']),
      runStatus: _parseString(json['runStatus']),
      runStatusText: _parseString(json['runStatusText']),
      deviceParams: json['deviceParams'] != null
          ? DeviceParamsBO.fromJson(json['deviceParams'] as Map<String, dynamic>)
          : null,
    );
  }

  @override
  List<Object?> get props => [
        deviceId,
        deviceName,
        deviceTypeName,
        deviceTypeIcon,
        powerStatus,
        powerStatusText,
        runStatus,
        runStatusText,
        deviceParams,
      ];
}

class DeviceParamsBO extends Equatable {
  final String temperature;
  final String targetTemperature;
  final String mode;
  final String fanSpeed;

  const DeviceParamsBO({
    required this.temperature,
    required this.targetTemperature,
    required this.mode,
    required this.fanSpeed,
  });

  factory DeviceParamsBO.fromJson(Map<String, dynamic> json) {
    return DeviceParamsBO(
      temperature: _parseString(json['temperature']),
      targetTemperature: _parseString(json['targetTemperature']),
      mode: _parseString(json['mode']),
      fanSpeed: _parseString(json['fanSpeed']),
    );
  }

  @override
  List<Object?> get props => [temperature, targetTemperature, mode, fanSpeed];
}

class RoomDeviceControlBO extends Equatable {
  final RoomPowerControlBO roomPowerControl;
  final List<SubDeviceControlBO> subDeviceControls;

  const RoomDeviceControlBO({
    required this.roomPowerControl,
    required this.subDeviceControls,
  });

  factory RoomDeviceControlBO.fromJson(Map<String, dynamic> json) {
    return RoomDeviceControlBO(
      roomPowerControl: RoomPowerControlBO.fromJson(
          json['roomPowerControl'] as Map<String, dynamic>? ?? {}),
      subDeviceControls: (json['subDeviceControls'] as List<dynamic>?)
              ?.map((e) =>
                  SubDeviceControlBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  @override
  List<Object?> get props => [roomPowerControl, subDeviceControls];
}

class RoomPowerControlBO extends Equatable {
  final int roomId;
  final bool powerOn;
  final String description;

  const RoomPowerControlBO({
    required this.roomId,
    required this.powerOn,
    required this.description,
  });

  factory RoomPowerControlBO.fromJson(Map<String, dynamic> json) {
    return RoomPowerControlBO(
      roomId: _parseInt(json['roomId']),
      powerOn: json['powerOn'] as bool? ?? false,
      description: _parseString(json['description']),
    );
  }

  @override
  List<Object?> get props => [roomId, powerOn, description];
}

class SubDeviceControlBO extends Equatable {
  final int deviceId;
  final String deviceName;
  final String deviceTypeName;
  final bool powerOn;
  final String currentParamDisplay;

  const SubDeviceControlBO({
    required this.deviceId,
    required this.deviceName,
    required this.deviceTypeName,
    required this.powerOn,
    required this.currentParamDisplay,
  });

  factory SubDeviceControlBO.fromJson(Map<String, dynamic> json) {
    return SubDeviceControlBO(
      deviceId: _parseInt(json['deviceId']),
      deviceName: _parseString(json['deviceName']),
      deviceTypeName: _parseString(json['deviceTypeName']),
      powerOn: json['powerOn'] as bool? ?? false,
      currentParamDisplay: _parseString(json['currentParamDisplay']),
    );
  }

  @override
  List<Object?> get props =>
      [deviceId, deviceName, deviceTypeName, powerOn, currentParamDisplay];
}