<?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:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:converters="clr-namespace:BookReader.Converters"
             x:Class="BookReader.Views.ReaderPage"
             x:DataType="vm:ReaderViewModel"
             Shell.NavBarIsVisible="False"
             NavigationPage.HasNavigationBar="False">

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

    <Grid>
        <!-- Рендер книги -->
        <HybridWebView x:Name="ReaderWebView"
                        RawMessageReceived="OnRawMessageReceived"
                        DefaultFile="index.html"
                        HorizontalOptions="Fill"
                        VerticalOptions="Fill" />

        <!-- Всплывающее меню -->
        <Grid IsVisible="{Binding IsMenuVisible}"
              BackgroundColor="Transparent"
              InputTransparent="False">
            <Grid.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding HideMenuCommand}" />
            </Grid.GestureRecognizers>
            <!-- Верхняя всплывающая панель -->
            <Frame  BackgroundColor="#2C2C2C"
         VerticalOptions="Start"
        HorizontalOptions="FillAndExpand"
         Padding="20"
         BorderColor="Transparent">
                <VerticalStackLayout>
                    <Label Text="{Binding ProgressText}"
             TextColor="White"
             FontSize="12"
             HorizontalTextAlignment="Center" />
                    <!--Back Button-->
                    <Button Text="← Back to Library"
             BackgroundColor="#D32F2F"
             TextColor="White"
             FontSize="14"
             CornerRadius="8"
             HeightRequest="45"
             Clicked="OnBackToLibrary" />
                </VerticalStackLayout>
            </Frame>
            <!--Нижняя панель-->
            <Frame VerticalOptions="End"
                   HorizontalOptions="FillAndExpand"
                   BackgroundColor="#2C2C2C"
                   Padding="20"
                   BorderColor="Transparent">
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnMenuPanelTapped" />
                </Frame.GestureRecognizers>

                <VerticalStackLayout Spacing="15">
                    <!--Title-->
                    <Label Text="Reading Settings"
                           FontSize="18"
                           FontAttributes="Bold"
                           TextColor="White"
                           HorizontalOptions="Center" />

                    <!--Font Size-->
                    <VerticalStackLayout Spacing="5">
                        <Label Text="Font Size"
                               FontSize="14"
                               TextColor="#B0B0B0" />
                        <Grid ColumnDefinitions="Auto,*,Auto" ColumnSpacing="10">
                            <Button Grid.Column="0"
                                    Text="A-"
                                    FontSize="14"
                                    WidthRequest="45"
                                    HeightRequest="40"
                                    BackgroundColor="#444"
                                    TextColor="White"
                                    CornerRadius="8"
                                    Clicked="OnDecreaseFontSize" />
                            <Label Grid.Column="1"
                                   Text="{Binding FontSize, StringFormat='{0}px'}"
                                   FontSize="16"
                                   TextColor="White"
                                   HorizontalOptions="Center"
                                   VerticalOptions="Center" />
                            <Button Grid.Column="2"
                                    Text="A+"
                                    FontSize="14"
                                    WidthRequest="45"
                                    HeightRequest="40"
                                    BackgroundColor="#444"
                                    TextColor="White"
                                    CornerRadius="8"
                                    Clicked="OnIncreaseFontSize" />
                        </Grid>
                    </VerticalStackLayout>

                    <!--Font Family-->
                    <VerticalStackLayout Spacing="5">
                        <Label Text="Font Family"
                               FontSize="14"
                               TextColor="#B0B0B0" />
                        <Picker x:Name="FontFamilyPicker"
                                ItemsSource="{Binding AvailableFonts}"
                                SelectedItem="{Binding FontFamily}"
                                TextColor="White"
                                BackgroundColor="#444"
                                FontSize="14"
                                SelectedIndexChanged="OnFontFamilyChanged" />
                    </VerticalStackLayout>

                    <!--Chapters Button-->
                    <Button Text="📑 Chapters"
                            BackgroundColor="#5D4037"
                            TextColor="White"
                            FontSize="14"
                            CornerRadius="8"
                            HeightRequest="45"
                            Command="{Binding ToggleChapterListCommand}" />

                    <!--Chapter List-->
                    <CollectionView ItemsSource="{Binding Chapters}"
                                    IsVisible="{Binding IsChapterListVisible}"
                                    MaximumHeightRequest="200"
                                    SelectionMode="Single"
                                    SelectionChanged="OnChapterSelected">
                        <CollectionView.ItemTemplate>
                            <DataTemplate x:DataType="x:String">
                                <Grid Padding="10,8" BackgroundColor="Transparent">
                                    <Label Text="{Binding .}"
                                           TextColor="#E0E0E0"
                                           FontSize="13"
                                           LineBreakMode="TailTruncation" />
                                </Grid>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
                    </CollectionView>
                </VerticalStackLayout>
            </Frame>
        </Grid>

        <!-- Нижняя постоянная панель отображает прогресс чтения  -->
        <VerticalStackLayout VerticalOptions="End" 
             HorizontalOptions="Center" 
             Padding="10"
             InputTransparent="True">

            <Frame BackgroundColor="#AA000000" 
   CornerRadius="15" 
   Padding="10,2" 
   BorderColor="Transparent"
   HasShadow="False">
                <Label Text="{Binding ProgressText}"
       TextColor="White"
       FontSize="12"
       HorizontalTextAlignment="Center" />
            </Frame>
        </VerticalStackLayout>
    </Grid>
</ContentPage>