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

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

  final List<EnergyDataModel> chartData;

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: LineChart(
        LineChartData(
          maxY: chartData.isEmpty
              ? null
              : chartData.map((e) => e.value).reduce((a, b) => a > b ? a : b) + 250,
          clipData: const FlClipData.vertical(),
          titlesData: EnergyManagementChartsHelper.titlesData(
            context,
            leftTitlesInterval: 500,
          ),
          gridData: EnergyManagementChartsHelper.gridData().copyWith(
            checkToShowHorizontalLine: (value) => true,
            horizontalInterval: 500,
          ),
          borderData: EnergyManagementChartsHelper.borderData(),
          lineTouchData: EnergyManagementChartsHelper.lineTouchData(),
          lineBarsData: _lineBarsData,
        ),
        duration: Duration.zero,
        curve: Curves.easeIn,
      ),
    );
  }

  List<LineChartBarData> get _lineBarsData {
    return [
      LineChartBarData(
        spots: chartData
            .asMap()
            .entries
            .map(
              (entry) => FlSpot(
                entry.value.date.day.toDouble(),
                entry.value.value,
              ),
            )
            .toList(),
        color: ColorsManager.blueColor.withValues(alpha: 0.6),
        shadow: const Shadow(color: Colors.black12),
        show: true,
        isCurved: true,
        belowBarData: BarAreaData(
          show: true,
          gradient: LinearGradient(
            colors: [
              ColorsManager.vividBlue.withValues(alpha: 0.3),
              ColorsManager.vividBlue.withValues(alpha: 0.2),
              ColorsManager.vividBlue.withValues(alpha: 0.1),
              Colors.transparent,
            ],
            begin: Alignment.center,
            end: Alignment.bottomCenter,
          ),
        ),
        dotData: const FlDotData(show: false),
        isStrokeCapRound: true,
        barWidth: 3,
      ),
    ];
  }
}
