import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_calendar_widget.dart';

class KwDatePickerPopup extends StatefulWidget {
  const KwDatePickerPopup({
    super.key,
    this.initialDate,
    this.firstDate,
    this.lastDate,
    this.currentDate,
  });

  final DateTime? initialDate;
  final DateTime? firstDate;
  final DateTime? lastDate;
  final DateTime? currentDate;

  @override
  State<KwDatePickerPopup> createState() => _KwDatePickerPopupState();

  static Future<DateTime?> show({
    required BuildContext context,
    DateTime? initDate,
    DateTime? firstDate,
    DateTime? lastDate,
    DateTime? currentDate,
  }) async {
    return showDialog<DateTime>(
      context: context,
      builder: (context) => KwDatePickerPopup(
        initialDate: initDate,
        firstDate: firstDate,
        lastDate: lastDate,
        currentDate: currentDate,
      ),
    );
  }
}

class _KwDatePickerPopupState extends State<KwDatePickerPopup> {
  DateTime? selectedDate;

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

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          padding: const EdgeInsets.all(24),
          margin: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: AppColors.bgColorLight,
            borderRadius: BorderRadius.circular(24),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              const Row(
                children: [
                  Text(
                    'Select Date from Calendar',
                    style: AppTextStyles.headingH3,
                  ),
                ],
              ),
              const Gap(24),
              Container(
                decoration: BoxDecoration(
                  color: AppColors.grayPrimaryFrame,
                  borderRadius: BorderRadius.circular(12),
                  border: Border.all(color: AppColors.grayTintStroke),
                ),
                child: KwCalendarWidget(
                  onDateSelected: (date) {
                    setState(() {
                      selectedDate = date;
                    });
                  },
                  initialDate: selectedDate ?? widget.initialDate,
                ),
              ),
              const Gap(24),
              KwButton.primary(
                disabled: selectedDate == null,
                label: 'Pick Date',
                onPressed: () {
                  Navigator.of(context).pop(selectedDate);
                },
              ),
              const Gap(8),
              KwButton.outlinedPrimary(
                label: 'Cancel',
                onPressed: () {
                  Navigator.of(context).pop(widget.initialDate);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
