using System.Diagnostics;

namespace Trek_12.Views;

using Models.Game;
using Stub;

public partial class PageBoard : ContentPage
{
    public Stub MyStub { get; set; } = new Stub();

    public PageBoard()
	{
		InitializeComponent();
        BindingContext = MyStub;
        MyStub.ThePartie.InitializeGame(MyStub.ThePartie.UsedMap, MyStub.ListPlayer[0]);
        MyStub.ThePartie.DiceRolled += ThePartie_DiceRolled;
    }

    private void ThePartie_DiceRolled(object? sender, Models.Events.DiceRolledEventArgs e)
    {
        MyStub.ThePartie.Dice1.Roll();
    }

    private void OnOperationCellSelected(object sender, SelectionChangedEventArgs e)
    {
        Debug.WriteLine("OnOperationCellSelected"); // Debug
        if (e.CurrentSelection.Count > 0) // Si un élément est sélectionné
        {
            var selectedCell = (OperationCell)e.CurrentSelection[0];
            if (selectedCell != null && !selectedCell.IsChecked)
            {
                selectedCell.Check();
                Debug.WriteLine("OperationCell at ({0}, {1}) is checked", selectedCell.X, selectedCell.Y); // Debug
            }
            ((CollectionView)sender).SelectedItem = null; // Déselectionne l'élément pour la CollectionView
        }
    }
}