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

import 'package:jeweled/cubit/game_cubit.dart';
import 'package:jeweled/models/game.dart';
import 'package:jeweled/ui/widgets/app_titles.dart';

class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
  const GlobalAppBar({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<GameCubit, GameState>(
      builder: (BuildContext context, GameState gameState) {
        final Game currentGame = gameState.currentGame;

        final List<Widget> menuActions = [];

        if (currentGame.isRunning) {
          menuActions.add(TextButton(
            child: const Image(
              image: AssetImage('assets/icons/button_back.png'),
              fit: BoxFit.fill,
            ),
            onPressed: () {},
            onLongPress: () {
              final GameCubit gameCubit = BlocProvider.of<GameCubit>(context);
              gameCubit.quitGame();
            },
          ));
        }

        return AppBar(
          title: const AppTitle(text: 'app_name'),
          actions: menuActions,
        );
      },
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(50);
}