<?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"
             x:Class="BookReader.Views.SettingsPage"
             x:DataType="vm:SettingsViewModel"
             Title="Настройки"
             Background="{StaticResource AppBackgroundBrush}">

    <Grid RowDefinitions="Auto,*">
        <VerticalStackLayout Grid.Row="0"
                             Padding="20,24,20,14"
                             Spacing="6">
            <Label Text="Настройки"
                   Style="{StaticResource PageTitleStyle}" />
            <Label Text="Подключите Calibre и задайте параметры чтения, которые будут применяться ко всем новым книгам."
                   Style="{StaticResource PageSubtitleStyle}" />
        </VerticalStackLayout>

        <ScrollView Grid.Row="1"
                    Margin="0,0,0,24">
            <VerticalStackLayout Padding="20,0,20,0"
                                 Spacing="16">
                <Border Style="{StaticResource CardBorderStyle}">
                    <VerticalStackLayout Spacing="14">
                        <Label Text="Подключение к Calibre"
                               Style="{StaticResource SectionTitleStyle}" />

                        <VerticalStackLayout Spacing="6">
                            <Label Text="Адрес сервера"
                                   Style="{StaticResource CaptionStyle}" />
                            <Entry Text="{Binding CalibreUrl}"
                                   Placeholder="https://your-calibre-server.example"
                                   Style="{StaticResource AppEntryStyle}"
                                   Keyboard="Url" />
                        </VerticalStackLayout>

                        <VerticalStackLayout Spacing="6">
                            <Label Text="Логин"
                                   Style="{StaticResource CaptionStyle}" />
                            <Entry Text="{Binding CalibreUsername}"
                                   Placeholder="Имя пользователя"
                                   Style="{StaticResource AppEntryStyle}" />
                        </VerticalStackLayout>

                        <VerticalStackLayout Spacing="6">
                            <Label Text="Пароль"
                                   Style="{StaticResource CaptionStyle}" />
                            <Entry Text="{Binding CalibrePassword}"
                                   Placeholder="Пароль"
                                   Style="{StaticResource AppEntryStyle}"
                                   IsPassword="True" />
                        </VerticalStackLayout>

                        <Border Style="{StaticResource MutedCardBorderStyle}">
                            <Label Text="{Binding ConnectionSecurityHint}"
                                   TextColor="{Binding ConnectionSecurityColor}"
                                   FontSize="13"
                                   LineBreakMode="WordWrap" />
                        </Border>

                        <Grid ColumnDefinitions="*,*" ColumnSpacing="12">
                            <Button Grid.Column="0"
                                    Text="Проверить"
                                    Style="{StaticResource SecondaryButtonStyle}"
                                    Command="{Binding TestConnectionCommand}"
                                    IsEnabled="{Binding IsConnectionTesting, Converter={StaticResource InvertedBoolConverter}}" />
                            <Button Grid.Column="1"
                                    Text="Сохранить"
                                    Style="{StaticResource PrimaryButtonStyle}"
                                    Command="{Binding SaveSettingsCommand}" />
                        </Grid>

                        <Grid ColumnDefinitions="*,Auto"
                              IsVisible="{Binding ConnectionStatus, Converter={StaticResource IsNotNullOrEmptyConverter}}">
                            <Label Text="{Binding ConnectionStatus}"
                                   TextColor="{Binding ConnectionStatusColor}"
                                   FontSize="13"
                                   VerticalOptions="Center" />
                            <ActivityIndicator Grid.Column="1"
                                               IsRunning="{Binding IsConnectionTesting}"
                                               IsVisible="{Binding IsConnectionTesting}"
                                               Color="{StaticResource AccentColor}" />
                        </Grid>
                    </VerticalStackLayout>
                </Border>

                <Border Style="{StaticResource CardBorderStyle}">
                    <VerticalStackLayout Spacing="14">
                        <Label Text="Чтение по умолчанию"
                               Style="{StaticResource SectionTitleStyle}" />

                        <VerticalStackLayout Spacing="6">
                            <Label Text="Размер шрифта"
                                   Style="{StaticResource CaptionStyle}" />
                            <Picker ItemsSource="{Binding AvailableFontSizes}"
                                    SelectedItem="{Binding DefaultFontSize}"
                                    Style="{StaticResource AppPickerStyle}" />
                        </VerticalStackLayout>

                        <VerticalStackLayout Spacing="6">
                            <Label Text="Гарнитура"
                                   Style="{StaticResource CaptionStyle}" />
                            <Picker ItemsSource="{Binding AvailableFonts}"
                                    SelectedItem="{Binding DefaultFontFamily}"
                                    Style="{StaticResource AppPickerStyle}" />
                        </VerticalStackLayout>

                        <VerticalStackLayout Spacing="6">
                            <Label Text="Тема ридера"
                                   Style="{StaticResource CaptionStyle}" />
                            <Picker ItemsSource="{Binding AvailableReaderThemes}"
                                    SelectedItem="{Binding DefaultReaderTheme}"
                                    Style="{StaticResource AppPickerStyle}" />
                        </VerticalStackLayout>

                        <VerticalStackLayout Spacing="8">
                            <Label Text="{Binding DefaultBrightness, StringFormat='Яркость страницы: {0:F0}%'}"
                                   Style="{StaticResource CaptionStyle}" />
                            <Slider Minimum="70"
                                    Maximum="120"
                                    Value="{Binding DefaultBrightness}"
                                    Style="{StaticResource ReaderSliderStyle}" />
                        </VerticalStackLayout>
                    </VerticalStackLayout>
                </Border>
            </VerticalStackLayout>
        </ScrollView>
    </Grid>
</ContentPage>
