<?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:vm="clr-namespace:LolApp.ViewModels"
             xmlns:myviews="clr-namespace:LolApp.ContentViews"
             xmlns:appvm="clr-namespace:LolApp.ViewModels"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="LolApp.AddChampionPage"
             Title="AddChampionPage"
             x:Name="root">
    <Grid RowDefinitions="Auto,*, Auto" BackgroundColor="{StaticResource Black}">
        <VerticalStackLayout>
            <Label Text="Nouveau Champion" IsVisible="{Binding IsNew}"
                   Style="{StaticResource title}"/>
            <Label Text="Modifier le Champion" IsVisible="{Binding IsNew, Converter={StaticResource invertedBoolConverter}}"
                   Style="{StaticResource title}"/>
            <Grid><Line Stroke="{StaticResource Primary}"
                  X1="0" Y1="0" X2="200" Y2="0"
                  HorizontalOptions="Center"/>
            </Grid>
        </VerticalStackLayout>
        <ScrollView Grid.Row="1">
            <Grid ColumnDefinitions="*, 3*" RowDefinitions="Auto, Auto, Auto, 162, 162, Auto, Auto, Auto, Auto">
                <Label Text="Nom :"
                       Style="{StaticResource labelForEntry}"/>
                <Entry Grid.Column="1" Placeholder="Nom du champion" Text="{Binding Champion.Name}"
                       Style="{StaticResource defaultEntry}"
                       IsEnabled="{Binding IsNew}"/>

                <Label Text="Icone :" Grid.Row="1" Style="{StaticResource labelForEntry}"/>
                <ImageButton Grid.Row="1" Grid.Column="1" HeightRequest="42" WidthRequest="42"
                             Source="{Binding Champion.IconBase64, TargetNullValue='lol.png',
                             Converter={StaticResource base64ToImageSourceConverter}}"
                             BackgroundColor="{StaticResource Secondary}"
                             HorizontalOptions="Start"
                             Margin="6"
                             Command="{Binding PickIconCommand}"/>
                <Label Text="Image :" Grid.Row="2" Style="{StaticResource labelForEntry}"/>
                <Grid Grid.Row="2" Grid.Column="1" x:Name="largeImageGrid" Margin="0, 0, 12, 0">
                    <ImageButton WidthRequest="{Binding Width, Source={x:Reference largeImageGrid}}"
                                 HeightRequest="150"
                                 Source="{Binding Champion.LargeImageBase64, TargetNullValue='lollogo.jpg',
                                 Converter={StaticResource base64ToImageSourceConverter}}"
                                 BackgroundColor="{StaticResource Secondary}"
                                 HorizontalOptions="Start" 
                                 Margin="6" 
                                 Command="{Binding PickLargeImageCommand}"/>
                </Grid>
                <Label Text="Bio :" Grid.Row="3"
                       Style="{StaticResource labelForEntry}"/>
                <Editor Grid.Column="1" Grid.Row="3"
                        Text="{Binding Champion.Bio}" Style="{StaticResource defaultEditor}"/>
                <Label Text="Classe :" Grid.Row="4"
                       Style="{StaticResource labelForEntry}"/>
                <myviews:ChampionClassSelector Grid.Row="4" Grid.Column="1" MaximumWidthRequest="{OnPlatform WinUI=400}"
                    CheckedColor="{StaticResource Primary}"
                    UncheckedColor="{StaticResource Secondary}"
                    SelectedValue="{Binding Champion.ChampionClass, Mode=TwoWay}"/>
                <Label Text="Caractéristiques :" Grid.Row="5" Grid.RowSpan="2"
                       Style="{StaticResource labelForEntry}" VerticalOptions="Start"/>
                <Border Stroke="{StaticResource Secondary}" Grid.Row="5" Grid.Column="1" VerticalOptions="FillAndExpand">                <ListView ItemsSource="{Binding Champion.Characteristics}"
                          Margin="6" HeightRequest="100" HorizontalOptions="Fill" VerticalOptions="Fill"
                          BackgroundColor="{StaticResource Black}" SeparatorColor="{StaticResource Secondary}" >
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <ViewCell.ContextActions>
                                    <MenuItem Command="{Binding Source={x:Reference root}, Path=BindingContext.RemoveCharacteristicCommand}"
                                      CommandParameter="{Binding .}"
                                      IsDestructive="True" Text="Delete"/>
                                </ViewCell.ContextActions>
                                <Border Margin="0,4" BackgroundColor="{StaticResource Secondary}">
                                    <Border.StrokeShape>
                                        <RoundRectangle CornerRadius="10, 10, 0, 10"/>
                                    </Border.StrokeShape>
                                    <Grid ColumnDefinitions="*, Auto">
                                        <Label Text="{Binding Key}" TextColor="{StaticResource Black}"
                                               HorizontalOptions="Start" VerticalOptions="Center" Margin="4, 0, 0, 0"/>
                                        <Label Text="{Binding Value}" Grid.Column="1" TextColor="{StaticResource Black}"
                                               HorizontalOptions="End" VerticalOptions="Center" Margin="0, 0, 4, 0"/>
                                    </Grid>
                                </Border>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
                    </Border>
                <Grid Grid.Column="1" Grid.Row="6" ColumnDefinitions="*, 58, Auto">
                    <Entry Style="{StaticResource defaultEntry}" Placeholder="Caractéristique" Text="{Binding NewCharacteristicDescription}"/>
                    <Entry Style="{StaticResource defaultEntry}" Placeholder="Valeur" Grid.Column="1" Text="{Binding NewCharacteristicValue}" Keyboard="Numeric">
                        <Entry.Behaviors>
                            <toolkit:NumericValidationBehavior Flags="ValidateOnValueChanged"
                                                               MinimumValue="0"
                                                               MaximumValue="9999999"
                                                               MaximumDecimalPlaces="0"
                                                               InvalidStyle="{StaticResource InvalidEntryStyle}"
                                                               ValidStyle="{StaticResource defaultEntry}"/>
                        </Entry.Behaviors>
                    </Entry>
                    <Button Grid.Column="2" Margin="4,8" CornerRadius="22"
                            Text="{StaticResource plus}"
                            Command="{Binding AddCharacteristicCommand}"/>
                </Grid>
                <Label Style="{StaticResource labelForEntry}" Text="Compétences :" Grid.Row="7" VerticalOptions="Start"/>
                <Grid Grid.Row="7" Grid.Column="1" ColumnDefinitions="*, Auto">
                    <ListView ItemsSource="{Binding Champion.Skills}" Margin="6"
                              HeightRequest="100" HorizontalOptions="Fill" VerticalOptions="Fill" HasUnevenRows="True"
                          BackgroundColor="{StaticResource Black}" SeparatorColor="{StaticResource Secondary}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Grid RowDefinitions="Auto, Auto, *">
                                        <Grid.Resources>
                                            <Style TargetType="Label">
                                                <Setter Property="TextColor" Value="{StaticResource Primary}"/>
                                            </Style>

                                        </Grid.Resources>
                                        <Label Text="{Binding Name}" FontSize="Medium" FontAttributes="Bold" VerticalOptions="Center"/>
                                        <Label Text="{Binding Type}" Grid.Row="1" VerticalOptions="Center" FontAttributes="Italic" FontSize="Micro"/>
                                        <Label Text="{Binding Description}" FontSize="Micro" FontAttributes="Italic"
                                               Grid.Row="2"/>
                                    </Grid>

                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                    <Button Grid.Column="1" Margin="4,8" CornerRadius="22"
                            Text="{StaticResource plus}" VerticalOptions="Start"
                            Command="{Binding AddSkillCommand}"/>
                </Grid>
            </Grid>
        </ScrollView>
        <HorizontalStackLayout Grid.Row="2" HorizontalOptions="Center" Spacing="40" Margin="0, 10, 0, 20">
            <Button Text="Ajouter" Command="{Binding AddChampionCommand}" IsVisible="{Binding IsNew}"/>
            <Button Text="Modifier" Command="{Binding EditChampionCommand}" IsVisible="{Binding IsNew, Converter={StaticResource invertedBoolConverter}}"/>
            <Button Text="Annuler" Command="{Binding CancelCommand}"/>
        </HorizontalStackLayout>
    </Grid>
    
</ContentPage>
