<?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:view="clr-namespace:LivreLand.View"
             xmlns:contentView="clr-namespace:LivreLand.View.ContentViews"    
             xmlns:viewModel="clr-namespace:ViewModels;assembly=ViewModels"
             x:Class="LivreLand.View.EmpruntsPretsView"
             Title="EmpruntsPretsView">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <view:HeaderPage HeaderTitle="En prêt"
                         HeaderBackButtonText="Mes livres"
                         HeaderPlusButtonVisible="True"
                         HeaderSwitchButtonVisible="True"
                         ButtonPlusTappedCommand="{Binding EmpruntsPretsVM.Navigator.PopupHomePlusNavigationCommand}"
                         ButtonBackTappedCommand="{Binding EmpruntsPretsVM.Navigator.PopupBackButtonNavigationCommand}"
                         Grid.Row="0"/>
        <contentView:EmpruntsPretsButtonView Grid.Row="2"
                                             PretButtonBackgroundColor="{Binding EmpruntsPretsVM.PretButtonBackgroundColor}"
                                             PretButtonIsEnabled="{Binding EmpruntsPretsVM.PretButtonIsEnabled}"
                                             PretsButtonCommand="{Binding EmpruntsPretsVM.PretsButtonCommand}"
                                             EmpruntButtonBackgroundColor="{Binding EmpruntsPretsVM.EmpruntButtonBackgroundColor}"
                                             EmpruntButtonIsEnabled="{Binding EmpruntsPretsVM.EmpruntButtonIsEnabled}"
                                             EmpruntsButtonCommand="{Binding EmpruntsPretsVM.EmpruntsButtonCommand}"/>
        <ScrollView Grid.Row="4">
            <Grid>
                <CollectionView ItemsSource="{Binding EmpruntsPretsVM.Manager.AllCurrentGroupedLoans}"
                                SelectedItem="{Binding EmpruntsPretsVM.Manager.SelectedLoan}"
                                SelectionMode="Single"
                                SelectionChangedCommand="{Binding EmpruntsPretsVM.OnSelectionLoanChangedCommand}"
                                SelectionChangedCommandParameter="{Binding EmpruntsPretsVM.Manager.SelectedBook}"
                                IsVisible="{Binding EmpruntsPretsVM.PretCollectionIsVisible}"
                                IsGrouped="True">
                    <CollectionView.GroupHeaderTemplate>
                        <DataTemplate>
                            <Grid BackgroundColor="{AppThemeBinding Light={StaticResource HeaderGray}, Dark={StaticResource Gray900}}"
                                  Grid.Row="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10"/>
                                    <ColumnDefinition Width="auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Label VerticalOptions="Center"
                                       Style="{StaticResource HeaderCollectionViewText}"
                                       Grid.Column="1">
                                    <Label.Text>
                                        <MultiBinding StringFormat="{}{0} {1}">
                                            <Binding Path="Key.FirstName" />
                                            <Binding Path="Key.LastName" />
                                        </MultiBinding>
                                    </Label.Text>
                                </Label>
                            </Grid>
                        </DataTemplate>
                    </CollectionView.GroupHeaderTemplate>
                    <CollectionView.ItemTemplate>
                        <DataTemplate x:DataType="viewModel:LoanVM">
                            <VerticalStackLayout Margin="10"
                                                 Spacing="20">
                                <VisualStateManager.VisualStateGroups x:Name="SelectionStates">
                                    <VisualStateGroup>
                                        <VisualState x:Name="Selected">
                                            <VisualState.Setters>
                                                <Setter Property="BackgroundColor" Value="Transparent" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto"/>
                                        <ColumnDefinition Width="10"/>
                                        <ColumnDefinition Width="auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid Grid.Column="0"
                                          Grid.RowSpan="5">
                                        <Border Padding="-5,-5,-5,1"
                                                Margin="10,0,0,0"
                                                HeightRequest="100"
                                                WidthRequest="62"
                                                HorizontalOptions="Center"
                                                Stroke="{StaticResource Gray}"
                                                StrokeShape="RoundRectangle 3"
                                                StrokeThickness="3">
                                            <Border HorizontalOptions="Center"
                                                    Stroke="{StaticResource Gray}"
                                                    StrokeShape="RoundRectangle 3"
                                                    StrokeThickness="3">
                                                <Image Source="{Binding Book.ImageSmall}"
                                                       Aspect="AspectFill"
                                                       Grid.Column="0"
                                                       Grid.RowSpan="5"/>
                                            </Border>
                                        </Border>
                                        <Image Source="bookmark_red.png"
                                               HeightRequest="10"
                                               WidthRequest="10"
                                               Margin="0,97,20,0"
                                               VerticalOptions="End"/>
                                    </Grid>
                                    <Label Text="{Binding Book.Title}"
                                           Style="{StaticResource MasterTitleBookText}"
                                           Grid.Column="2"
                                           Grid.Row="0"/>
                                    <Label Text="Author"
                                           Style="{StaticResource MasterAuthorBookText}"
                                           Grid.Column="2"
                                           Grid.Row="1"/>
                                    <Label Text="{Binding Book.Status}"
                                           Style="{StaticResource MasterStateBookText}"                                   
                                           Grid.Column="2"
                                           Grid.Row="2"/>
                                    <contentView:StarNotationView Grid.Column="2"
                                                                  Grid.Row="4"/>
                                </Grid>
                                <contentView:SeparatorCutStartView/>
                            </VerticalStackLayout>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
                <CollectionView ItemsSource="{Binding EmpruntsPretsVM.Manager.AllCurrentGroupedBorrowings}"
                                SelectedItem="{Binding EmpruntsPretsVM.Manager.SelectedBorrowing}"
                                SelectionMode="Single"
                                SelectionChangedCommand="{Binding EmpruntsPretsVM.OnSelectionBorrowingChangedCommand}"
                                SelectionChangedCommandParameter="{Binding EmpruntsPretsVM.Manager.SelectedBorrowing}"
                                IsVisible="{Binding EmpruntsPretsVM.EmpruntCollectionIsVisible}"
                                IsGrouped="True">
                    <CollectionView.GroupHeaderTemplate>
                        <DataTemplate>
                            <Grid BackgroundColor="{AppThemeBinding Light={StaticResource HeaderGray}, Dark={StaticResource Gray900}}"
                                  Grid.Row="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10"/>
                                    <ColumnDefinition Width="auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Label VerticalOptions="Center"
                                       Style="{StaticResource HeaderCollectionViewText}"
                                       Grid.Column="1">
                                    <Label.Text>
                                        <MultiBinding StringFormat="{}{0} {1}">
                                            <Binding Path="Key.FirstName" />
                                            <Binding Path="Key.LastName" />
                                        </MultiBinding>
                                    </Label.Text>
                                </Label>
                            </Grid>
                        </DataTemplate>
                    </CollectionView.GroupHeaderTemplate>
                    <CollectionView.ItemTemplate>
                        <DataTemplate x:DataType="viewModel:BorrowingVM">
                            <VerticalStackLayout Margin="10"
                                                 Spacing="20">
                                <VisualStateManager.VisualStateGroups x:Name="SelectionStates">
                                    <VisualStateGroup>
                                        <VisualState x:Name="Selected">
                                            <VisualState.Setters>
                                                <Setter Property="BackgroundColor" Value="Transparent" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto"/>
                                        <ColumnDefinition Width="10"/>
                                        <ColumnDefinition Width="auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid Grid.Column="0"
                                          Grid.RowSpan="5">
                                        <Border Padding="-5,-5,-5,1"
                                                Margin="10,0,0,0"
                                                HeightRequest="100"
                                                WidthRequest="62"
                                                HorizontalOptions="Center"
                                                Stroke="{StaticResource Gray}"
                                                StrokeShape="RoundRectangle 3"
                                                StrokeThickness="3">
                                            <Border HorizontalOptions="Center"
                                                    Stroke="{StaticResource Gray}"
                                                    StrokeShape="RoundRectangle 3"
                                                    StrokeThickness="3">
                                                <Image Source="{Binding Book.ImageSmall}"
                                                       Aspect="AspectFill"
                                                       Grid.Column="0"
                                                       Grid.RowSpan="5"/>
                                            </Border>
                                        </Border>
                                        <Image Source="bookmark_red.png"
                                               HeightRequest="10"
                                               WidthRequest="10"
                                               Margin="0,97,20,0"
                                               VerticalOptions="End"/>
                                    </Grid>
                                    <Label Text="{Binding Book.Title}"
                                           Style="{StaticResource MasterTitleBookText}"
                                           Grid.Column="2"
                                           Grid.Row="0"/>
                                    <Label Text="Author"
                                           Style="{StaticResource MasterAuthorBookText}"
                                           Grid.Column="2"
                                           Grid.Row="1"/>
                                    <Label Text="{Binding Book.Status}"
                                           Style="{StaticResource MasterStateBookText}"                                   
                                           Grid.Column="2"
                                           Grid.Row="2"/>
                                    <contentView:StarNotationView Grid.Column="2"
                                                                  Grid.Row="4"/>
                                </Grid>
                                <contentView:SeparatorCutStartView/>
                            </VerticalStackLayout>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
            </Grid>
        </ScrollView>
    </Grid>
    
</ContentPage>