<?xml version="1.0" encoding="UTF-8" ?>
<Window
    x:Class="LlamaApp.SettingsWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <!--
        A centered settings window (separate from the tray flyout) styled after
        the Windows 11 Settings app: a custom Mica titlebar, a left
        NavigationView (General / Identity / Llama) over a Mica backdrop, and a
        footer action bar with Save / Cancel. Each page is a stack of rounded
        "SettingsCards" (icon tile + title/description + control). Saves to
        <see cref="Settings"/> on Save; discards on Cancel. See
        SettingsWindow.xaml.cs for sizing (fixed DIP size, scaled by the
        monitor's DPI, centered on the cursor's monitor).

        Cards use a stacked layout (title row, then wrapping description, then a
        full-width control) so descriptive text never gets cropped and the
        inputs get the full card width rather than a cramped right column.
    -->
    <Window.SystemBackdrop>
        <MicaBackdrop Kind="Base" />
    </Window.SystemBackdrop>

    <Grid RowSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" /> <!-- Custom titlebar (drag region) -->
            <RowDefinition Height="*" />    <!-- Body -->
        </Grid.RowDefinitions>

        <!-- Custom titlebar: Mica extends behind it, with an app label on
             the left and the system caption buttons (close) on the right.
             Marked as the drag region via SetTitleBar() in the code-behind. -->
        <Grid x:Name="AppTitleBar"
              Grid.Row="0"
              Height="40"
              Padding="32,0,0,0">
            <StackPanel Orientation="Horizontal"
                        Spacing="8"
                        VerticalAlignment="Center">
                <Image Source="{ThemeResource BrandLogoSource}"
                       Width="16" Height="16"
                       Stretch="Uniform"
                       VerticalAlignment="Center" />
                <TextBlock Text="Settings"
                           FontSize="12"
                           FontWeight="SemiBold"
                           Opacity="0.85"
                           VerticalAlignment="Center" />
            </StackPanel>
        </Grid>

        <!-- Body: left nav + page content, then the footer action bar -->
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />    <!-- NavigationView -->
                <RowDefinition Height="Auto" /> <!-- Divider -->
                <RowDefinition Height="Auto" /> <!-- Footer actions -->
            </Grid.RowDefinitions>

            <!-- Left menubar, Windows 11 Settings style: always-expanded pane
                 (no hamburger, no back button, no built-in Settings item).
                 Transparent chrome so the Mica backdrop flows through both the
                 pane and the content area. -->
            <NavigationView x:Name="NavView"
                            Grid.Row="0"
                            PaneDisplayMode="Left"
                            OpenPaneLength="190"
                            IsPaneToggleButtonVisible="False"
                            IsBackButtonVisible="Collapsed"
                            IsSettingsVisible="False"
                            AlwaysShowHeader="False"
                            Background="Transparent"
                            SelectionChanged="NavView_SelectionChanged">
                <NavigationView.Resources>
                    <SolidColorBrush x:Key="NavigationViewExpandedPaneBackground"
                                     Color="Transparent" />
                    <SolidColorBrush x:Key="NavigationViewDefaultPaneBackground"
                                     Color="Transparent" />
                    <!-- Selection pill in the brand orange instead of the
                         system accent, matching the flyout's wordmark. -->
                    <SolidColorBrush x:Key="NavigationViewSelectionIndicatorForeground"
                                     Color="#FF8A3D" />
                </NavigationView.Resources>
                <NavigationView.MenuItems>
                    <NavigationViewItem Content="General"
                                        Tag="general"
                                        IsSelected="True"
                                        ToolTipService.ToolTip="Startup and storage">
                        <NavigationViewItem.Icon>
                            <FontIcon Glyph="&#xE713;" />
                        </NavigationViewItem.Icon>
                    </NavigationViewItem>
                    <NavigationViewItem Content="Identity"
                                        Tag="identity"
                                        ToolTipService.ToolTip="Hugging Face account">
                        <NavigationViewItem.Icon>
                            <FontIcon Glyph="&#xE77B;" />
                        </NavigationViewItem.Icon>
                    </NavigationViewItem>
                    <NavigationViewItem Content="Llama"
                                        Tag="llama"
                                        ToolTipService.ToolTip="Local llama server">
                        <NavigationViewItem.Icon>
                            <ImageIcon Source="{ThemeResource BrandLogoSource}" />
                        </NavigationViewItem.Icon>
                    </NavigationViewItem>
                </NavigationView.MenuItems>

                <!-- Pages: exactly one is visible at a time, switched from
                     NavView_SelectionChanged. Each page has a Fluent page
                     header (subtitle style) above its cards. -->
                <Grid Padding="8,0,16,8">

                    <!-- ============ General page ============ -->
                    <ScrollViewer x:Name="GeneralPage"
                                  VerticalScrollBarVisibility="Auto"
                                  HorizontalScrollBarVisibility="Hidden"
                                  HorizontalScrollMode="Disabled">
                        <StackPanel Spacing="10" Padding="8,4,8,8">
                            <TextBlock Text="General"
                                       Style="{ThemeResource SubtitleTextBlockStyle}"
                                       Margin="4,4,0,10" />

                            <!-- Launch at startup (toggle) -->
                            <Border CornerRadius="8"
                                    Padding="16,14"
                                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                    BorderThickness="1">
                                <Grid ColumnSpacing="12">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Border Grid.Column="0"
                                            Width="36" Height="36"
                                            CornerRadius="{ThemeResource ControlCornerRadius}"
                                            Background="{StaticResource BrandAccentBrush}"
                                            VerticalAlignment="Top">
                                        <FontIcon Glyph="&#xE7B5;"
                                                  FontSize="16"
                                                  Foreground="White"
                                                  HorizontalAlignment="Center"
                                                  VerticalAlignment="Center" />
                                    </Border>
                                    <StackPanel Grid.Column="1" Spacing="2">
                                        <TextBlock Text="Launch at startup"
                                                   FontSize="14"
                                                   FontWeight="SemiBold" />
                                        <TextBlock Text="Start LlamaApp automatically when you sign in to Windows."
                                                   FontSize="12"
                                                   Opacity="0.6"
                                                   TextWrapping="WrapWholeWords" />
                                    </StackPanel>
                                    <CheckBox x:Name="LaunchAtStartupBox"
                                              Grid.Column="2"
                                              VerticalAlignment="Top"
                                              MinWidth="0"
                                              ToolTipService.ToolTip="Run LlamaApp when Windows starts" />
                                </Grid>
                            </Border>

                            <!-- Chat overlay shortcut (informational — the
                                 hotkey is hard-coded Alt+Space; this card is
                                 how a user discovers it exists). -->
                            <Border CornerRadius="8"
                                    Padding="16,14"
                                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                    BorderThickness="1">
                                <Grid ColumnSpacing="12">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Border Grid.Column="0"
                                            Width="36" Height="36"
                                            CornerRadius="{ThemeResource ControlCornerRadius}"
                                            Background="{StaticResource BrandAccentBrush}"
                                            VerticalAlignment="Top">
                                        <FontIcon Glyph="&#xE765;"
                                                  FontSize="16"
                                                  Foreground="White"
                                                  HorizontalAlignment="Center"
                                                  VerticalAlignment="Center" />
                                    </Border>
                                    <StackPanel Grid.Column="1" Spacing="2">
                                        <TextBlock Text="Chat overlay"
                                                   FontSize="14"
                                                   FontWeight="SemiBold" />
                                        <TextBlock FontSize="12"
                                                   Opacity="0.6"
                                                   TextWrapping="WrapWholeWords">
                                            <Run Text="Press" />
                                            <Run Text="Alt+Space" FontWeight="SemiBold" />
                                            <Run Text="anywhere to open the chat overlay for the loaded model. Press Esc to dismiss it." />
                                        </TextBlock>
                                    </StackPanel>
                                </Grid>
                            </Border>

                            <!-- Local models cache directory -->
                            <Border CornerRadius="8"
                                    Padding="16,14"
                                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                    BorderThickness="1">
                                <StackPanel Spacing="10">
                                    <Grid ColumnSpacing="12">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0"
                                                Width="36" Height="36"
                                                CornerRadius="{ThemeResource ControlCornerRadius}"
                                                Background="{StaticResource BrandAccentBrush}"
                                                VerticalAlignment="Center">
                                            <FontIcon Glyph="&#xE8B7;"
                                                      FontSize="16"
                                                      Foreground="White"
                                                      HorizontalAlignment="Center"
                                                      VerticalAlignment="Center" />
                                        </Border>
                                        <TextBlock Grid.Column="1"
                                                   Text="Local Models Cache"
                                                   FontSize="14"
                                                   FontWeight="SemiBold"
                                                   VerticalAlignment="Center" />
                                    </Grid>
                                    <TextBlock Text="Where downloaded model files are stored on disk. Pick a drive with enough free space for the models you plan to keep."
                                               FontSize="12"
                                               Opacity="0.6"
                                               TextWrapping="WrapWholeWords" />
                                    <Grid ColumnSpacing="8">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <TextBox x:Name="CacheBox"
                                                 Grid.Column="0"
                                                 PlaceholderText="Path to model folder"
                                                 IsReadOnly="False"
                                                 ToolTipService.ToolTip="Path to the local models cache" />
                                        <Button x:Name="BrowseButton"
                                                Grid.Column="1"
                                                Content="Browse…"
                                                Click="Browse_Click"
                                                ToolTipService.ToolTip="Pick a folder" />
                                    </Grid>
                                </StackPanel>
                            </Border>

                            <!-- Logs (informational — where the app writes its
                                 daily log files; the same "Open Folder" button
                                 pattern as the Installation Folder card). -->
                            <Border CornerRadius="8"
                                    Padding="16,14"
                                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                    BorderThickness="1">
                                <StackPanel Spacing="10">
                                    <Grid ColumnSpacing="12">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0"
                                                Width="36" Height="36"
                                                CornerRadius="{ThemeResource ControlCornerRadius}"
                                                Background="{StaticResource BrandAccentBrush}"
                                                VerticalAlignment="Center">
                                            <FontIcon Glyph="&#xE8A5;"
                                                      FontSize="16"
                                                      Foreground="White"
                                                      HorizontalAlignment="Center"
                                                      VerticalAlignment="Center" />
                                        </Border>
                                        <TextBlock Grid.Column="1"
                                                   Text="Logs"
                                                   FontSize="14"
                                                   FontWeight="SemiBold"
                                                   VerticalAlignment="Center" />
                                    </Grid>
                                    <TextBlock Text="Where LlamaApp writes its daily log files — include them when reporting a problem."
                                               FontSize="12"
                                               Opacity="0.6"
                                               TextWrapping="WrapWholeWords" />
                                    <StackPanel Orientation="Horizontal"
                                                Spacing="8">
                                        <Button x:Name="OpenLogFolderButton"
                                                Content="Open Folder"
                                                Click="OpenLogFolder_Click"
                                                ToolTipService.ToolTip="Show the log folder in File Explorer" />
                                    </StackPanel>
                                </StackPanel>
                            </Border>
                        </StackPanel>
                    </ScrollViewer>

                    <!-- ============ Identity page ============ -->
                    <ScrollViewer x:Name="IdentityPage"
                                  Visibility="Collapsed"
                                  VerticalScrollBarVisibility="Auto"
                                  HorizontalScrollBarVisibility="Hidden"
                                  HorizontalScrollMode="Disabled">
                        <StackPanel Spacing="10" Padding="8,4,8,8">
                            <TextBlock Text="Identity"
                                       Style="{ThemeResource SubtitleTextBlockStyle}"
                                       Margin="4,4,0,10" />

                            <!-- Hugging Face token (password style — masked) -->
                            <Border CornerRadius="8"
                                    Padding="16,14"
                                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                    BorderThickness="1">
                                <StackPanel Spacing="10">
                                    <Grid ColumnSpacing="12">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <!-- Accent icon tile -->
                                        <Border Grid.Column="0"
                                                Width="36" Height="36"
                                                CornerRadius="{ThemeResource ControlCornerRadius}"
                                                Background="{StaticResource BrandAccentBrush}"
                                                VerticalAlignment="Center">
                                            <FontIcon Glyph="&#xE72E;"
                                                      FontSize="16"
                                                      Foreground="White"
                                                      HorizontalAlignment="Center"
                                                      VerticalAlignment="Center" />
                                        </Border>
                                        <TextBlock Grid.Column="1"
                                                   Text="Hugging Face Token"
                                                   FontSize="14"
                                                   FontWeight="SemiBold"
                                                   VerticalAlignment="Center" />
                                    </Grid>
                                    <!-- Description (wraps freely across the full card width) -->
                                    <RichTextBlock FontSize="12"
                                                   Opacity="0.6"
                                                   TextWrapping="WrapWholeWords"
                                                   IsTextSelectionEnabled="False">
                                        <Paragraph>
                                            Used to pull private or gated models. Optional — leave
                                            blank for public repos. Create one at
                                            <Hyperlink NavigateUri="https://huggingface.co/settings/tokens">
                                                huggingface.co/settings/tokens
                                            </Hyperlink>.
                                        </Paragraph>
                                    </RichTextBlock>
                                    <!-- Control: full card width -->
                                    <PasswordBox x:Name="TokenBox"
                                                 PlaceholderText="hf_…"
                                                 PasswordRevealMode="Peek"
                                                 ToolTipService.ToolTip="Paste your Hugging Face access token" />
                                </StackPanel>
                            </Border>
                        </StackPanel>
                    </ScrollViewer>

                    <!-- ============ Llama page ============ -->
                    <ScrollViewer x:Name="LlamaPage"
                                  Visibility="Collapsed"
                                  VerticalScrollBarVisibility="Auto"
                                  HorizontalScrollBarVisibility="Hidden"
                                  HorizontalScrollMode="Disabled">
                        <StackPanel Spacing="10" Padding="8,4,8,8">
                            <TextBlock Text="Llama"
                                       Style="{ThemeResource SubtitleTextBlockStyle}"
                                       Margin="4,4,0,10" />

                            <!-- Llama server port -->
                            <Border CornerRadius="8"
                                    Padding="16,14"
                                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                    BorderThickness="1">
                                <StackPanel Spacing="10">
                                    <Grid ColumnSpacing="12">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0"
                                                Width="36" Height="36"
                                                CornerRadius="{ThemeResource ControlCornerRadius}"
                                                Background="{StaticResource BrandAccentBrush}"
                                                VerticalAlignment="Center">
                                            <FontIcon Glyph="&#xE968;"
                                                      FontSize="16"
                                                      Foreground="White"
                                                      HorizontalAlignment="Center"
                                                      VerticalAlignment="Center" />
                                        </Border>
                                        <TextBlock Grid.Column="1"
                                                   Text="Llama Server Port"
                                                   FontSize="14"
                                                   FontWeight="SemiBold"
                                                   VerticalAlignment="Center" />
                                    </Grid>
                                    <TextBlock Text="Port the local llama server listens on (1–65535). Takes effect the next time LlamaApp starts."
                                               FontSize="12"
                                               Opacity="0.6"
                                               TextWrapping="WrapWholeWords" />
                                    <NumberBox x:Name="PortBox"
                                               Minimum="1"
                                               Maximum="65535"
                                               SmallChange="1"
                                               LargeChange="10"
                                               SpinButtonPlacementMode="Hidden"
                                               ToolTipService.ToolTip="Port for the local llama server (1–65535)" />
                                </StackPanel>
                            </Border>

                            <!-- Installation folder (read-only — informational,
                                 not a setting) -->
                            <Border CornerRadius="8"
                                    Padding="16,14"
                                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                    BorderThickness="1">
                                <StackPanel Spacing="10">
                                    <Grid ColumnSpacing="12">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0"
                                                Width="36" Height="36"
                                                CornerRadius="{ThemeResource ControlCornerRadius}"
                                                Background="{StaticResource BrandAccentBrush}"
                                                VerticalAlignment="Center">
                                            <FontIcon Glyph="&#xE8B7;"
                                                      FontSize="16"
                                                      Foreground="White"
                                                      HorizontalAlignment="Center"
                                                      VerticalAlignment="Center" />
                                        </Border>
                                        <TextBlock Grid.Column="1"
                                                   Text="Installation Folder"
                                                   FontSize="14"
                                                   FontWeight="SemiBold"
                                                   VerticalAlignment="Center" />
                                    </Grid>
                                    <TextBlock x:Name="InstallDescriptionText"
                                               FontSize="12"
                                               Opacity="0.6"
                                               TextWrapping="WrapWholeWords" />
                                    <!-- Read-only: the location isn't configurable,
                                         but the box stays selectable/copyable. -->
                                    <TextBox x:Name="InstallPathBox"
                                             IsReadOnly="True"
                                             TextWrapping="NoWrap"
                                             ToolTipService.ToolTip="Llama installation folder (read-only)" />
                                    <StackPanel Orientation="Horizontal"
                                                Spacing="8">
                                        <Button x:Name="OpenInstallFolderButton"
                                                Content="Open Folder"
                                                Click="OpenInstallFolder_Click"
                                                ToolTipService.ToolTip="Show the folder in File Explorer" />
                                        <Button x:Name="EmptyInstallFolderButton"
                                                Content="Empty Folder…"
                                                Click="EmptyInstallFolder_Click"
                                                ToolTipService.ToolTip="Delete the folder's contents — the llama binary is downloaded again on next launch" />
                                    </StackPanel>
                                </StackPanel>
                            </Border>
                        </StackPanel>
                    </ScrollViewer>
                </Grid>
            </NavigationView>

            <!-- ============ Footer divider + actions ============ -->
            <Rectangle Grid.Row="1"
                       Height="1"
                       Fill="{ThemeResource DividerStrokeColorDefaultBrush}" />

            <StackPanel Grid.Row="2"
                        Orientation="Horizontal"
                        Spacing="8"
                        Padding="24,12,24,16"
                        HorizontalAlignment="Right">
                <Button Content="Cancel"
                        Padding="24,10"
                        Click="Cancel_Click"
                        ToolTipService.ToolTip="Discard changes" />
                <Button Content="Save"
                        Padding="24,10"
                        Style="{ThemeResource AccentButtonStyle}"
                        Click="Save_Click"
                        ToolTipService.ToolTip="Save changes" />
            </StackPanel>
        </Grid>
    </Grid>
</Window>
