import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.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/features/create_event/presentation/event_date_section/create_event_calendar_widget.dart';

class CreateEventDatePopup extends StatefulWidget {
  final DateTime? initDate;

  const CreateEventDatePopup({
    super.key,
    this.initDate,
  });

  @override
  State<CreateEventDatePopup> createState() => _CreateEventDatePopupState();

  static Future<DateTime?> show(
      {required BuildContext context, DateTime? initDate}) async {
    return showDialog<DateTime>(
      context: context,
      builder: (context) => CreateEventDatePopup(
        initDate: initDate,
      ),
    );
  }
}

class _CreateEventDatePopupState extends State<CreateEventDatePopup> {
  DateTime? selectedDate;

  @override
  void initState() {
    selectedDate = widget.initDate;
    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: KwBoxDecorations.white24,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              buildHeader(),
              const Gap(24),
              _buildCalendar(),
              const Gap(24),
              ..._buttonGroup(),
            ],
          ),
        ),
      ),
    );
  }

  Row buildHeader() {
    return const Row(
      children: [
        Text('Select Date from Calendar', style: AppTextStyles.headingH3),
      ],
    );
  }

  List<Widget> _buttonGroup() {
    return [
      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.initDate);
        },
      ),
    ];
  }

  _buildCalendar() {
    return Container(
      decoration: BoxDecoration(
        color: AppColors.grayPrimaryFrame,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppColors.grayTintStroke),
      ),
      child: CreateEventCalendar(
        onDateSelected: (date) {
          setState(() {
            selectedDate = date;
          });
        },
        initialDate: selectedDate ?? widget.initDate,
      ),
    );
  }
}
