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 DeviceSubtitleBO extends Equatable {
  final String text;
  final String voltage;
  final String powerDisplay;
  final String temperature;
  final String statusText;
  final String wifiSignal;

  const DeviceSubtitleBO({
    required this.text,
    required this.voltage,
    required this.powerDisplay,
    required this.temperature,
    required this.statusText,
    required this.wifiSignal,
  });

  factory DeviceSubtitleBO.fromJson(Map<String, dynamic> json) {
    return DeviceSubtitleBO(
      text: _parseString(json['text']),
      voltage: _parseString(json['voltage']),
      powerDisplay: _parseString(json['powerDisplay']),
      temperature: _parseString(json['temperature']),
      statusText: _parseString(json['statusText']),
      wifiSignal: _parseString(json['wifiSignal']),
    );
  }

  @override
  List<Object?> get props =>
      [text, voltage, powerDisplay, temperature, statusText, wifiSignal];
}

class DeviceStatusTagBO extends Equatable {
  final String text;
  final String color;

  const DeviceStatusTagBO({required this.text, required this.color});

  factory DeviceStatusTagBO.fromJson(Map<String, dynamic> json) {
    return DeviceStatusTagBO(
      text: _parseString(json['text']),
      color: _parseString(json['color']),
    );
  }

  @override
  List<Object?> get props => [text, color];
}

class DeviceRowBO extends Equatable {
  final int deviceId;
  final String deviceName;
  final String deviceCode;
  final String deviceLocation;
  final String deviceTypeIcon;
  final String deviceTypeName;
  final DeviceSubtitleBO subtitle;
  final DeviceStatusTagBO statusTag;
  final String onlineStatus;
  final String runStatus;
  final int roomId;
  final String roomNumber;

  const DeviceRowBO({
    required this.deviceId,
    required this.deviceName,
    required this.deviceCode,
    required this.deviceLocation,
    required this.deviceTypeIcon,
    required this.deviceTypeName,
    required this.subtitle,
    required this.statusTag,
    required this.onlineStatus,
    required this.runStatus,
    required this.roomId,
    required this.roomNumber,
  });

  factory DeviceRowBO.fromJson(Map<String, dynamic> json) {
    return DeviceRowBO(
      deviceId: _parseInt(json['deviceId']),
      deviceName: _parseString(json['deviceName']),
      deviceCode: _parseString(json['deviceCode']),
      deviceLocation: _parseString(json['deviceLocation']),
      deviceTypeIcon: _parseString(json['deviceTypeIcon']),
      deviceTypeName: _parseString(json['deviceTypeName']),
      subtitle: DeviceSubtitleBO.fromJson(
          json['subtitle'] as Map<String, dynamic>? ?? {}),
      statusTag: DeviceStatusTagBO.fromJson(
          json['statusTag'] as Map<String, dynamic>? ?? {}),
      onlineStatus: _parseString(json['onlineStatus']),
      runStatus: _parseString(json['runStatus']),
      roomId: _parseInt(json['roomId']),
      roomNumber: _parseString(json['roomNumber']),
    );
  }

  @override
  List<Object?> get props => [
        deviceId,
        deviceName,
        deviceCode,
        deviceLocation,
        deviceTypeIcon,
        deviceTypeName,
        subtitle,
        statusTag,
        onlineStatus,
        runStatus,
        roomId,
        roomNumber,
      ];
}

class DeviceListTabCountBO extends Equatable {
  final int all;
  final int airSwitch;
  final int guestControl;
  final int network;

  const DeviceListTabCountBO({
    required this.all,
    required this.airSwitch,
    required this.guestControl,
    required this.network,
  });

  factory DeviceListTabCountBO.fromJson(Map<String, dynamic> json) {
    return DeviceListTabCountBO(
      all: _parseInt(json['all']),
      airSwitch: _parseInt(json['airSwitch']),
      guestControl: _parseInt(json['guestControl']),
      network: _parseInt(json['network']),
    );
  }

  @override
  List<Object?> get props => [all, airSwitch, guestControl, network];
}

class DeviceListBO extends Equatable {
  final int total;
  final DeviceListTabCountBO tabCount;
  final List<DeviceRowBO> rows;

  const DeviceListBO({
    required this.total,
    required this.tabCount,
    required this.rows,
  });

  factory DeviceListBO.fromJson(Map<String, dynamic> json) {
    return DeviceListBO(
      total: _parseInt(json['total']),
      tabCount: DeviceListTabCountBO.fromJson(
          json['tabCount'] as Map<String, dynamic>? ?? {}),
      rows: (json['rows'] as List<dynamic>?)
              ?.map((e) => DeviceRowBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  @override
  List<Object?> get props => [total, tabCount, rows];
}
