<controls:EnhancedPage
    x:Class="Pixeval.Pages.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Pixeval.Controls"
    xmlns:controls1="using:CommunityToolkit.WinUI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:fluent="using:FluentIcons.WinUI"
    xmlns:local="using:Pixeval.Pages"
    xmlns:management="using:Pixeval.AppManagement"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:win2d="using:Microsoft.Graphics.Canvas.UI.Xaml"
    xmlns:windowing="using:Pixeval.Controls.Windowing"
    AllowDrop="True"
    DragEnter="MainPage_OnDragEnter"
    DragLeave="MainPage_OnDragLeave"
    Drop="MainPage_OnDrop"
    Loaded="MainPage_OnLoaded"
    mc:Ignorable="d">
    <controls:EnhancedPage.Resources>
        <DataTemplate x:Key="CommonSuggestionModelTemplate" x:DataType="local:SuggestionModel">
            <controls1:DockPanel Padding="0,10" HorizontalSpacing="10">
                <ContentPresenter
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    controls1:DockPanel.Dock="Left"
                    Content="{x:Bind SymbolIcon}" />
                <StackPanel VerticalAlignment="Center" Spacing="1">
                    <TextBlock
                        MaxHeight="35"
                        Style="{StaticResource BodyTextBlockStyle}"
                        Text="{x:Bind Name}"
                        TextTrimming="CharacterEllipsis"
                        TextWrapping="WrapWholeWords" />
                    <TextBlock
                        MaxHeight="15"
                        Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                        Style="{StaticResource CaptionTextBlockStyle}"
                        Text="{x:Bind TranslatedName}"
                        TextTrimming="CharacterEllipsis"
                        TextWrapping="WrapWholeWords"
                        Visibility="{x:Bind controls:C.IsNotNullToVisibility(TranslatedName)}" />
                </StackPanel>
            </controls1:DockPanel>
        </DataTemplate>
        <local:SuggestionModelDataTemplateSelector
            x:Key="SuggestionModelDataTemplateSelector"
            AutoCompletionHeader="/MainPage/TagAutoCompletionSuggestionTextBlock"
            CommonSuggestion="{StaticResource CommonSuggestionModelTemplate}"
            IllustrationHeader="/MainPage/IllustrationHeaderSuggestionTextBlock"
            NovelHeader="/MainPage/NovelHeaderSuggestionTextBlock"
            SettingEntryHeader="/MainPage/SettingsEntryHeaderSuggestionTextBlock" />
        <DataTemplate x:Key="NavigationViewTagDataTemplate" x:DataType="windowing:NavigationViewTag">
            <NavigationViewItem
                Content="{x:Bind}"
                Icon="{x:Bind Icon}"
                Tag="{x:Bind}">
                <NavigationViewItem.InfoBadge>
                    <InfoBadge Style="{StaticResource AttentionIconInfoBadgeStyle}" Visibility="{x:Bind controls:C.ToVisibility(ShowIconBadge), Mode=OneWay}" />
                </NavigationViewItem.InfoBadge>
            </NavigationViewItem>
        </DataTemplate>
        <DataTemplate x:Key="NavigationViewSeparatorDataTemplate" x:DataType="windowing:NavigationViewSeparator">
            <NavigationViewItemSeparator />
        </DataTemplate>
        <local:NavigationViewItemDataTemplateSelector
            x:Key="NavigationViewItemDataTemplateSelector"
            NavigationViewSeparatorDataTemplate="{StaticResource NavigationViewSeparatorDataTemplate}"
            NavigationViewTagDataTemplate="{StaticResource NavigationViewTagDataTemplate}" />
    </controls:EnhancedPage.Resources>
    <Grid>
        <controls1:DockPanel>
            <controls1:TitleBar
                x:Name="TitleBar"
                Title="{x:Bind management:AppInfo.AppIdentifier}"
                VerticalAlignment="Top"
                controls1:DockPanel.Dock="Top"
                DisplayMode="Tall"
                IsPaneButtonVisible="True"
                PaneButtonClick="TitleBar_OnPaneButtonClicked"
                Window="{x:Bind Window}">
                <controls1:TitleBar.Icon>
                    <ImageIcon Source="ms-appx:///Assets/Images/logo.svg" />
                </controls1:TitleBar.Icon>
                <controls1:TitleBar.Footer>
                    <Button
                        Padding="1"
                        Background="{x:Bind controls:C.IsPremiumBorderBrush(_viewModel.IsPremium), Mode=OneWay}"
                        CornerRadius="18">
                        <Button.Flyout>
                            <MenuFlyout Placement="Bottom">
                                <MenuFlyoutItem
                                    x:Uid="/MainPage/OpenMyPageItem"
                                    Click="OpenMyPage_OnClick"
                                    Icon="{fluent:SymbolIcon Symbol=Open}" />
                                <MenuFlyoutItem
                                    x:Uid="/MainPage/OpenMyPageInBrowserItem"
                                    Click="OpenLinkViaTagUri_OnClicked"
                                    Icon="{fluent:SymbolIcon Symbol=GlobeArrowUp}"
                                    Tag="{x:Bind _viewModel.Url, Mode=OneWay}" />
                                <MenuFlyoutSeparator />
                                <ToggleMenuFlyoutItem
                                    x:Name="RestrictedModeItem"
                                    x:Uid="/MainPage/RestrictedModeItem"
                                    Click="RestrictedModeItem_OnClick"
                                    Icon="{fluent:SymbolIcon Symbol=PersonProhibited}"
                                    IsEnabled="{x:Bind controls:C.Negation(_viewModel.RestrictedModeProcessing), Mode=OneWay}"
                                    Visibility="Collapsed" />
                                <ToggleMenuFlyoutItem
                                    x:Name="AiShowItem"
                                    x:Uid="/MainPage/AiShowItem"
                                    Click="AiShowButtonItem_OnClick"
                                    Icon="{fluent:SymbolIcon Symbol=Bot}"
                                    IsEnabled="{x:Bind controls:C.Negation(_viewModel.AiShowProcessing), Mode=OneWay}" />
                                <MenuFlyoutItem
                                    x:Uid="/MainPage/MoreViewingSettingsItem"
                                    Click="OpenLinkViaTag_OnClicked"
                                    Icon="{fluent:SymbolIcon Symbol=MoreHorizontal}"
                                    Tag="https://www.pixiv.net/settings/viewing" />
                                <MenuFlyoutSeparator />
                                <MenuFlyoutItem
                                    x:Uid="/MainPage/LogoutItem"
                                    Click="Logout_OnClicked"
                                    Icon="{fluent:SymbolIcon Symbol=SignOut}" />
                            </MenuFlyout>
                        </Button.Flyout>
                        <PersonPicture
                            MaxHeight="33"
                            ProfilePicture="{x:Bind _viewModel.AvatarSource, Mode=OneWay}"
                            ToolTipService.ToolTip="{x:Bind _viewModel.UserName, Mode=OneWay}" />
                    </Button>
                </controls1:TitleBar.Footer>
                <controls1:TitleBar.Content>
                    <controls1:DockPanel>
                        <CommandBar
                            Margin="0,-4"
                            controls1:DockPanel.Dock="Right"
                            DefaultLabelPosition="Collapsed"
                            OverflowButtonVisibility="Collapsed">
                            <AppBarButton
                                x:Uid="/MainPage/ReverseSearchButton"
                                Click="ReverseSearchButton_OnClicked"
                                Content="{fluent:SymbolIcon Symbol=ImageSearch}" />
                            <AppBarButton
                                x:Uid="/MainPage/OpenSearchSettingButton"
                                Click="OpenSearchSettingButton_OnClicked"
                                Content="{fluent:SymbolIcon Symbol=Settings}" />
                        </CommandBar>
                        <AutoSuggestBox
                            x:Name="MainPageAutoSuggestionBox"
                            x:Uid="/MainPage/MainPageAutoSuggestionBox"
                            VerticalAlignment="Center"
                            GotFocus="KeywordAutoSuggestBox_GotFocus"
                            IsFocusEngaged="False"
                            ItemTemplateSelector="{StaticResource SuggestionModelDataTemplateSelector}"
                            ItemsSource="{x:Bind _viewModel.SuggestionProvider.Suggestions, Mode=OneWay}"
                            KeyDown="KeywordAutoSuggestionBox_OnKeyDown"
                            QueryIcon="{fluent:SymbolIcon Symbol=Search,
                                                          FontSize={StaticResource SmallIconFontSize}}"
                            QuerySubmitted="KeywordAutoSuggestBox_QuerySubmitted"
                            SuggestionChosen="KeywordAutoSuggestBox_OnSuggestionChosen"
                            TextChanged="KeywordAutoSuggestBox_OnTextChanged">
                            <AutoSuggestBox.ItemContainerTransitions>
                                <TransitionCollection>
                                    <AddDeleteThemeTransition />
                                </TransitionCollection>
                            </AutoSuggestBox.ItemContainerTransitions>
                        </AutoSuggestBox>
                    </controls1:DockPanel>
                </controls1:TitleBar.Content>
            </controls1:TitleBar>
            <NavigationView
                x:Name="NavigationView"
                FooterMenuItemsSource="{x:Bind _viewModel.FooterMenuItems}"
                IsBackButtonVisible="Collapsed"
                IsPaneToggleButtonVisible="False"
                IsSettingsVisible="False"
                ItemInvoked="NavigationView_OnItemInvoked"
                MenuItemTemplateSelector="{StaticResource NavigationViewItemDataTemplateSelector}"
                MenuItemsSource="{x:Bind _viewModel.MenuItems}"
                OpenPaneLength="{x:Bind _viewModel.MainPageRootNavigationViewOpenPanelLength}">
                <NavigationView.Resources>
                    <StaticResource x:Key="NavigationViewContentBackground" ResourceKey="SystemControlTransparentBrush" />
                </NavigationView.Resources>
                <windowing:TabPage x:Name="MainPageRootTab" />
            </NavigationView>
        </controls1:DockPanel>
        <Grid
            x:Name="ImageSearchGrid"
            Background="{StaticResource PixevalAppAcrylicBrush}"
            Opacity="0"
            Visibility="{x:Bind controls:C.IsNotZeroDToVisibility(ImageSearchGrid.Opacity), Mode=OneWay}">
            <Grid.OpacityTransition>
                <ScalarTransition Duration="0:0:0.5" />
            </Grid.OpacityTransition>
            <win2d:CanvasControl
                x:Name="CanvasControl"
                Margin="20"
                Draw="CanvasControl_OnDraw" />
            <TextBlock
                x:Uid="/MainPage/ReverseSearchDropHintTextBlock"
                Margin="40"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Style="{StaticResource TitleTextBlockStyle}" />
        </Grid>
    </Grid>
</controls:EnhancedPage>
