import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../api/mhsl/custom_timetable_event/custom_timetable_event.dart';
import '../../../../state/app/modules/timetable/bloc/timetable_bloc.dart';
import '../../../../widget/confirm_dialog.dart';
import '../../../../widget/demo_restricted.dart';

Completer<void> showDeleteCustomEventDialog(
  BuildContext context,
  CustomTimetableEvent event,
) {
  final completer = Completer<void>();
  if (guardDemoAction(context)) return completer;
  final bloc = context.read<TimetableBloc>();
  ConfirmDialog(
    title: 'Termin löschen',
    content:
        'Der ${event.rrule.isEmpty ? "Termin" : "Serientermin"} wird unwiederruflich gelöscht.',
    confirmButton: 'Löschen',
    onConfirmAsync: () async {
      await bloc.removeCustomEvent(event.id);
      completer.complete();
    },
  ).asDialog(context);
  return completer;
}
