import 'package:equatable/equatable.dart';

// ============================================================
// 客房状态总览-客房概览统计 BO 模型
// ============================================================

/// 概览统计（顶部统计区域）
class RoomOverviewSummaryBO extends Equatable {
  final int occupiedCount;
  final int vacantCount;
  final int reservedCount;
  final int alarmCount;

  const RoomOverviewSummaryBO({
    this.occupiedCount = 0,
    this.vacantCount = 0,
    this.reservedCount = 0,
    this.alarmCount = 0,
  });

  factory RoomOverviewSummaryBO.fromJson(Map<String, dynamic> json) {
    return RoomOverviewSummaryBO(
      occupiedCount: json['occupiedCount'] as int? ?? 0,
      vacantCount: json['vacantCount'] as int? ?? 0,
      reservedCount: json['reservedCount'] as int? ?? 0,
      alarmCount: json['alarmCount'] as int? ?? 0,
    );
  }

  @override
  List<Object?> get props => [occupiedCount, vacantCount, reservedCount, alarmCount];
}

/// 楼层Tab列表
class RoomFloorBO extends Equatable {
  final int floorId;
  final int floorNumber;
  final String floorName;
  final bool isActive;

  const RoomFloorBO({
    this.floorId = 0,
    this.floorNumber = 0,
    this.floorName = '',
    this.isActive = false,
  });

  factory RoomFloorBO.fromJson(Map<String, dynamic> json) {
    return RoomFloorBO(
      floorId: json['floorId'] as int? ?? 0,
      floorNumber: json['floorNumber'] as int? ?? 0,
      floorName: json['floorName'] as String? ?? '',
      isActive: json['isActive'] as bool? ?? false,
    );
  }

  @override
  List<Object?> get props => [floorId, floorNumber, floorName, isActive];
}

/// 房间网格（房间卡片网格布局）
class RoomGridBO extends Equatable {
  final int roomId;
  final int floorNumber;
  final String roomNumber;
  final String statusText;
  final String statusColor;
  final bool hasAlarmIcon;

  const RoomGridBO({
    this.roomId = 0,
    this.floorNumber = 0,
    this.roomNumber = '',
    this.statusText = '',
    this.statusColor = '',
    this.hasAlarmIcon = false,
  });

  factory RoomGridBO.fromJson(Map<String, dynamic> json) {
    return RoomGridBO(
      roomId: json['roomId'] as int? ?? 0,
      roomNumber: json['roomNumber'] as String? ?? '',
      statusText: json['statusText'] as String? ?? '',
      statusColor: json['statusColor'] as String? ?? '',
      hasAlarmIcon: json['hasAlarmIcon'] as bool? ?? false,
      floorNumber: json['floorNumber'] as int? ?? 0,
    );
  }

  @override
  List<Object?> get props => [roomId, roomNumber, statusText, statusColor, hasAlarmIcon, floorNumber];
}

/// 房间基础信息（客房概览统计）
class RoomOverviewBasicInfoBO extends Equatable {
  final int roomId;
  final String roomNumber;
  final String roomStatusText;
  final int floorNumber;

  const RoomOverviewBasicInfoBO({
    this.roomId = 0,
    this.roomNumber = '',
    this.roomStatusText = '',
    this.floorNumber = 0,
  });

  factory RoomOverviewBasicInfoBO.fromJson(Map<String, dynamic> json) {
    return RoomOverviewBasicInfoBO(
      roomId: json['roomId'] as int? ?? 0,
      roomNumber: json['roomNumber'] as String? ?? '',
      roomStatusText: json['roomStatusText'] as String? ?? '',
      floorNumber: json['floorNumber'] as int? ?? 0,
    );
  }

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

/// 状态标签
class StatusTagsBO extends Equatable {
  final bool isOccupied;
  final bool powerSupplyStatus;
  final bool wifiNormalStatus;

  const StatusTagsBO({
    this.isOccupied = false,
    this.powerSupplyStatus = false,
    this.wifiNormalStatus = false,
  });

  factory StatusTagsBO.fromJson(Map<String, dynamic> json) {
    return StatusTagsBO(
      isOccupied: json['isOccupied'] as bool? ?? false,
      powerSupplyStatus: json['powerSupplyStatus'] as bool? ?? false,
      wifiNormalStatus: json['wifiNormalStatus'] as bool? ?? false,
    );
  }

  @override
  List<Object?> get props => [isOccupied, powerSupplyStatus, wifiNormalStatus];
}

/// 房间设备项
class DeviceItemBO extends Equatable {
  final int deviceId;
  final String deviceName;
  final String deviceTypeIcon;
  final String powerStatus;
  final String powerStatusText;
  final String temperatureDisplay;
  final String paramDisplayText;

  const DeviceItemBO({
    this.deviceId = 0,
    this.deviceName = '',
    this.deviceTypeIcon = '',
    this.powerStatus = '',
    this.powerStatusText = '',
    this.temperatureDisplay = '',
    this.paramDisplayText = '',
  });

  factory DeviceItemBO.fromJson(Map<String, dynamic> json) {
    return DeviceItemBO(
      deviceId: json['deviceId'] as int? ?? 0,
      deviceName: json['deviceName'] as String? ?? '',
      deviceTypeIcon: json['deviceTypeIcon'] as String? ?? '',
      powerStatus: json['powerStatus'] as String? ?? '',
      powerStatusText: json['powerStatusText'] as String? ?? '',
      temperatureDisplay: json['temperatureDisplay'] as String? ?? '',
      paramDisplayText: json['paramDisplayText'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [
    deviceId,
    deviceName,
    deviceTypeIcon,
    powerStatus,
    powerStatusText,
    temperatureDisplay,
    paramDisplayText,
  ];
}

/// 房间详情列表项
class RoomDetailListBO extends Equatable {
  final RoomOverviewBasicInfoBO roomBasicInfo;
  final StatusTagsBO statusTags;
  final List<DeviceItemBO> deviceList;

  const RoomDetailListBO({
    required this.roomBasicInfo,
    required this.statusTags,
    this.deviceList = const [],
  });

  factory RoomDetailListBO.fromJson(Map<String, dynamic> json) {
    return RoomDetailListBO(
      roomBasicInfo: RoomOverviewBasicInfoBO.fromJson(json['roomBasicInfo'] as Map<String, dynamic>? ?? {}),
      statusTags: StatusTagsBO.fromJson(json['statusTags'] as Map<String, dynamic>? ?? {}),
      deviceList: (json['deviceList'] as List<dynamic>?)
              ?.map((e) => DeviceItemBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

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

/// 客房概览统计根对象
class RoomOverviewBO extends Equatable {
  final RoomOverviewSummaryBO summary;
  final List<RoomFloorBO> floorList;
  final List<RoomGridBO> roomGrid;
  final List<RoomDetailListBO> roomDetailList;

  const RoomOverviewBO({
    required this.summary,
    this.floorList = const [],
    this.roomGrid = const [],
    this.roomDetailList = const [],
  });

  factory RoomOverviewBO.fromJson(Map<String, dynamic> json) {
    return RoomOverviewBO(
      summary: RoomOverviewSummaryBO.fromJson(json['summary'] as Map<String, dynamic>? ?? {}),
      floorList: (json['floorList'] as List<dynamic>?)
              ?.map((e) => RoomFloorBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      roomGrid: (json['roomGrid'] as List<dynamic>?)
              ?.map((e) => RoomGridBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      roomDetailList: (json['roomDetailList'] as List<dynamic>?)
              ?.map((e) => RoomDetailListBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  @override
  List<Object?> get props => [summary, floorList, roomGrid, roomDetailList];
}

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 int floorNumber;
  final String floorName;
  final List<String> areas;
  final List<FloorRoomSimpleBO> rooms;

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

  factory FloorAreaBO.fromJson(Map<String, dynamic> json) {
    return FloorAreaBO(
      floorId: _parseInt(json['floorId']),
      floorNumber: _parseInt(json['floorNumber']),
      floorName: json['floorName'] as String? ?? '',
      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, floorNumber, floorName, areas, rooms];
}

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;//房间有无人状态，0=无人 1=有人
  final String roomStatus;//房间状态，0=无人 1=有人
  final String cleanStatus;//清洁灯状态，0=待清洁 2=已完成
  final String devicePowerStatus;//设备开关状态  0=关 1=开

  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];
}