<UserControl x:Class="UniGetUI.Avalonia.Views.SidebarView"
             xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:automation="clr-namespace:Avalonia.Automation;assembly=Avalonia.Controls"
             xmlns:vm="using:UniGetUI.Avalonia.ViewModels"
             xmlns:controls="using:UniGetUI.Avalonia.Views.Controls"
             xmlns:local="using:UniGetUI.Avalonia.Views"
             xmlns:t="using:UniGetUI.Avalonia.MarkupExtensions"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Name="SidebarRoot"
             x:DataType="vm:SidebarViewModel"
             MinWidth="64">

    <Grid x:Name="SidebarLayout" RowDefinitions="*,Auto">

        <!-- Main nav items -->
        <ListBox x:Name="NavListBox"
                 Grid.Row="0"
                 SelectionMode="Single"
                 SelectionChanged="NavListBox_SelectionChanged"
                 Background="Transparent"
                 BorderThickness="0"
                 automation:AutomationProperties.AccessibilityView="Control"
                 automation:AutomationProperties.Name="{t:Translate Navigation}"
                 Padding="8,10,8,0">

            <!-- Discover Packages -->
            <ListBoxItem x:Name="DiscoverNavBtn" Tag="Discover" Classes="nav-item" Padding="4,2" CornerRadius="6"
                         automation:AutomationProperties.Name="{t:Translate Discover Packages}"
                         automation:AutomationProperties.AcceleratorKey="Ctrl+1"
                         ToolTip.Tip="{t:Translate Discover Packages}">
                <Panel>
                    <StackPanel Orientation="Horizontal" Spacing="12" Margin="8,8">
                        <controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/DiscoverPackage.svg" Width="24" Height="24" VerticalAlignment="Center" />
                        <TextBlock Text="{t:Translate Discover Packages}"
                                   FontSize="14"
                                   FontWeight="Normal"
                                   VerticalAlignment="Center"
                                   IsVisible="{Binding $parent[local:SidebarView].ShowLabels}"/>
                    </StackPanel>
                    <ProgressBar IsIndeterminate="{Binding DiscoverIsLoading}"
                                 IsVisible="{Binding DiscoverIsLoading}"
                                 Height="2"
                                 Width="32"
                                 MinWidth="0"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Bottom"
                                 Margin="4,0,0,2"/>
                </Panel>
            </ListBoxItem>

            <!-- Software Updates -->
            <ListBoxItem x:Name="UpdatesNavBtn" Tag="Updates" Classes="nav-item" Padding="4,2" CornerRadius="6"
                         automation:AutomationProperties.Name="{t:Translate Software Updates}"
                         automation:AutomationProperties.AcceleratorKey="Ctrl+2"
                         ToolTip.Tip="{t:Translate Software Updates}">
                <Panel>
                    <StackPanel Orientation="Horizontal" Spacing="12" Margin="8,8">
                        <controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/update.svg" Width="24" Height="24" VerticalAlignment="Center"/>
                        <TextBlock Text="{t:Translate Software Updates}"
                                   FontSize="14"
                                   FontWeight="Normal"
                                   VerticalAlignment="Center"
                                   IsVisible="{Binding $parent[local:SidebarView].ShowLabels}"/>
                    </StackPanel>
                    <!-- Update count bubble (InfoBadge-style), anchored to the icon's top-right in both rail and flyout -->
                    <Border Background="{DynamicResource SystemAccentColor}"
                            CornerRadius="8"
                            Padding="3,1"
                            MinWidth="16"
                            Height="16"
                            IsVisible="{Binding UpdatesBadgeVisible}"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Top"
                            Margin="26,5,0,0">
                        <TextBlock Text="{Binding UpdatesBadgeCount}"
                                   FontSize="9"
                                   FontWeight="Bold"
                                   Foreground="White"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"/>
                    </Border>
                    <ProgressBar IsIndeterminate="{Binding UpdatesIsLoading}"
                                 IsVisible="{Binding UpdatesIsLoading}"
                                 Height="2"
                                 Width="32"
                                 MinWidth="0"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Bottom"
                                 Margin="4,0,0,2"/>
                </Panel>
            </ListBoxItem>

            <!-- Installed Packages -->
            <ListBoxItem x:Name="InstalledNavBtn" Tag="Installed" Classes="nav-item" Padding="4,2" CornerRadius="6"
                         automation:AutomationProperties.Name="{t:Translate Installed Packages}"
                         automation:AutomationProperties.AcceleratorKey="Ctrl+3"
                         ToolTip.Tip="{t:Translate Installed Packages}">
                <Panel>
                    <StackPanel Orientation="Horizontal" Spacing="12" Margin="8,8">
                        <controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/InstalledPackages.svg" Width="24" Height="24" VerticalAlignment="Center"/>
                        <TextBlock Text="{t:Translate Installed Packages}"
                                   FontSize="14"
                                   FontWeight="Normal"
                                   VerticalAlignment="Center"
                                   IsVisible="{Binding $parent[local:SidebarView].ShowLabels}"/>
                    </StackPanel>
                    <ProgressBar IsIndeterminate="{Binding InstalledIsLoading}"
                                 IsVisible="{Binding InstalledIsLoading}"
                                 Height="2"
                                 Width="32"
                                 MinWidth="0"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Bottom"
                                 Margin="4,0,0,2"/>
                </Panel>
            </ListBoxItem>

            <!-- Package Bundles -->
            <ListBoxItem x:Name="BundlesNavBtn" Tag="Bundles" Classes="nav-item" Padding="4,2" CornerRadius="6"
                         automation:AutomationProperties.Name="{t:Translate Package Bundles}"
                         automation:AutomationProperties.AcceleratorKey="Ctrl+4"
                         ToolTip.Tip="{t:Translate Package Bundles}">
                <Panel>
                    <StackPanel Orientation="Horizontal" Spacing="12" Margin="8,8">
                        <controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/PackagesBundle.svg" Width="24" Height="24" VerticalAlignment="Center"/>
                        <TextBlock Text="{t:Translate Package Bundles}"
                                   FontSize="14"
                                   FontWeight="Normal"
                                   VerticalAlignment="Center"
                                   IsVisible="{Binding $parent[local:SidebarView].ShowLabels}"/>
                    </StackPanel>
                    <!-- Unsaved changes dot, anchored to the icon's top-right in both rail and flyout -->
                    <Border Background="{DynamicResource SystemFillColorCautionBrush}"
                            CornerRadius="5"
                            Width="10"
                            Height="10"
                            IsVisible="{Binding BundlesBadgeVisible}"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Top"
                            Margin="28,7,0,0"/>
                </Panel>
            </ListBoxItem>

        </ListBox>

        <!-- Footer items -->
        <StackPanel Grid.Row="1" Margin="8,0,8,12" Spacing="2">
            <controls:UserAvatarControl HorizontalAlignment="Left" Margin="8,4,0,4"/>

            <!-- Settings & Package Managers are nav targets too, so they live in their own ListBox
                 (selection coordinated with the main one in code-behind) and share the nav-item look. -->
            <ListBox x:Name="FooterNavListBox"
                     SelectionMode="Single"
                     SelectionChanged="FooterNavListBox_SelectionChanged"
                     Background="Transparent"
                     BorderThickness="0"
                     Padding="0">

                <!-- Settings -->
                <ListBoxItem x:Name="SettingsNavBtn" Tag="Settings" Classes="nav-item" Padding="4,2" CornerRadius="6"
                             Tapped="SettingsNavBtn_Tapped"
                             automation:AutomationProperties.Name="{t:Translate Settings}"
                             automation:AutomationProperties.AcceleratorKey="Ctrl+5"
                             ToolTip.Tip="{t:Translate Settings}">
                    <StackPanel Orientation="Horizontal" Spacing="12" Margin="8,8">
                        <controls:SvgIcon x:Name="SettingsIcon" Path="avares://UniGetUI/Assets/Symbols/settings.svg" Width="24" Height="24" VerticalAlignment="Center" RenderTransformOrigin="50%,50%"/>
                        <TextBlock Text="{t:Translate Settings}" FontSize="14" VerticalAlignment="Center"
                                   IsVisible="{Binding $parent[local:SidebarView].ShowLabels}"/>
                    </StackPanel>
                </ListBoxItem>

                <!-- Package Managers -->
                <ListBoxItem x:Name="ManagersNavBtn" Tag="Managers" Classes="nav-item" Padding="4,2" CornerRadius="6"
                             automation:AutomationProperties.Name="{t:Translate Package Managers}"
                             automation:AutomationProperties.AcceleratorKey="Ctrl+6"
                             ToolTip.Tip="{t:Translate Package Managers}">
                    <StackPanel Orientation="Horizontal" Spacing="12" Margin="8,8">
                        <controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/package_managers.svg" Width="24" Height="24" VerticalAlignment="Center"/>
                        <TextBlock Text="{t:Translate Package Managers}" FontSize="14" VerticalAlignment="Center"
                                   IsVisible="{Binding $parent[local:SidebarView].ShowLabels}"/>
                    </StackPanel>
                </ListBoxItem>

                <!-- More is kept selected while its flyout is open so its navigation pill remains visible. -->
                <ListBoxItem x:Name="MoreNavBtn" Tag="More" Classes="nav-item" Padding="4,2" CornerRadius="6"
                             automation:AutomationProperties.Name="{t:Translate More}"
                             ToolTip.Tip="{t:Translate More}">
                    <StackPanel Orientation="Horizontal" Spacing="12" Margin="8,8">
                        <TextBlock Text="···" FontSize="20" Width="24" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"
                                   automation:AutomationProperties.AccessibilityView="Raw"/>
                        <TextBlock Text="{t:Translate More}" FontSize="14" VerticalAlignment="Center"
                                   IsVisible="{Binding $parent[local:SidebarView].ShowLabels}"/>
                    </StackPanel>
                    <FlyoutBase.AttachedFlyout>
                        <MenuFlyout Placement="RightEdgeAlignedTop">
                        <MenuItem Header="{t:Translate UniGetUI Log}" Command="{Binding RequestNavigationCommand}" CommandParameter="OwnLog">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/buggy.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{t:Translate Package Manager logs}" Command="{Binding RequestNavigationCommand}" CommandParameter="ManagerLog">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/console.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{t:Translate Operation history}" Command="{Binding RequestNavigationCommand}" CommandParameter="OperationHistory">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/history.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        <Separator/>
                        <MenuItem Header="{Binding VersionLabel}" IsEnabled="False">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/info_round.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{t:Translate Release notes}" Command="{Binding RequestNavigationCommand}" CommandParameter="ReleaseNotes">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/megaphone.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{t:Translate Check for UniGetUI updates}" Command="{Binding CheckForUpdatesCommand}">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/update.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        <Separator/>
                        <MenuItem Header="{t:Translate Help}" Command="{Binding RequestNavigationCommand}" CommandParameter="Help">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/help.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{t:Translate About}" Command="{Binding RequestNavigationCommand}" CommandParameter="About">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/info_round.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        <Separator/>
                        <MenuItem Header="{t:Translate Quit UniGetUI}" Command="{Binding RequestNavigationCommand}" CommandParameter="Quit">
                            <MenuItem.Icon><controls:SvgIcon Path="avares://UniGetUI/Assets/Symbols/close_round.svg" Width="16" Height="16"/></MenuItem.Icon>
                        </MenuItem>
                        </MenuFlyout>
                    </FlyoutBase.AttachedFlyout>
                </ListBoxItem>

            </ListBox>

        </StackPanel>

        <!-- One shared indicator moves between items so selection changes use WinUI's
             leading/trailing-edge stretch instead of fading separate item indicators. -->
        <Canvas Grid.RowSpan="2" IsHitTestVisible="False">
            <Border x:Name="NavigationSelectionPill"
                    Width="3"
                    Height="16"
                    CornerRadius="1.5"
                    Background="{DynamicResource AccentFillColorDefaultBrush}"
                    IsVisible="False"/>
        </Canvas>
    </Grid>
</UserControl>
