class DeviceControlBO {
  final DeviceBasicInfoBO deviceBasicInfo;
  final RealtimeParamsBO realtimeParams;
  final DeviceTrendChartBO trendChart;

  const DeviceControlBO({
    required this.deviceBasicInfo,
    required this.realtimeParams,
    required this.trendChart,
  });

  factory DeviceControlBO.fromJson(Map<String, dynamic> json) {
    return DeviceControlBO(
      deviceBasicInfo: DeviceBasicInfoBO.fromJson(
        json['deviceBasicInfo'] as Map<String, dynamic>? ?? {},
      ),
      realtimeParams: RealtimeParamsBO.fromJson(
        json['realtimeParams'] as Map<String, dynamic>? ?? {},
      ),
      trendChart: DeviceTrendChartBO.fromJson(
        json['trendChart'] as Map<String, dynamic>? ?? {},
      ),
    );
  }
}

class DeviceBasicInfoBO {
  final int deviceId;
  final String deviceName;
  final String deviceCode;
  final String deviceLocation;
  final String deviceTypeIcon;
  final String onlineStatus;

  const DeviceBasicInfoBO({
    required this.deviceId,
    required this.deviceName,
    required this.deviceCode,
    required this.deviceLocation,
    required this.deviceTypeIcon,
    required this.onlineStatus,
  });

  factory DeviceBasicInfoBO.fromJson(Map<String, dynamic> json) {
    return DeviceBasicInfoBO(
      deviceId: json['deviceId'] as int? ?? 0,
      deviceName: json['deviceName'] as String? ?? '',
      deviceCode: json['deviceCode'] as String? ?? '',
      deviceLocation: json['deviceLocation'] as String? ?? '',
      deviceTypeIcon: json['deviceTypeIcon'] as String? ?? '',
      onlineStatus: json['onlineStatus'] as String? ?? '',
    );
  }
}

class RealtimeParamsBO {
  final String voltage;
  final String current;
  final String activePower;
  final String deviceTemperature;

  const RealtimeParamsBO({
    required this.voltage,
    required this.current,
    required this.activePower,
    required this.deviceTemperature,
  });

  factory RealtimeParamsBO.fromJson(Map<String, dynamic> json) {
    return RealtimeParamsBO(
      voltage: json['voltage'] as String? ?? '',
      current: json['current'] as String? ?? '',
      activePower: json['activePower'] as String? ?? '',
      deviceTemperature: json['deviceTemperature'] as String? ?? '',
    );
  }
}

class DeviceTrendChartBO {
  final String chartTitle;
  final String unit;
  final List<DeviceDataPointBO> dataPoints;
  final int yaxisMax;
  final int yaxisMin;

  const DeviceTrendChartBO({
    required this.chartTitle,
    required this.unit,
    required this.dataPoints,
    required this.yaxisMax,
    required this.yaxisMin,
  });

  factory DeviceTrendChartBO.fromJson(Map<String, dynamic> json) {
    return DeviceTrendChartBO(
      chartTitle: json['chartTitle'] as String? ?? '',
      unit: json['unit'] as String? ?? '',
      dataPoints: (json['dataPoints'] as List<dynamic>?)
              ?.map(
                  (e) => DeviceDataPointBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      yaxisMax: json['yaxisMax'] as int? ?? 100,
      yaxisMin: json['yaxisMin'] as int? ?? 0,
    );
  }
}

class DeviceDataPointBO {
  final String time;
  final double power;
  final double temperature;

  const DeviceDataPointBO({
    required this.time,
    required this.power,
    required this.temperature,
  });

  factory DeviceDataPointBO.fromJson(Map<String, dynamic> json) {
    final rawPower = json['power'];
    final double power;
    if (rawPower is num) {
      power = rawPower.toDouble();
    } else if (rawPower is String) {
      power = double.tryParse(rawPower) ?? 0;
    } else {
      power = 0;
    }

    final rawTemperature = json['temperature'];
    final double temperature;
    if (rawTemperature is num) {
      temperature = rawTemperature.toDouble();
    } else if (rawTemperature is String) {
      temperature = double.tryParse(rawTemperature) ?? 0;
    } else {
      temperature = 0;
    }

    return DeviceDataPointBO(
      time: json['time'] as String? ?? '',
      power: power,
      temperature: temperature,
    );
  }
}