import 'package:flutter/material.dart';

import '../../../../extensions/date_time.dart';
import '../../../../state/app/infrastructure/loadable_state/view/loadable_state_consumer.dart';
import '../../../../state/app/modules/timetable/bloc/timetable_bloc.dart';
import '../../../../state/app/modules/timetable/bloc/timetable_state.dart';
import '../../../../widget/centered_leading.dart';
import '../../../../widget/placeholder_view.dart';
import '../details/delete_custom_event.dart';
import 'custom_event_edit_dialog.dart';

class CustomEventsView extends StatelessWidget {
  const CustomEventsView({super.key});

  void _openCreateDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (_) => const CustomEventEditDialog(),
      barrierDismissible: false,
    );
  }

  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      title: const Text('Eigene Termine'),
      actions: [
        IconButton(
          icon: const Icon(Icons.add),
          tooltip: 'Termin erstellen',
          onPressed: () => _openCreateDialog(context),
        ),
      ],
    ),
    body: LoadableStateConsumer<TimetableBloc, TimetableState>(
      child: (state, _) {
        final events = state.customEvents?.events ?? const [];

        if (events.isEmpty) {
          return PlaceholderView(
            icon: Icons.calendar_today_outlined,
            text: 'Keine Einträge vorhanden',
            button: TextButton(
              onPressed: () => _openCreateDialog(context),
              child: const Text('Termin erstellen'),
            ),
          );
        }

        return ListView(
          children: events
              .map(
                (e) => ListTile(
                  title: Text(e.title),
                  subtitle: Text(
                    '${e.rrule.isNotEmpty ? "wiederholend, " : ""}'
                    'beginnend ${e.startDate.formatRelative()}',
                  ),
                  leading: CenteredLeading(
                    Icon(
                      e.rrule.isEmpty
                          ? Icons.event_outlined
                          : Icons.event_repeat_outlined,
                    ),
                  ),
                  trailing: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      IconButton(
                        icon: const Icon(Icons.edit_outlined),
                        tooltip: 'Bearbeiten',
                        onPressed: () => showDialog(
                          context: context,
                          builder: (_) =>
                              CustomEventEditDialog(existingEvent: e),
                        ),
                      ),
                      IconButton(
                        icon: const Icon(Icons.delete_outline),
                        tooltip: 'Löschen',
                        onPressed: () =>
                            showDeleteCustomEventDialog(context, e),
                      ),
                    ],
                  ),
                ),
              )
              .toList(),
        );
      },
    ),
  );
}
