<Window
    x:Class="Nickvision.Parabolic.WinUI.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Nickvision.Parabolic.WinUI.Views"
    xmlns:nickvision="using:Nickvision.Desktop.WinUI.Controls"
    xmlns:community="using:CommunityToolkit.WinUI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Window.SystemBackdrop>
        <MicaBackdrop/>
    </Window.SystemBackdrop>

    <Grid x:Name="MainGrid" Loaded="Window_Loaded">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TitleBar x:Name="TitleBar" Grid.Row="0" IsBackButtonVisible="False" BackRequested="TitleBar_BackRequested">
            <TitleBar.LeftHeader>
                <Grid Margin="8,0,0,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <ImageIcon Grid.Column="0" VerticalAlignment="Center" Height="18" Source="/Assets/org.nickvision.tubeconverter.ico"/>

                    <TextBlock x:Name="LblTitle" Grid.Column="1" Margin="12,0,6,0" VerticalAlignment="Center" Style="{ThemeResource CaptionTextBlockStyle}"/>

                    <MenuBar Grid.Column="2">
                        <MenuBarItem x:Name="MenuFile">
                            <MenuFlyoutItem x:Name="MenuAddDownload" Click="AddDownload">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="N" Modifiers="Control"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE710;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutSeparator/>

                            <MenuFlyoutItem x:Name="MenuExit" Click="Exit">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="F4" Modifiers="Menu"/>

                                    <KeyboardAccelerator Key="Q" Modifiers="Control"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE894;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>
                        </MenuBarItem>

                        <MenuBarItem x:Name="MenuEdit">
                            <MenuFlyoutItem x:Name="MenuKeyring" Click="Keyring">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="K" Modifiers="Control"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE72E;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutItem x:Name="MenuSettings" Click="Settings">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="I" Modifiers="Control"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE713;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>
                        </MenuBarItem>

                        <MenuBarItem x:Name="MenuView">
                            <MenuFlyoutItem x:Name="MenuHistory" Click="History">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="H" Modifiers="Control"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE81C;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>
                        </MenuBarItem>

                        <MenuBarItem x:Name="MenuDownloads">
                            <MenuFlyoutItem x:Name="MenuStopAllRemaining" Click="StopAllRemaining">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="S" Modifiers="Control,Shift"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE71A;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutItem x:Name="MenuRetryAllFailed" Click="RetryAllFailed">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="R" Modifiers="Control,Shift"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE72C;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutSeparator/>

                            <MenuFlyoutItem x:Name="MenuClearAllQueued" Click="ClearAllQueued">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE894;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutItem x:Name="MenuClearAllCompleted" Click="ClearAllCompleted">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE894;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>
                        </MenuBarItem>

                        <MenuBarItem x:Name="MenuHelp">
                            <MenuFlyoutItem x:Name="MenuCheckForUpdates" Click="CheckForUpdates">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xF0C5;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutSeparator/>

                            <MenuFlyoutItem x:Name="MenuGitHubRepo" Click="GitHubRepo">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE8F1;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutItem x:Name="MenuReportABug" Click="ReportABug">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="I" Modifiers="Control,Shift"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xEBE8;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutItem x:Name="MenuDiscussions" Click="Discussions">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE8F2;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutSeparator/>

                            <MenuFlyoutItem x:Name="MenuAbout" Click="About">
                                <MenuFlyoutItem.KeyboardAccelerators>
                                    <KeyboardAccelerator Key="F1" Modifiers="None"/>
                                </MenuFlyoutItem.KeyboardAccelerators>

                                <MenuFlyoutItem.Icon>
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE946;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>
                        </MenuBarItem>
                    </MenuBar>
                </Grid>
            </TitleBar.LeftHeader>

            <TitleBar.RightHeader>
                <StackPanel Orientation="Horizontal" Spacing="6">
                    <Button x:Name="BtnPreview" Background="Transparent" BorderThickness="0" Visibility="Collapsed">
                        <Button.Content>
                            <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" FontSize="18" Glyph="&#xE794;"/>
                        </Button.Content>

                        <Button.Flyout>
                            <Flyout>
                                <TextBlock x:Name="LblPreview" TextWrapping="Wrap"/>
                            </Flyout>
                        </Button.Flyout>
                    </Button>

                    <Button x:Name="BtnUpdateProgress" Background="Transparent" BorderThickness="0" Visibility="Collapsed">
                        <Button.Content>
                            <ProgressRing x:Name="RingUpdateProcess" Width="24" Height="24" IsIndeterminate="False"/>
                        </Button.Content>

                        <Button.Flyout>
                            <Flyout x:Name="FlyoutUpdateProgress" Placement="Bottom">
                                <TextBlock x:Name="LblUpdateProgress" TextWrapping="WrapWholeWords"/>
                            </Flyout>
                        </Button.Flyout>
                    </Button>
                </StackPanel>
            </TitleBar.RightHeader>
        </TitleBar>

        <MenuFlyoutSeparator Grid.Row="1"/>

        <Grid Grid.Row="2">
            <nickvision:ViewStack x:Name="ViewStack" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <nickvision:ViewStack.Pages>
                    <StackPanel Margin="48,48,48,48" HorizontalAlignment="Stretch" Orientation="Vertical" Spacing="12">
                        <Image HorizontalAlignment="Left" Width="64" Height="64" Source="/Assets/org.nickvision.tubeconverter.ico"/>

                        <TextBlock x:Name="LblHomeTitle" HorizontalAlignment="Left" Style="{ThemeResource TitleTextBlockStyle}"/>

                        <TextBlock x:Name="LblHomeDescription" HorizontalAlignment="Left" Foreground="Gray"/>

                        <HyperlinkButton Margin="0,24,0,0" Click="AddDownload">
                            <HyperlinkButton.Content>
                                <StackPanel Orientation="Horizontal" Spacing="6">
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE710;"/>

                                    <TextBlock x:Name="LblAddDownload"/>
                                </StackPanel>
                            </HyperlinkButton.Content>
                        </HyperlinkButton>

                        <HyperlinkButton Click="Keyring">
                            <HyperlinkButton.Content>
                                <StackPanel Orientation="Horizontal" Spacing="6">
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE72E;"/>

                                    <TextBlock x:Name="LblKeyring"/>
                                </StackPanel>
                            </HyperlinkButton.Content>
                        </HyperlinkButton>

                        <HyperlinkButton Click="Settings">
                            <HyperlinkButton.Content>
                                <StackPanel Orientation="Horizontal" Spacing="6">
                                    <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE713;"/>

                                    <TextBlock x:Name="LblSettings"/>
                                </StackPanel>
                            </HyperlinkButton.Content>
                        </HyperlinkButton>
                    </StackPanel>

                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <Grid Grid.Row="0" Margin="12,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <Button Grid.Column="1" Style="{ThemeResource AccentButtonStyle}" Click="AddDownload">
                                <Button.Content>
                                    <StackPanel Orientation="Horizontal" Spacing="6">
                                        <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE710;"/>

                                        <TextBlock x:Name="LblDownloadsAddDownload"/>
                                    </StackPanel>
                                </Button.Content>
                            </Button>

                            <CommandBar Grid.Column="2" DefaultLabelPosition="Right">
                                <CommandBar.PrimaryCommands>
                                    <AppBarButton x:Name="BtnStopAllRemaining" Click="StopAllRemaining">
                                        <AppBarButton.Icon>
                                            <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE71A;"/>
                                        </AppBarButton.Icon>
                                    </AppBarButton>

                                    <AppBarButton x:Name="BtnRetryAllFailed" Click="RetryAllFailed">
                                        <AppBarButton.Icon>
                                            <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE72C;"/>
                                        </AppBarButton.Icon>
                                    </AppBarButton>

                                    <AppBarSeparator/>
                                </CommandBar.PrimaryCommands>

                                <CommandBar.SecondaryCommands>
                                    <AppBarButton x:Name="BtnClearAllQueued" Click="ClearAllQueued">
                                        <AppBarButton.Icon>
                                            <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE894;"/>
                                        </AppBarButton.Icon>
                                    </AppBarButton>

                                    <AppBarButton x:Name="BtnClearAllCompleted" Click="ClearAllCompleted">
                                        <AppBarButton.Icon>
                                            <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE894;"/>
                                        </AppBarButton.Icon>
                                    </AppBarButton>
                                </CommandBar.SecondaryCommands>
                            </CommandBar>
                        </Grid>

                        <MenuFlyoutSeparator Grid.Row="1" HorizontalAlignment="Stretch"/>

                        <NavigationView x:Name="NavViewDownloads" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                        IsPaneToggleButtonVisible="False" IsBackButtonVisible="Collapsed" IsSettingsVisible="False" PaneDisplayMode="Left"
                                        SelectionChanged="NavViewDownloads_SelectionChanged">
                            <NavigationView.Resources>
                                <SolidColorBrush x:Key="NavigationViewContentBackground" Color="Transparent"/>
                                <SolidColorBrush x:Key="NavigationViewContentGridBorderBrush" Color="Transparent"/>
                            </NavigationView.Resources>

                            <NavigationView.MenuItems>
                                <NavigationViewItem x:Name="NavDownloadsAll" Tag="0" IsSelected="True">
                                    <NavigationViewItem.Icon>
                                        <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE71D;"/>
                                    </NavigationViewItem.Icon>

                                    <NavigationViewItem.InfoBadge>
                                        <InfoBadge x:Name="InfoBadgeDownloadsAll" Style="{StaticResource AttentionValueInfoBadgeStyle}" Visibility="Collapsed"/>
                                    </NavigationViewItem.InfoBadge>
                                </NavigationViewItem>

                                <NavigationViewItem x:Name="NavDownloadsRunning" Tag="1">
                                    <NavigationViewItem.Icon>
                                        <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE768;"/>
                                    </NavigationViewItem.Icon>

                                    <NavigationViewItem.InfoBadge>
                                        <InfoBadge x:Name="InfoBadgeDownloadsRunning" Style="{StaticResource AttentionValueInfoBadgeStyle}" Visibility="Collapsed"/>
                                    </NavigationViewItem.InfoBadge>
                                </NavigationViewItem>

                                <NavigationViewItem x:Name="NavDownloadsQueued" Tag="2">
                                    <NavigationViewItem.Icon>
                                        <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE917;"/>
                                    </NavigationViewItem.Icon>

                                    <NavigationViewItem.InfoBadge>
                                        <InfoBadge x:Name="InfoBadgeDownloadsQueued" Style="{StaticResource AttentionValueInfoBadgeStyle}" Visibility="Collapsed"/>
                                    </NavigationViewItem.InfoBadge>
                                </NavigationViewItem>

                                <NavigationViewItem x:Name="NavDownloadsCompleted" Tag="3">
                                    <NavigationViewItem.Icon>
                                        <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE73E;"/>
                                    </NavigationViewItem.Icon>

                                    <NavigationViewItem.InfoBadge>
                                        <InfoBadge x:Name="InfoBadgeDownloadsCompleted" Style="{StaticResource AttentionValueInfoBadgeStyle}" Visibility="Collapsed"/>
                                    </NavigationViewItem.InfoBadge>
                                </NavigationViewItem>

                                <NavigationViewItem x:Name="NavDownloadsFailed" Tag="4">
                                    <NavigationViewItem.Icon>
                                        <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE783;"/>
                                    </NavigationViewItem.Icon>

                                    <NavigationViewItem.InfoBadge>
                                        <InfoBadge x:Name="InfoBadgeDownloadsFailed" Style="{StaticResource AttentionValueInfoBadgeStyle}" Visibility="Collapsed"/>
                                    </NavigationViewItem.InfoBadge>
                                </NavigationViewItem>
                            </NavigationView.MenuItems>

                            <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                                <nickvision:ViewStack x:Name="ViewStackDownloads">
                                    <nickvision:ViewStack.Pages>
                                        <nickvision:StatusPage x:Name="StatusNoneDownloads" Glyph="&#xE896;">
                                            <nickvision:StatusPage.Child>
                                                <Button Click="AddDownload">
                                                    <Button.Content>
                                                        <StackPanel Orientation="Horizontal" Spacing="6">
                                                            <FontIcon FontFamily="{ThemeResource SymbolThemeFontFamily}" Glyph="&#xE710;"/>

                                                            <TextBlock x:Name="LblNoneAddDownload"/>
                                                        </StackPanel>
                                                    </Button.Content>
                                                </Button>
                                            </nickvision:StatusPage.Child>
                                        </nickvision:StatusPage>

                                        <ItemsRepeater x:Name="ListDownloads" Margin="12,6,12,0">
                                            <ItemsRepeater.Layout>
                                                <StackLayout Spacing="6"/>
                                            </ItemsRepeater.Layout>
                                        </ItemsRepeater>
                                    </nickvision:ViewStack.Pages>
                                </nickvision:ViewStack>
                            </ScrollViewer>
                        </NavigationView>
                    </Grid>

                    <Frame x:Name="FrameCustom"/>
                </nickvision:ViewStack.Pages>
            </nickvision:ViewStack>

            <InfoBar x:Name="InfoBar" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,12" IsOpen="False">
                <InfoBar.ActionButton>
                    <Button x:Name="BtnInfoBar" HorizontalAlignment="Right" Visibility="Collapsed"/>
                </InfoBar.ActionButton>
            </InfoBar>
        </Grid>

        <ContentDialog x:Name="DlgCredential" DefaultButton="Primary">
            <StackPanel Orientation="Vertical" Spacing="12">
                <TextBlock x:Name="LblCredentialRequired"/>

                <TextBox x:Name="TxtCredentialUrl" IsReadOnly="True"/>

                <TextBox x:Name="TxtCredentialUsername"/>

                <PasswordBox x:Name="TxtCredentialPassword"/>
            </StackPanel>
        </ContentDialog>
    </Grid>
</Window>
