<?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:Gready_Poang.ViewsPartial"
             x:Class="Gready_Poang.Views.RoundStartingView"
             xmlns:vm="clr-namespace:Gready_Poang.CommandClasses"
             xmlns:local="clr-namespace:Gready_Poang.EntityLayer;assembly=Gready_Poang.EntityLayer"
             xmlns:model="clr-namespace:Gready_Poang.EntityLayer;assembly=Gready_Poang.EntityLayer"
             x:DataType="vm:RoundStartingViewModelCommands"
             x:Name="GameRoundStartingPage">
    <!--Title="Starta ny Runda"-->



    <Border Style="{StaticResource Border.Page}" StrokeThickness="4">
        <Grid Style="{StaticResource Grid.Page}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <partial:HeaderView Grid.Row="0"    
                Grid.ColumnSpan="2"                
                ViewTitle="Starta en Spelrunda"
                ViewDescription="Välj deltagare och initiera spel" />
            <Border Grid.Row="1" Stroke="Gold" StrokeThickness="2" BackgroundColor="LemonChiffon"   >
                <ScrollView Orientation="Horizontal">
                    <StackLayout x:Name="ResponsiveStack" Spacing="5" Style="{StaticResource ResponsiveStackStyle}" >
                        <Border     Stroke="Gray"
                        StrokeThickness="2"
                        BackgroundColor="LightGray"
                        Padding="10"
                        Margin="2"
                        HorizontalOptions="Start"
                        VerticalOptions="Center"
                        WidthRequest="250">
                            <Picker ItemsSource="{Binding ParticipantList}"
                                ItemDisplayBinding="{Binding LastNameFirstName}"
                                SelectedItem="{Binding SelectedItem}"
                                TextColor="Black"
                                Title="Välj deltagare"/>
                        </Border>
                        <Border Stroke="Gold" 
                            StrokeThickness="2" 
                            BackgroundColor="LightCyan" >
                            <!--WidthRequest="100">-->
                            <CollectionView 
                            ItemsSource="{Binding RoundElements}"
                            ItemsLayout="HorizontalList"
                            x:Name="ParticipantList"
                            SelectionMode="None">
                                <CollectionView.ItemTemplate>
                                    <DataTemplate x:DataType="model:RoundBuilderElement">
                                        <Border
                                        Padding="2"
                                        Margin="5"
                                        WidthRequest="150"
                                        HeightRequest="100"
                                        Stroke="LightGray"
                                        StrokeThickness="1"
                                        x:Name="RoundElementBorder"
                                        StrokeShape="RoundRectangle 10"
                                        Style="{StaticResource StatusBorderStyle}"
                                        >
                                            <Border.GestureRecognizers>
                                                <TapGestureRecognizer
                                                Command="{Binding BindingContext.ParticipantTappedCommand, Source={x:Reference Name=ParticipantList}}"
                                                CommandParameter="{Binding .}" />
                                            </Border.GestureRecognizers>
                                            <Border Margin="1" Padding="2" StrokeThickness="3" >
                                                <VerticalStackLayout>
                                                    <Label Text="{Binding ParticipantName}" FontAttributes="Bold" FontSize="14" Style="{StaticResource StatusLabelStyle}"/>
                                                    <Label Text="{Binding GameRoundStartDateString}" FontAttributes="Bold" FontSize="14" Style="{StaticResource StatusLabelStyle}"/>
                                                    <Label Text="{Binding StatusString}" FontSize="14" Style="{StaticResource StatusLabelStyle}" />
                                                </VerticalStackLayout>
                                            </Border>
                                        </Border>
                                    </DataTemplate>
                                </CollectionView.ItemTemplate>
                            </CollectionView>
                        </Border>
                        <Border  
                        HorizontalOptions="Start"
                        Padding="2"
                        Margin="5"
                        WidthRequest="150"
                        HeightRequest="100"
                        Stroke="LightGray"
                        StrokeThickness="1"
                        x:Name="RoundElementBorder"
                        BackgroundColor="BlanchedAlmond"
                        StrokeShape="RoundRectangle 10">
                            <VerticalStackLayout Spacing="5" Padding="4">
                                <Button Text="Spara omgång" WidthRequest="130"
                                Style="{StaticResource HoverButtonBlueStyle}"
                                Command="{Binding SaveCommand}" />
                                <Button Text="Rensa" WidthRequest="130"
                                Style="{StaticResource HoverButtonRedStyle}"
                                Command="{Binding RensaCommand}" />
                            </VerticalStackLayout>
                        </Border>
                    </StackLayout>
                </ScrollView>
            </Border>
            <Border Grid.Row="2" Stroke="Gold" BackgroundColor="Ivory" StrokeThickness="2" Padding="5">
                <ScrollView Orientation="Horizontal">
                    <CollectionView ItemsSource="{Binding GameRoundList}" x:Name="OuterList">
                        <CollectionView.ItemTemplate>
                            <DataTemplate  x:DataType="model:RoundBuilderGroup">
                                <VerticalStackLayout Padding="10">
                                    <!-- Gruppens rubrik -->
                                    <HorizontalStackLayout>
                                        <Label Text="{Binding GameRoundId, StringFormat='Runda: {0}'}" FontAttributes="Bold" FontSize="18"  />
                                        <Label Text="{Binding GameRoundStartDate, StringFormat='Startdatum: {0}'}" FontAttributes="Bold" FontSize="18"/>
                                        <Label Text="{Binding Status, StringFormat='Status: {0}'}" FontAttributes="Bold" FontSize="18" />
                                    </HorizontalStackLayout>
                                    <!-- Horisontell lista med deltagare -->
                                    <CollectionView ItemsSource="{Binding Elements}"
                                                ItemsLayout="HorizontalList"
                                                x:Name="InnerList" 
                                                Margin="0,10,0,10"
                                                ItemSizingStrategy="MeasureAllItems">
                                        <CollectionView.ItemTemplate>
                                            <DataTemplate x:DataType="model:RoundBuilderElement">
                                                <Border 
                                                Stroke="Gray" 
                                                Padding="5" 
                                                Margin="5,0"
                                                Style="{StaticResource StatusBorderStyle}" >
                                                    <Border.GestureRecognizers>
                                                        <TapGestureRecognizer
                                                        Command="{Binding Path=BindingContext.ElementTappedCommand,Source={x:Reference OuterList}}"
                                                        CommandParameter="{Binding .}" />
                                                    </Border.GestureRecognizers>

                                                    <VerticalStackLayout>
                                                        <Label Text="{Binding ParticipantName}" FontAttributes="Bold" Style="{StaticResource StatusLabelStyle}" />
                                                        <Label Text="{Binding GameRegPoints, StringFormat='Poäng: {0}'}" Style="{StaticResource StatusLabelStyle}"/>
                                                        <Label Text="{Binding  GameRoundRegNr, StringFormat='RegNr: {0}'}" Style="{StaticResource StatusLabelStyle}"/>
                                                    </VerticalStackLayout>
                                                </Border>
                                            </DataTemplate>
                                        </CollectionView.ItemTemplate>
                                    </CollectionView>
                                </VerticalStackLayout>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
                    </CollectionView>

                </ScrollView>
            </Border>
        </Grid>
    </Border>
</ContentPage>