using Qwirkle.Views;
using QwirkleClassLibrary.Games;

namespace Qwirkle.Pages;

public partial class SetPlayers : ContentPage
{
    private Game game = ((App)App.Current!).Game;
   
    public SetPlayers()
	{
		InitializeComponent();
        BindingContext = this;

    }

    public EntryPlayer Entry1 { get; set; }
        = new EntryPlayer
        {
            TextOn = "Player 1",
            TextHolder = "Entry tag of player 1"
        };
    public EntryPlayer Entry2 { get; set; }
        = new EntryPlayer
        {
            TextOn = "Player 2",
            TextHolder = "Entry tag of player 2"
        };

    public EntryPlayer Entry3 { get; set; }
        = new EntryPlayer
        {
            TextOn = "Player 3",
            TextHolder = "Entry tag of player 3"
        };

    public EntryPlayer Entry4 { get; set; }
        = new EntryPlayer
        {
            TextOn = "Player 4",
            TextHolder = "Entry tag of player 4"

        };

    void OnButtonContinueClick(object sender, EventArgs e)
    {
        game.PlayerAddNotified += Game_PlayerAddNotified;

        List<string> playerstag = [Entry1.TextIn!, Entry2.TextIn!, Entry3.TextIn!, Entry4.TextIn!];

        if (game.AddPlayerInGame(playerstag))
        {
            game.StartGame();
            game.GiveTilesToPlayers();
            game.SetNextPlayer();
            Shell.Current.GoToAsync("Gameboard");
            //Navigation.PushAsync(new Gameboard());

        }

        game.PlayerAddNotified -= Game_PlayerAddNotified;

    }

    private void Game_PlayerAddNotified(object? sender, QwirkleClassLibrary.Events.AddPlayerNotifiedEventArgs args)
    {
        DisplayAlert("Game notification", args.ReturnedNotified, "Ok ! Lets's go !");
    }
}