import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/blocs/non_bookable_spaces_bloc/non_bookaable_spaces_bloc.dart';
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/widgets/search_unbookable_spaces_widget.dart';
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/widgets/time_picker_widget.dart';
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/widgets/week_checkbox_title_widget.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/string_utils.dart';

class StepTwoDetailsWidget extends StatelessWidget {
  final TextEditingController pointsController;
  const StepTwoDetailsWidget({
    super.key,
    required this.pointsController,
  });
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 450,
      height: 480,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const WeekDaysCheckboxRow(),
          const SizedBox(
            height: 20,
          ),
          Row(
            children: [
              TitleAndTimePickerWidget(
                title: 'Booking Start Time',
                onTimePicked: (timePicked) {
                  if (timePicked == null) {
                    return;
                  }
                  final nonBookableBloc = context.read<NonBookableSpacesBloc>();

                  if (nonBookableBloc.endTime != null &&
                      isEndTimeAfterStartTime(
                          timePicked, nonBookableBloc.endTime!)) {
                    ScaffoldMessenger.of(context).clearSnackBars();
                    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
                      content:
                          Text("You can't choose start Time Before End time"),
                      duration: Duration(seconds: 2),
                      backgroundColor: ColorsManager.red,
                    ));
                    throw Exception();
                  } else {
                    nonBookableBloc.selectedBookableSpaces.forEach(
                      (e) => e.spaceConfig!.bookingStartTime = timePicked,
                    );
                  }
                },
              ),
              const SizedBox(
                width: 20,
              ),
              TitleAndTimePickerWidget(
                title: 'Booking End Time',
                onTimePicked: (timePicked) {
                  if (timePicked == null) {
                    return;
                  }
                  final nonBookableBloc = context.read<NonBookableSpacesBloc>();
                  if (nonBookableBloc.startTime != null &&
                      isEndTimeAfterStartTime(
                          nonBookableBloc.startTime!, timePicked)) {
                    ScaffoldMessenger.of(context).clearSnackBars();
                    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
                      content:
                          Text("You can't choose End Time After Start time"),
                      duration: Duration(seconds: 2),
                      backgroundColor: ColorsManager.red,
                    ));
                    throw Exception();
                  } else {
                    nonBookableBloc.selectedBookableSpaces.forEach(
                      (e) => e.spaceConfig!.bookingEndTime = timePicked,
                    );
                  }
                },
              )
            ],
          ),
          const SizedBox(
            height: 20,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                '* ',
                style: Theme.of(context)
                    .textTheme
                    .bodyMedium!
                    .copyWith(color: Colors.red),
              ),
              const Text('Points/hrs'),
            ],
          ),
          const SizedBox(
            height: 5,
          ),
          SearchUnbookableSpacesWidget(
            title: 'Ex: 0',
            height: 40,
            onChanged: (p0) {
              context
                  .read<NonBookableSpacesBloc>()
                  .selectedBookableSpaces
                  .forEach(
                    (e) => e.spaceConfig!.cost = int.parse(
                      pointsController.text.isEmpty
                          ? '0'
                          : pointsController.text,
                    ),
                  );
              context
                  .read<NonBookableSpacesBloc>()
                  .add(CheckConfigurValidityEvent());
            },
            controller: pointsController,
            inputFormatters: [FilteringTextInputFormatter.digitsOnly],
            suffix: const SizedBox(),
          ),
        ],
      ),
    );
  }
}

class TitleAndTimePickerWidget extends StatelessWidget {
  final void Function(TimeOfDay? timePicked) onTimePicked;
  final String title;
  const TitleAndTimePickerWidget({
    super.key,
    required this.onTimePicked,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Text(
              '* ',
              style: Theme.of(context)
                  .textTheme
                  .bodyMedium!
                  .copyWith(color: Colors.red),
            ),
            Text(title),
          ],
        ),
        const SizedBox(
          height: 5,
        ),
        TimePickerWidget(
          onTimePicked: onTimePicked,
        ),
      ],
    );
  }
}
