import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/analytics/models/analytics_device.dart';
import 'package:syncrow_web/pages/analytics/models/device_energy_data_model.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/widgets/chart_informative_cell.dart';

class EnergyConsumptionPerDeviceDevicesList extends StatelessWidget {
  const EnergyConsumptionPerDeviceDevicesList({
    required this.chartData,
    required this.devices,
    super.key,
  });

  final List<AnalyticsDevice> devices;
  final List<DeviceEnergyDataModel> chartData;

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Row(
        spacing: 16,
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: devices.map((e) => _buildDeviceCell(context, e)).toList(),
      ),
    );
  }

  Widget _buildDeviceCell(BuildContext context, AnalyticsDevice device) {
    final deviceColor = chartData
        .firstWhere(
          (element) => element.deviceId == device.uuid,
          orElse: () => const DeviceEnergyDataModel(
            energy: [],
            deviceName: '',
            deviceId: '',
            color: Colors.red,
          ),
        )
        .color;

    return Tooltip(
      message: '${device.name}\n${device.spaceUuid ?? ''}',
      child: ChartInformativeCell(title: Text(device.name), color: deviceColor),
    );
  }
}
