import 'package:flutter/material.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

class YearPickerWidget extends StatefulWidget {
  const YearPickerWidget({
    super.key,
    required this.selectedDate,
    required this.onDateSelected,
  });

  final DateTime selectedDate;
  final ValueChanged<DateTime>? onDateSelected;

  @override
  State<YearPickerWidget> createState() => _YearPickerWidgetState();
}

class _YearPickerWidgetState extends State<YearPickerWidget> {
  late int _currentYear;

  static final years = List.generate(
    DateTime.now().year - (DateTime.now().year - 5) + 1,
    (index) => (2020 + index),
  ).where((year) => year <= DateTime.now().year).toList();

  @override
  void initState() {
    super.initState();
    _currentYear = widget.selectedDate.year;
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: ColorsManager.whiteColors,
      child: Container(
        padding: const EdgeInsetsDirectional.all(20),
        width: 320,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildMonthsGrid(),
            const SizedBox(height: 20),
            Row(
              spacing: 12,
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                FilledButton(
                  onPressed: () => Navigator.pop(context),
                  style: FilledButton.styleFrom(
                    fixedSize: const Size(106, 40),
                    backgroundColor: const Color(0xFFEDF2F7),
                    padding: const EdgeInsetsDirectional.symmetric(
                      vertical: 10,
                      horizontal: 16,
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20),
                    ),
                  ),
                  child: Text(
                    'Cancel',
                    style: context.textTheme.titleSmall?.copyWith(
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                      color: ColorsManager.grey700,
                    ),
                  ),
                ),
                FilledButton(
                  onPressed: () {
                    Navigator.pop(context);
                    final date = DateTime(_currentYear);
                    widget.onDateSelected?.call(date);
                  },
                  style: FilledButton.styleFrom(
                    fixedSize: const Size(106, 40),
                    backgroundColor: ColorsManager.vividBlue.withValues(
                      alpha: 0.7,
                    ),
                    padding: const EdgeInsetsDirectional.symmetric(
                      vertical: 10,
                      horizontal: 16,
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20),
                    ),
                  ),
                  child: Text(
                    'Done',
                    style: context.textTheme.titleSmall?.copyWith(
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                      color: ColorsManager.whiteColors,
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildMonthsGrid() {
    return GridView.builder(
      shrinkWrap: true,
      itemCount: years.length,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        childAspectRatio: 2.5,
        mainAxisSpacing: 8,
        mainAxisExtent: 30,
      ),
      itemBuilder: (context, index) {
        final isSelected = _currentYear == years[index];
        return InkWell(
          onTap: () => setState(() => _currentYear = years[index]),
          child: DecoratedBox(
            decoration: BoxDecoration(
              color: const Color(0xFFEDF2F7),
              borderRadius: BorderRadius.only(
                topLeft: index % 3 == 0 ? const Radius.circular(16) : Radius.zero,
                bottomLeft: index % 3 == 0 ? const Radius.circular(16) : Radius.zero,
                topRight: index % 3 == 2 ? const Radius.circular(16) : Radius.zero,
                bottomRight:
                    index % 3 == 2 ? const Radius.circular(16) : Radius.zero,
              ),
            ),
            child: Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: isSelected
                    ? ColorsManager.vividBlue.withValues(alpha: 0.7)
                    : const Color(0xFFEDF2F7),
                borderRadius:
                    isSelected ? BorderRadius.circular(15) : BorderRadius.zero,
              ),
              child: Text(
                years[index].toString(),
                style: context.textTheme.titleSmall?.copyWith(
                  fontSize: 12,
                  color: isSelected
                      ? ColorsManager.whiteColors
                      : ColorsManager.blackColor.withValues(alpha: 0.8),
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
