import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/widgets/ui_kit/dialogs/kw_dialog.dart';
import 'package:krow/features/profile/schedule/domain/bloc/schedule_bloc.dart';
import 'package:krow/features/profile/schedule/presentation/widgets/weekly_option_selector.dart';

enum SelectedDatesOption { add, edit, editAll }

Future<void> onEditButtonPress(
  BuildContext context, {
  required bool isWeeklySchedule,
  SelectedDatesOption selectedDatesOption = SelectedDatesOption.edit,
  DateTime? editedDate,
}) async {
  if (selectedDatesOption != SelectedDatesOption.edit) {
    context.read<ScheduleBloc>().add(
          const ScheduleEventChangeMode(
            isInScheduleAddMode: true,
            isInEditScheduleMode: true,
          ),
        );
    return;
  }

  if (!isWeeklySchedule) {
    context.read<ScheduleBloc>().add(
      ScheduleEventChangeMode(
        isWeekly: false,
        isInEditScheduleMode: true,
        editedDate: editedDate,
      ),
    );
    return;
  }

  var isWeeklySelected = false;
  await KwDialog.show(
    context: context,
    icon: Assets.images.icons.menuBoard,
    title: 'update_schedule'.tr(),
    message: 'update_schedule_message'.tr(),
    primaryButtonLabel: 'submit_update'.tr(),
    secondaryButtonLabel: 'cancel'.tr(),
    onPrimaryButtonPressed: (dialogContext) {
      context.read<ScheduleBloc>().add(
            ScheduleEventChangeMode(
              isWeekly: isWeeklySelected,
              isInEditScheduleMode: true,
              editedDate: editedDate,
            ),
          );

      Navigator.of(dialogContext).pop();
    },
    child: WeeklyOptionSelector(
      onWeeklyToggle: (isWeekly) {
        isWeeklySelected = isWeekly;
      },
    ),
  );
}
