import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/analytics/helpers/format_number_to_kwh.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

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

  static FlTitlesData titlesData(
    BuildContext context, {
    double? leftTitlesInterval,
  }) {
    const emptyTitle = AxisTitles(sideTitles: SideTitles(showTitles: false));
    return FlTitlesData(
      show: true,
      bottomTitles: AxisTitles(
        drawBelowEverything: true,
        sideTitles: SideTitles(
          interval: 1,
          reservedSize: 32,
          showTitles: true,
          maxIncluded: false,
          minIncluded: false,
          getTitlesWidget: (value, meta) => Padding(
            padding: const EdgeInsetsDirectional.only(top: 20.0),
            child: Text(
              (value + 1).toString(),
              style: context.textTheme.bodySmall?.copyWith(
                color: ColorsManager.greyColor,
                fontSize: 12,
              ),
            ),
          ),
        ),
      ),
      leftTitles: AxisTitles(
        sideTitles: SideTitles(
          showTitles: true,
          maxIncluded: false,
          minIncluded: false,
          interval: leftTitlesInterval,
          reservedSize: 110,
          getTitlesWidget: (value, meta) => Padding(
            padding: const EdgeInsetsDirectional.only(end: 12),
            child: FittedBox(
              alignment: AlignmentDirectional.centerStart,
              fit: BoxFit.scaleDown,
              child: Text(
                value.formatNumberToKwh,
                style: context.textTheme.bodySmall?.copyWith(
                  fontSize: 12,
                  color: ColorsManager.greyColor,
                ),
              ),
            ),
          ),
        ),
      ),
      rightTitles: emptyTitle,
      topTitles: emptyTitle,
    );
  }

  static String getToolTipLabel(num month, double value) {
    final monthLabel = month.toString();
    final valueLabel = value.formatNumberToKwh;
    final labels = [monthLabel, valueLabel];
    return labels.where((element) => element.isNotEmpty).join(', ');
  }

  static List<LineTooltipItem?> getTooltipItems(List<LineBarSpot> touchedSpots) {
    return touchedSpots.map((spot) {
      return LineTooltipItem(
        getToolTipLabel(spot.x + 1, spot.y),
        const TextStyle(
          color: ColorsManager.textPrimaryColor,
          fontWeight: FontWeight.w600,
          fontSize: 12,
        ),
      );
    }).toList();
  }

  static LineTouchTooltipData lineTouchTooltipData() {
    return LineTouchTooltipData(
      getTooltipColor: (touchTooltipItem) => ColorsManager.whiteColors,
      tooltipBorder: const BorderSide(color: ColorsManager.semiTransparentBlack),
      tooltipRoundedRadius: 16,
      showOnTopOfTheChartBoxArea: false,
      tooltipPadding: const EdgeInsets.all(8),
      getTooltipItems: getTooltipItems,
    );
  }

  static FlBorderData borderData() {
    return FlBorderData(
      show: true,
      border: const Border.symmetric(
        horizontal: BorderSide(
          color: ColorsManager.greyColor,
          style: BorderStyle.solid,
          width: 1,
        ),
      ),
    );
  }

  static FlGridData gridData() {
    return const FlGridData(
      show: true,
      drawVerticalLine: false,
      drawHorizontalLine: true,
    );
  }

  static LineTouchData lineTouchData() {
    return LineTouchData(
      handleBuiltInTouches: true,
      touchSpotThreshold: 2,
      touchTooltipData: EnergyManagementChartsHelper.lineTouchTooltipData(),
    );
  }
}
