<?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:local="clr-namespace:Linaris"
             xmlns:model="clr-namespace:Model;assembly=Model"
             xmlns:converters="clr-namespace:Linaris.Converters"
             x:Class="Linaris.LocalFilesPage"
             Title="LocalFilesPage"
             Style="{StaticResource PageFlyoutTrigger}">

    <ContentPage.Resources>
        <converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
    </ContentPage.Resources>

    <Grid Style="{StaticResource GridFlyoutTrigger}" RowDefinitions="*,100">
        <local:Layout Grid.Column="0"/>
        <ScrollView Grid.Column="1" Grid.Row="0" BackgroundColor="#404040">
            <VerticalStackLayout>
                <SearchBar Style="{StaticResource SearchBar}"/>
                <FlexLayout Direction="Row" AlignItems="Start" BindableLayout.ItemsSource="{Binding CustomTitles}" JustifyContent="SpaceAround" Wrap="Wrap">
                    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <Grid ColumnDefinitions="2*,*">
                                <VerticalStackLayout Margin="10" Grid.ColumnSpan="2">
                                    <Image Source="{Binding ImageURL}" Aspect="Fill" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="150" WidthRequest="150" IsAnimationPlaying="True" InputTransparent="False">
                                        <Image.GestureRecognizers>
                                            <TapGestureRecognizer Tapped="ShowSubMenu"/>
                                        </Image.GestureRecognizers>
                                    </Image>
                                    <Label Text="{Binding Name}" Style="{StaticResource Titre}" HorizontalTextAlignment="Center" WidthRequest="250" LineBreakMode="TailTruncation"/>
                                </VerticalStackLayout>

                                <Grid IsVisible="{Binding IsSubMenuVisible}" BackgroundColor="Grey" HeightRequest="125" WidthRequest="155" Grid.Column="0" RowDefinitions="*,*,*">
                                    <Button Grid.Row="0" Text="Ajouter à une playlist" Clicked="ShowPlaylistMenu"/>
                                    <Button Grid.Row="1" Text="Supprimer" Clicked="RemoveCustomTitle"/>
                                    <Button Grid.Row="3" Text="Changer l'image" Clicked="ChangeImage"/>
                                </Grid>
                                <ScrollView Grid.Column="1" IsVisible="{Binding IsPlaylistMenuVisible}" BackgroundColor="Grey" WidthRequest="150" HeightRequest="200">
                                    <StackLayout>
                                        <Button Text="+ Nouvelle playlist" TextColor="CornflowerBlue" FontAttributes="Bold" Clicked="ShowNewPlaylistMenu" IsVisible="{Binding IsNewPlaylistMenuVisible, Converter={StaticResource InverseBooleanConverter}}"/>
                                        <Entry Placeholder="Nom de la playlist" PlaceholderColor="LightGrey" IsVisible="{Binding IsNewPlaylistMenuVisible}" Completed="AddPlaylist"/>
                                        <StackLayout BindableLayout.ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}}, Path=Playlists}">
                                            <BindableLayout.ItemTemplate>
                                                <DataTemplate>
                                                    <Button Text="{Binding Name}" Clicked="AddToPlaylist"/>
                                                </DataTemplate>
                                            </BindableLayout.ItemTemplate>
                                        </StackLayout>
                                    </StackLayout>
                                </ScrollView>

                                <Grid.GestureRecognizers>
                                    <TapGestureRecognizer />
                                </Grid.GestureRecognizers>
                            </Grid>
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </FlexLayout>
                <Button WidthRequest="300" HeightRequest="50" Margin="0,20,0,20" BackgroundColor="{StaticResource Primary}" Text="Ajouter d'autres titres" Clicked="AddTitles"/>
            </VerticalStackLayout>
        </ScrollView>
        <local:FooterPage Grid.Row="1" Grid.ColumnSpan="2"/>
        <Grid.GestureRecognizers>
            <TapGestureRecognizer Tapped="ResetAll"/>
        </Grid.GestureRecognizers>
    </Grid>
</ContentPage>