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/pages/analytics/widgets/charts_x_axis_title.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(
        axisNameWidget: const ChartsXAxisTitle(),
        drawBelowEverything: true,
        sideTitles: SideTitles(
          interval: 1,
          reservedSize: 32,
          showTitles: true,
          maxIncluded: true,
          minIncluded: true,
          getTitlesWidget: (value, meta) => Padding(
            padding: const EdgeInsetsDirectional.only(top: 20.0),
            child: Text(
              value.toString(),
              style: context.textTheme.bodySmall?.copyWith(
                color: ColorsManager.lightGreyColor,
                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.lightGreyColor,
                ),
              ),
            ),
          ),
        ),
      ),
      rightTitles: emptyTitle,
      topTitles: emptyTitle,
    );
  }

  static String getToolTipLabel(double value) => value.formatNumberToKwh;

  static List<LineTooltipItem?> getTooltipItems(List<LineBarSpot> touchedSpots) {
    return touchedSpots.map((spot) {
      return LineTooltipItem(
        getToolTipLabel(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 FlGridData gridData({
    double horizontalInterval = 250,
  }) {
    return FlGridData(
      show: true,
      drawVerticalLine: false,
      drawHorizontalLine: true,
      horizontalInterval: horizontalInterval,
      getDrawingHorizontalLine: (value) {
        return FlLine(
          color: ColorsManager.greyColor,
          strokeWidth: 1,
          dashArray: value == 0 ? null : [5, 5],
        );
      },
    );
  }

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

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