using GreadyPoang.CommandClasses;
using GreadyPoang.EntityLayer;

namespace GreadyPoang.Views;

public partial class RoundRunningView : ContentPage
{
    public RoundRunningView(RoundRunningViewModelCommands viewModel)
    {
        InitializeComponent();
        ViewModel = viewModel;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        BindingContext = ViewModel;
        ViewModel.Get();
        BuildScoreGrid(ViewModel.PlayerColumns); // <-- här bygger du layouten

    }
    public RoundRunningViewModelCommands ViewModel { get; set; }
    //public int GameRoundId { get; set; }

    //--------------------------------------------------------------



    private void BuildScoreGrid(IEnumerable<PlayerColumn> columns)
    {
        ScoreGrid.ColumnDefinitions.Clear();
        ScoreGrid.Children.Clear();

        int colIndex = 0;
        foreach (var column in columns)
        {
            ScoreGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });

            var stack = new VerticalStackLayout();

            stack.Children.Add(new Label
            {
                Text = column.PlayerName,
                FontAttributes = FontAttributes.Bold,
                HorizontalOptions = LayoutOptions.Center
            });

            foreach (var value in column.Values)
            {
                stack.Children.Add(new Label
                {
                    Text = value,
                    HorizontalOptions = LayoutOptions.Center
                });
            }

            ScoreGrid.Children.Add(stack);
            Grid.SetColumn(stack, colIndex++);
        }
    }
}


