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

abstract final class RangeOfAqiChartsHelper {
  const RangeOfAqiChartsHelper._();

  static const gradientData = <(Color color, String label)>[
    (ColorsManager.goodGreen, 'Good'),
    (ColorsManager.moderateYellow, 'Moderate'),
    (ColorsManager.poorOrange, 'Poor'),
    (ColorsManager.unhealthyRed, 'Unhealthy'),
    (ColorsManager.severePink, 'Severe'),
    (ColorsManager.hazardousPurple, 'Hazardous'),
  ];

  static FlTitlesData titlesData(
    BuildContext context,
    List<RangeOfAqi> data, {
    double leftSideInterval = 50,
  }) {
    final titlesData = EnergyManagementChartsHelper.titlesData(context);
    return titlesData.copyWith(
      bottomTitles: titlesData.bottomTitles.copyWith(
        sideTitles: titlesData.bottomTitles.sideTitles.copyWith(
          reservedSize: 36,
          getTitlesWidget: (value, meta) => Padding(
            padding: const EdgeInsetsDirectional.only(top: 20.0),
            child: Text(
              data.isNotEmpty ? data[value.toInt()].date.day.toString() : '',
              style: context.textTheme.bodySmall?.copyWith(
                fontSize: 12,
                color: ColorsManager.lightGreyColor,
              ),
            ),
          ),
        ),
      ),
      leftTitles: titlesData.leftTitles.copyWith(
        sideTitles: titlesData.leftTitles.sideTitles.copyWith(
          reservedSize: 70,
          interval: leftSideInterval,
          maxIncluded: false,
          minIncluded: true,
          getTitlesWidget: (value, meta) {
            final text = value.toInt().toString();
            return Padding(
              padding: const EdgeInsetsDirectional.only(end: 12),
              child: FittedBox(
                alignment: AlignmentDirectional.centerStart,
                fit: BoxFit.scaleDown,
                child: Text(
                  text,
                  style: context.textTheme.bodySmall?.copyWith(
                    fontSize: 12,
                    color: ColorsManager.lightGreyColor,
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  static List<LineTooltipItem?> getTooltipItems(
    List<LineBarSpot> touchedSpots,
    List<RangeOfAqi> chartData,
  ) {
    return touchedSpots.asMap().entries.map((entry) {
      final index = entry.key;
      final spot = entry.value;

      final label = switch (spot.barIndex) {
        0 => 'Max',
        1 => 'Avg',
        2 => 'Min',
        _ => '',
      };

      final date = DateFormat('dd/MM').format(chartData[spot.x.toInt()].date);

      return LineTooltipItem(
        index == 0 ? '$date\n' : '',
        const TextStyle(
          color: ColorsManager.textPrimaryColor,
          fontWeight: FontWeight.w600,
          fontSize: 12,
        ),
        children: [
          TextSpan(text: '$label: ${spot.y.toStringAsFixed(0)}'),
        ],
      );
    }).toList();
  }

  static LineTouchData lineTouchData(
    List<RangeOfAqi> chartData,
  ) {
    return LineTouchData(
      touchTooltipData: LineTouchTooltipData(
        getTooltipColor: (touchTooltipItem) => ColorsManager.whiteColors,
        tooltipBorder: const BorderSide(
          color: ColorsManager.semiTransparentBlack,
        ),
        tooltipRoundedRadius: 16,
        showOnTopOfTheChartBoxArea: false,
        tooltipPadding: const EdgeInsets.all(8),
        getTooltipItems: (touchedSpots) => RangeOfAqiChartsHelper.getTooltipItems(
          touchedSpots,
          chartData,
        ),
      ),
    );
  }
}
