import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:intl/intl.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/widgets/month_picker_widget.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/widgets/year_picker_widget.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';

enum DatePickerType { month, year }

class AnalyticsDateFilterButton extends StatefulWidget {
  const AnalyticsDateFilterButton({
    required this.selectedDate,
    required this.onDateSelected,
    this.datePickerType = DatePickerType.month,
    super.key,
  });

  final DateTime selectedDate;
  final void Function(DateTime)? onDateSelected;
  final DatePickerType datePickerType;

  static final Color _color = ColorsManager.blackColor.withValues(alpha: 0.8);

  @override
  State<AnalyticsDateFilterButton> createState() =>
      _AnalyticsDateFilterButtonState();
}

class _AnalyticsDateFilterButtonState extends State<AnalyticsDateFilterButton> {
  @override
  Widget build(BuildContext context) {
    return TextButton.icon(
      style: TextButton.styleFrom(
        foregroundColor: AnalyticsDateFilterButton._color,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(16),
          side: const BorderSide(
            color: ColorsManager.greyColor,
            width: 1,
          ),
        ),
        backgroundColor: ColorsManager.transparentColor,
        padding: const EdgeInsets.symmetric(
          horizontal: 32,
          vertical: 16,
        ),
      ),
      icon: SvgPicture.asset(
        Assets.blankCalendar,
        height: 20,
        width: 20,
        colorFilter:
            ColorFilter.mode(AnalyticsDateFilterButton._color, BlendMode.srcIn),
      ),
      label: Text(
        _formatDate(widget.selectedDate),
        style: const TextStyle(
          fontWeight: FontWeight.w700,
        ),
      ),
      onPressed: () {
        showDialog<void>(
          context: context,
          builder: (_) => switch (widget.datePickerType) {
            DatePickerType.month => MonthPickerWidget(
                selectedDate: widget.selectedDate,
                onDateSelected: (value) {
                  widget.onDateSelected?.call(value);
                },
              ),
            DatePickerType.year => YearPickerWidget(
                selectedDate: widget.selectedDate,
                onDateSelected: (value) {
                  widget.onDateSelected?.call(value);
                },
              ),
          },
        );
      },
    );
  }

  String _formatDate(DateTime? date) {
    final formatterBasedOnDatePickerType = switch (widget.datePickerType) {
      DatePickerType.month => DateFormat('MMMM yyyy'),
      DatePickerType.year => DateFormat('yyyy'),
    };
    final formattedDate = formatterBasedOnDatePickerType.format(
      date ?? DateTime.now(),
    );
    return formattedDate;
  }
}
