<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:partial="clr-namespace:GreadyPoang.ViewsPartial"
             x:Class="GreadyPoang.Views.RoundRunningView"
             xmlns:vm ="clr-namespace:GreadyPoang.ViewModelLayer;assembly=GreadyPoang.ViewModelLayer"
             xmlns:behaviors="clr-namespace:GreadyPoang.Common;assembly=GreadyPoang.Common"
             xmlns:model="clr-namespace:GreadyPoang.EntityLayer;assembly=GreadyPoang.EntityLayer"
             x:DataType="vm:RoundRunningViewModel"
             Title="RoundRunningView">
    <!--xmlns:local="clr-namespace:GreadyPoang.EntityLayer;assembly=GreadyPoang.EntityLayer"-->
    <!--<ContentPage.Behaviors>
        <behaviors:LoadedBehavior Command="{Binding OnLoadedCommand}" />
    </ContentPage.Behaviors>-->
    <Border 
        behaviors:LoadedBehavior.Command="{Binding OnLoadedCommand}"
        Style="{StaticResource Border.Page}" StrokeThickness="4">
        <Grid Style="{StaticResource Grid.Page}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <partial:HeaderView Grid.Row="0"    
                Grid.ColumnSpan="2"                
                ViewTitle="Starta eller fortsätt en registrerad Runda"
                ViewDescription="Deltagarna visas först!" />
            <CollectionView Grid.Row="1" 
                        ItemsSource="{Binding RoundElements}"
                        ItemsLayout="HorizontalList"
                        Margin="2">
                <CollectionView.ItemTemplate>
                    <DataTemplate x:DataType="model:RoundBuilderElement">
                        <Border Stroke="Gray" 
                                Padding="10" 
                                Margin="5"
                                StrokeShape="RoundRectangle 10"
                                Style="{StaticResource StatusBorderStyle}">
                            <Border.GestureRecognizers>
                                <TapGestureRecognizer 
                                Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}}, Path=BindingContext.ParticipantTappedCommand}"
                                CommandParameter="{Binding .}" />
                            </Border.GestureRecognizers>
                            <Grid VerticalOptions="Fill" >
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Label Grid.Row="0" Text="Spelare" />
                                <Label Grid.Row="1" Text="{Binding ParticipantName}" FontAttributes="Bold" Style="{StaticResource StatusLabelStyle}" />
                                <Label Grid.Row="2" Text="{Binding GameRegPoints, StringFormat='Poäng: {0}'}" Style="{StaticResource StatusLabelStyle}" />
                                <Label Grid.Row="3" Text="{Binding Status}" Style="{StaticResource StatusLabelStyle}"/>
                                <Label Grid.Row="4" Text="{Binding GameRoundStartDate, StringFormat='Start: {0:yyyy-MM-dd}'}" Style="{StaticResource StatusLabelStyle}"/>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
            <BoxView 
                Grid.Row="2"
                 Grid.ColumnSpan="2"
                 Margin="0,0,0,5"
                 HeightRequest="1"
                 Color="Black" />
            <Border Grid.Row="3">
                <HorizontalStackLayout>
                    <Grid VerticalOptions="Fill">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" Text="{Binding BuilderObject.GameRoundId, StringFormat='Runda: {0}'}"/>
                        <Label Grid.Row="1" Text="{Binding BuilderObject.ParticipantName}" FontAttributes="Bold" FontSize="Subtitle" />
                        <Entry Grid.Row="2" x:Name="Points" Placeholder="Latest Points" Text="{Binding BuilderObject.GameRegPoints}"  FontAttributes="Bold" FontSize="20" >
                            <Entry.Behaviors>
                                <behaviors:DigitsOnlyBehavior/>
                                <behaviors:EventToCommandBehavior EventName="Completed"
                                                                  Command="{Binding StoreAndHandlePointsAsyncCommand}" />
                            </Entry.Behaviors>
                        </Entry>
                    </Grid>
                    <Button Text="Register Points"  
                            Style="{StaticResource HoverButtonRedStyle}" 
                            Command="{Binding StoreAndHandlePointsAsyncCommand}"/>
                </HorizontalStackLayout>
            </Border>

            <ScrollView  Grid.Row="4" Orientation="Horizontal">
                <HorizontalStackLayout 
                        BindableLayout.ItemsSource="{Binding ScoreColumns}"
                        x:Name="ScoreGrid">
                    <BindableLayout.ItemTemplate>
                        <DataTemplate x:DataType="model:ScoreColumn">
                            <VerticalStackLayout BindableLayout.ItemsSource="{Binding Cells}">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate x:DataType="model:ScoreCell">
                                        <Border
                                                Stroke="Gray"
                                                StrokeThickness="1"
                                                BackgroundColor="{Binding IsHeader, Converter={StaticResource HeaderColorConverter}}"
                                                Margin="1"
                                                Padding="2">
                                            <Label
                                                Text="{Binding Text}"
                                                FontAttributes="{Binding IsHeader, Converter={StaticResource FontWeightConverter}}"
                                                TextColor="{Binding IsHeader, Converter={StaticResource TextColorConverter}}"
                                                HorizontalOptions="Center"
                                                VerticalOptions="Center" />
                                        </Border>
                                    </DataTemplate>
                                </BindableLayout.ItemTemplate>
                            </VerticalStackLayout>
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </HorizontalStackLayout>
            </ScrollView>
        </Grid>
    </Border>
</ContentPage>