import 'package:syncrow_web/pages/analytics/models/device_energy_data_model.dart';
import 'package:syncrow_web/pages/analytics/models/energy_data_model.dart';
import 'package:syncrow_web/pages/analytics/params/get_energy_consumption_per_device_param.dart';
import 'package:syncrow_web/pages/analytics/services/energy_consumption_per_device/energy_consumption_per_device_service.dart';
import 'package:syncrow_web/utils/helpers/safe_division_helper.dart';

class EnergyConsumptionPerDeviceValueDividerDecorator
    implements EnergyConsumptionPerDeviceService {
  const EnergyConsumptionPerDeviceValueDividerDecorator(
    this._decoratee, {
    this.divider = 100,
  });

  final EnergyConsumptionPerDeviceService _decoratee;
  final double divider;

  @override
  Future<List<DeviceEnergyDataModel>> load(
    GetEnergyConsumptionPerDeviceParam param,
  ) async {
    final result = await _decoratee.load(param);
    return result.map((device) {
      return DeviceEnergyDataModel(
        deviceId: device.deviceId,
        deviceName: device.deviceName,
        color: device.color,
        energy: device.energy.map((e) {
          return EnergyDataModel(
            date: e.date,
            value: SafeDivisionHelper.divide(e.value, divider),
          );
        }).toList(),
      );
    }).toList();
  }
}
