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

import '../screens/home.dart';
import '../screens/game.dart';
import '../provider/data.dart';
import '../utils/constants.dart';

class DialogGameOver extends StatelessWidget {
  final GameOver gameOver;
  const DialogGameOver(this.gameOver, {super.key});

  @override
  Widget build(BuildContext context) {
    final Data myProvider = Provider.of<Data>(context);

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: myProvider.searching == true
          ? const Center(child: CircularProgressIndicator())
          : WillPopScope(
              onWillPop: () async => false,
              child: AlertDialog(
                title: Row(
                  children: [
                    Icon(gameOver.icon),
                    const SizedBox(width: 10.0),
                    Text(gameOver.title),
                  ],
                ),
                content: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      if (gameOver == GameOver.defeat)
                        Text('Le mot secret était ${myProvider.secretWord}'),
                      const Text('Rejouer ?'),
                    ],
                  ),
                ),
                actions: [
                  TextButton(
                    child: const Text('QUITTER'),
                    onPressed: () {
                      Navigator.pushNamed(context, Home.id);
                    },
                  ),
                  TextButton(
                    child: const Text('REJOUER'),
                    onPressed: () async {
                      myProvider.resetSuccessAndErrors();
                      await const Game().pickWord(context, myProvider);
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              ),
            ),
    );
  }
}