import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class GlobalTotalChart extends StatelessWidget {
  final List<FlSpot> monthlyTotals;

  const GlobalTotalChart({super.key, required this.monthlyTotals});

  @override
  Widget build(BuildContext context) {
    var maxY = 1.0;
    var minY = -1.0;
    if (monthlyTotals.isNotEmpty) {
      maxY = monthlyTotals.map((e) => e.y).toList().reduce((value, element) => value > element ? value : element) + 1000;
      minY = monthlyTotals.map((e) => e.y).toList().reduce((value, element) => value < element ? value : element) - 1000;
    }

    return Padding(
      padding: const EdgeInsets.only(
        bottom: 12,
        top: 20,
      ),
      child: AspectRatio(
        aspectRatio: 1,
        child: LayoutBuilder(
          builder: (context, constraints) => LineChart(
            LineChartData(
              lineTouchData: LineTouchData(
                touchTooltipData: LineTouchTooltipData(
                  maxContentWidth: 100,
                  tooltipBgColor: Theme.of(context).colorScheme.primaryContainer,
                  getTooltipItems: (touchedSpots) {
                    return touchedSpots.map((LineBarSpot touchedSpot) {
                      final textStyle = TextStyle(
                        color: touchedSpot.bar.gradient?.colors[0] ?? touchedSpot.bar.color,
                        fontWeight: FontWeight.bold,
                        fontSize: 14,
                      );
                      final date = DateTime(DateTime.now().year).add(Duration(days: touchedSpot.x.toInt() - 1));
                      return LineTooltipItem(
                        "${NumberFormat('#######.00 €', 'fr_FR').format(touchedSpot.y )} ${date.day}/${date.month}",
                        textStyle,
                      );
                    }).toList();
                  },
                ),
                handleBuiltInTouches: true,
                getTouchLineStart: (data, index) => 0,
              ),
              lineBarsData: [
                LineChartBarData(
                  color: Theme.of(context).colorScheme.primary,
                  spots: monthlyTotals,
                  isCurved: true,
                  isStrokeCapRound: true,
                  barWidth: 3,
                  belowBarData: BarAreaData(
                    show: true,
                    gradient: LinearGradient(
                      colors: [Theme.of(context).colorScheme.primary, Theme.of(context).colorScheme.secondary]
                        .map((color) => color.withOpacity(0.2))
                        .toList(),
                    ),
                  ),
                  dotData: const FlDotData(show: false),
                ),
              ],
              minY: minY,
              maxY: maxY,
              titlesData: const FlTitlesData(
                leftTitles: AxisTitles(
                  sideTitles: SideTitles(showTitles: false),
                ),
                rightTitles: AxisTitles(
                  sideTitles: SideTitles(showTitles: false),
                ),
                topTitles: AxisTitles(
                  sideTitles: SideTitles(showTitles: false),
                ),
                bottomTitles: AxisTitles(
                  sideTitles: SideTitles(showTitles: false),
                ),
              ),
              gridData: const FlGridData(
                show: false,
              ),
              borderData: FlBorderData(show: false),
            ),
          )
        )
      )
    );
  }
}