// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
import 'package:flutter/material.dart';
import 'package:nokken/src/core/ui/theme/app_colors.dart';
import 'package:nokken/src/core/ui/theme/app_icons.dart';
import 'package:nokken/src/core/ui/theme/app_text_styles.dart';
import 'package:nokken/src/core/ui/widgets/shared_widgets.dart';

/// A class to manage timeframe selection across the app
class TimeframeSelector {
  /// Standard timeframe options available throughout the app
  static const List<String> standardTimeframes = [
    '3 Months',
    '6 Months',
    '1 Year',
    '5 Years',
    'All Time'
  ];

  /// Returns a filter icon button for AppBar actions
  static Widget dropdownButton({
    required BuildContext context,
    required String selectedTimeframe,
    required Function(String) onTimeframeSelected,
    List<String>? timeframes,
  }) {
    final options = timeframes ?? standardTimeframes;

    return PopupMenuButton<String>(
      icon: Icon(AppIcons.getIcon('filter_list')),
      tooltip: 'Filter time range',
      onSelected: onTimeframeSelected,
      itemBuilder: (BuildContext context) {
        return options.map((String range) {
          return PopupMenuItem<String>(
            value: range,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(range),
                if (selectedTimeframe == range)
                  Icon(AppIcons.getIcon('checkmark'), size: 18)
              ],
            ),
          );
        }).toList();
      },
    );
  }

  /// Returns a card with timeframe chip selectors
  static Widget chipSelector({
    required BuildContext context,
    required String selectedTimeframe,
    required Function(String) onTimeframeSelected,
    List<String>? timeframes,
    String title = 'Time Range',
  }) {
    final options = timeframes ?? standardTimeframes;

    return SharedWidgets.basicCard(
      context: context,
      title: title,
      children: [
        SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Row(
            children: options.map((timeFrame) {
              final isSelected = selectedTimeframe == timeFrame;
              return Padding(
                padding: const EdgeInsets.only(right: 8.0),
                child: ChoiceChip(
                  label: Text(timeFrame),
                  selected: isSelected,
                  onSelected: (selected) {
                    if (selected) {
                      onTimeframeSelected(timeFrame);
                    }
                  },
                  backgroundColor: AppColors.surface,
                  selectedColor: AppColors.primary.withAlpha(100),
                  labelStyle: AppTextStyles.labelMedium.copyWith(
                    color: isSelected ? AppColors.primary : null,
                    fontWeight: isSelected ? FontWeight.bold : null,
                  ),
                ),
              );
            }).toList(),
          ),
        ),
      ],
    );
  }

  /// Utility function to filter a list of items by timeframe
  /// T must have a 'date' property of type DateTime
  static List<T> filterByTimeframe<T>({
    required List<T> items,
    required String timeframe,
    required DateTime Function(T) getDate,
  }) {
    if (timeframe == 'All Time') {
      return items;
    }

    final now = DateTime.now();
    DateTime cutoffDate;

    switch (timeframe) {
      case '3 Months':
        cutoffDate = DateTime(now.year, now.month - 3, now.day);
        break;
      case '6 Months':
        cutoffDate = DateTime(now.year, now.month - 6, now.day);
        break;
      case '1 Year':
        cutoffDate = DateTime(now.year - 1, now.month, now.day);
        break;
      case '5 Years':
        cutoffDate = DateTime(now.year - 5, now.month, now.day);
        break;
      default:
        return items;
    }

    return items.where((item) => getDate(item).isAfter(cutoffDate)).toList();
  }

  /// Returns a DateTimeRange for the specified timeframe
  static DateTimeRange? getDateRangeForTimeframe(String timeframe) {
    final now = DateTime.now();
    final today =
        DateTime(now.year, now.month, now.day); // Normalize to start of day

    DateTime startDate;

    switch (timeframe) {
      case '3 Months':
        startDate = DateTime(now.year, now.month - 3, now.day);
        break;
      case '6 Months':
        startDate = DateTime(now.year, now.month - 6, now.day);
        break;
      case '1 Year':
        startDate = DateTime(now.year - 1, now.month, now.day);
        break;
      case '5 Years':
        startDate = DateTime(now.year - 5, now.month, now.day);
        break;
      case 'All Time':
        // For "All Time", we'll use a date far in the past
        startDate = DateTime(2000, 1, 1);
        break;
      default:
        // Return null for unrecognized timeframes
        return null;
    }

    return DateTimeRange(start: startDate, end: today);
  }
}
