<?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:BookReader.ViewModels"
             xmlns:models="clr-namespace:BookReader.Models"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="BookReader.Views.BookshelfPage"
             x:DataType="vm:BookshelfViewModel"
             Title="{Binding Title}"
             Shell.NavBarIsVisible="True">

    <ContentPage.Resources>
        <toolkit:InvertedBoolConverter x:Key="InvertedBoolConverter" />
    </ContentPage.Resources>

    <Shell.TitleView>
        <Grid ColumnDefinitions="*,Auto" Padding="0,0,5,0">
            <Label Grid.Column="0"
                   Text="📚 My Library"
                   FontSize="20"
                   FontAttributes="Bold"
                   VerticalOptions="Center"
                   TextColor="White" />
            <ImageButton Grid.Column="1"
                         Source="dots_vertical.png"
                         WidthRequest="30"
                         HeightRequest="30"
                         VerticalOptions="Center"
                         Clicked="OnMenuClicked" />
        </Grid>
    </Shell.TitleView>

    <Grid RowDefinitions="Auto,*,Auto" BackgroundColor="#3E2723">

        <!-- Search Bar -->
        <SearchBar Grid.Row="0"
                   Text="{Binding SearchText, Mode=TwoWay}"
                   SearchCommand="{Binding SearchCommand}"
                   SearchCommandParameter="{Binding Text, Source={RelativeSource Self}}"
                   Placeholder="Search by title or author..."
                   PlaceholderColor="#A1887F"
                   TextColor="White"
                   BackgroundColor="#4E342E"
                   Margin="10,5" />

        <!-- Bookshelf Background -->
        <Grid Grid.Row="1">
            <!-- Empty state -->
            <VerticalStackLayout IsVisible="{Binding IsEmpty}"
                                 VerticalOptions="Center"
                                 HorizontalOptions="Center"
                                 Spacing="20"
                                 Padding="40">
                <Label Text="📖"
                       FontSize="80"
                       HorizontalOptions="Center" />
                <Label Text="Your bookshelf is empty"
                       FontSize="20"
                       TextColor="#D7CCC8"
                       HorizontalOptions="Center" />
                <Label Text="Add books from your device or Calibre library"
                       FontSize="14"
                       TextColor="#A1887F"
                       HorizontalOptions="Center"
                       HorizontalTextAlignment="Center" />
            </VerticalStackLayout>

            <!-- Book Collection -->
            <CollectionView ItemsSource="{Binding Books}"
                            SelectionMode="None"
                            Margin="10">

                <CollectionView.ItemsLayout>
                    <GridItemsLayout Orientation="Vertical"
                                     Span="3"
                                     HorizontalItemSpacing="10"
                                     VerticalItemSpacing="15" />
                </CollectionView.ItemsLayout>

                    <CollectionView.ItemTemplate>
                        <DataTemplate x:DataType="models:Book">
                            <SwipeView>
                                <SwipeView.RightItems>
                                    <SwipeItems>
                                        <SwipeItem Text="Delete"
                                                   BackgroundColor="#D32F2F"
                                                   Command="{Binding Source={RelativeSource AncestorType={x:Type vm:BookshelfViewModel}}, Path=DeleteBookCommand}"
                                                   CommandParameter="{Binding .}" />
                                    </SwipeItems>
                                </SwipeView.RightItems>

                                <Frame Padding="0"
                                       CornerRadius="8"
                                       BackgroundColor="#5D4037"
                                       HasShadow="True"
                                       BorderColor="Transparent">
                                    <Frame.GestureRecognizers>
                                        <TapGestureRecognizer
                                            Command="{Binding Source={RelativeSource AncestorType={x:Type vm:BookshelfViewModel}}, Path=OpenBookCommand}"
                                            CommandParameter="{Binding .}" />
                                    </Frame.GestureRecognizers>

                                    <Grid RowDefinitions="150,Auto,Auto,Auto" Padding="0">
                                        <!-- Cover Image -->
                                        <Frame Grid.Row="0"
                                               Padding="0"
                                               CornerRadius="8,8,0,0"
                                               IsClippedToBounds="True"
                                               HasShadow="False"
                                               BorderColor="Transparent">
                                            <Image Source="{Binding CoverImage, Converter={StaticResource ByteArrayToImageConverter}}"
                                                   Aspect="AspectFill"
                                                   HeightRequest="150" />
                                        </Frame>

                                        <!-- Progress Bar -->
                                        <Grid Grid.Row="0"
                                              VerticalOptions="End"
                                              HeightRequest="4"
                                              BackgroundColor="#44000000">
                                            <BoxView BackgroundColor="#4CAF50"
                                                     HorizontalOptions="Start"
                                                     WidthRequest="{Binding ReadingProgress, Converter={StaticResource ProgressToWidthConverter}}" />
                                        </Grid>

                                        <!-- Title -->
                                        <Label Grid.Row="1"
                                               Text="{Binding Title}"
                                               FontSize="11"
                                               FontAttributes="Bold"
                                               TextColor="#EFEBE9"
                                               LineBreakMode="TailTruncation"
                                               MaxLines="2"
                                               Padding="5,5,5,0" />

                                        <!-- Author -->
                                        <Label Grid.Row="2"
                                               Text="{Binding Author}"
                                               FontSize="9"
                                               TextColor="#A1887F"
                                               LineBreakMode="TailTruncation"
                                               MaxLines="1"
                                               Padding="5,0,5,2" />

                                        <!-- Progress Text -->
                                        <Label Grid.Row="3"
                                               Text="{Binding ProgressText}"
                                               FontSize="9"
                                               TextColor="#81C784"
                                               Padding="5,0,5,5" />
                                    </Grid>
                                </Frame>
                            </SwipeView>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
        </Grid>

        <!-- Bottom Shelf / Action Bar -->
        <Grid Grid.Row="2"
              BackgroundColor="#4E342E"
              Padding="15,10"
              ColumnDefinitions="*,Auto,Auto">

            <!-- Shelf decoration -->
            <BoxView Grid.ColumnSpan="3"
                     BackgroundColor="#6D4C41"
                     HeightRequest="3"
                     VerticalOptions="Start"
                     Margin="0,-10,0,0" />

            <Label Grid.Column="0"
                   Text="{Binding Books.Count, StringFormat='{0} books'}"
                   TextColor="#A1887F"
                   FontSize="12"
                   VerticalOptions="Center" />

            <Button Grid.Column="1"
                    Text="📁 Add File"
                    BackgroundColor="#6D4C41"
                    TextColor="White"
                    FontSize="12"
                    CornerRadius="20"
                    Padding="15,8"
                    Margin="5,0"
                    Command="{Binding AddBookFromFileCommand}" />

            <Button Grid.Column="2"
                    Text="☁️ Calibre"
                    BackgroundColor="#6D4C41"
                    TextColor="White"
                    FontSize="12"
                    CornerRadius="20"
                    Padding="15,8"
                    Command="{Binding OpenCalibreLibraryCommand}" />
        </Grid>
    </Grid>
</ContentPage>
