import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/analytics/models/device_energy_data_model.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/helpers/energy_management_charts_helper.dart';

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

  final List<DeviceEnergyDataModel> chartData;
  static const _kLeftTitlesInterval = 2.5;

  @override
  Widget build(BuildContext context) {
    return LineChart(
      LineChartData(
        clipData: const FlClipData.vertical(),
        titlesData: EnergyManagementChartsHelper.titlesData(
          context,
          leftTitlesInterval: _kLeftTitlesInterval,
        ),
        gridData: EnergyManagementChartsHelper.gridData().copyWith(
          checkToShowHorizontalLine: (value) => true,
          horizontalInterval: _kLeftTitlesInterval,
        ),
        borderData: EnergyManagementChartsHelper.borderData(),
        lineTouchData: EnergyManagementChartsHelper.lineTouchData(),
        lineBarsData: chartData.map((e) {
          return _buildChartBar(
            color: e.color,
            spots: e.energy
                .map(
                  (energy) => FlSpot(
                    energy.date.day.toDouble(),
                    energy.value,
                  ),
                )
                .toList(),
          );
        }).toList(),
      ),
      duration: Duration.zero,
      curve: Curves.easeIn,
    );
  }

  LineChartBarData _buildChartBar({
    required Color color,
    required List<FlSpot> spots,
  }) {
    return LineChartBarData(
      spots: spots,
      dashArray: [12, 18],
      isCurved: true,
      color: color,
      barWidth: 3,
      isStrokeCapRound: true,
      dotData: const FlDotData(show: false),
    );
  }
}
