import 'package:bloc_library/bloc_library_keys.dart';
import 'package:bloc_library/blocs/todos/todos.dart';
import 'package:bloc_library/models/models.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:todos_app_core/todos_app_core.dart';

class ExtraActions extends StatelessWidget {
  const ExtraActions({super.key = ArchSampleKeys.extraActionsButton});

  @override
  Widget build(BuildContext context) {
    final todosBloc = BlocProvider.of<TodosBloc>(context);
    return BlocBuilder(
      bloc: todosBloc,
      builder: (BuildContext context, TodosState state) {
        if (state is TodosLoaded) {
          final allComplete = (todosBloc.state as TodosLoaded).todos.every(
            (todo) => todo.complete,
          );
          return PopupMenuButton<ExtraAction>(
            key: BlocLibraryKeys.extraActionsPopupMenuButton,
            onSelected: (action) {
              switch (action) {
                case ExtraAction.clearCompleted:
                  todosBloc.add(ClearCompleted());
                  break;
                case ExtraAction.toggleAllComplete:
                  todosBloc.add(ToggleAll());
                  break;
              }
            },
            itemBuilder: (BuildContext context) => <PopupMenuItem<ExtraAction>>[
              PopupMenuItem<ExtraAction>(
                key: ArchSampleKeys.toggleAll,
                value: ExtraAction.toggleAllComplete,
                child: Text(
                  allComplete
                      ? ArchSampleLocalizations.of(context).markAllIncomplete
                      : ArchSampleLocalizations.of(context).markAllComplete,
                ),
              ),
              PopupMenuItem<ExtraAction>(
                key: ArchSampleKeys.clearCompleted,
                value: ExtraAction.clearCompleted,
                child: Text(ArchSampleLocalizations.of(context).clearCompleted),
              ),
            ],
          );
        }
        return Container(key: BlocLibraryKeys.extraActionsEmptyContainer);
      },
    );
  }
}
