<Page
    x:Class="AdvancedPaste.Pages.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:AdvancedPaste.Controls"
    xmlns:converters="using:AdvancedPaste.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:library="using:Microsoft.PowerToys.Settings.UI.Library"
    xmlns:local="using:AdvancedPaste.Models"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:tkconverters="using:CommunityToolkit.WinUI.Converters"
    xmlns:ui="using:CommunityToolkit.WinUI"
    KeyDown="Page_KeyDown"
    KeyboardAcceleratorPlacementMode="Hidden"
    mc:Ignorable="d">
    <Page.Resources>
        <tkconverters:BoolToVisibilityConverter x:Name="BoolToVisibilityConverter" />
        <converters:CountToVisibilityConverter x:Name="countToVisibilityConverter" />
        <converters:PasteFormatsToHeightConverter x:Name="standardPasteFormatsToHeightConverter" />
        <converters:CountToDoubleConverter
            x:Name="customActionsToMinHeightConverter"
            ValueIfNonZero="40"
            ValueIfZero="0" />
        <controls:PasteFormatTemplateSelector x:Key="PasteFormatTemplateSelector">
            <controls:PasteFormatTemplateSelector.ItemTemplate>
                <DataTemplate x:DataType="local:PasteFormat">
                    <Grid
                        Margin="0"
                        Padding="-9,0,0,0"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        AutomationProperties.AcceleratorKey="{x:Bind ShortcutText, Mode=OneTime}"
                        AutomationProperties.AutomationControlType="ListItem"
                        AutomationProperties.FullDescription="{x:Bind ToolTip, Mode=OneTime}"
                        AutomationProperties.HelpText="{x:Bind Name, Mode=OneTime}"
                        AutomationProperties.Name="{x:Bind AccessibleName, Mode=OneTime}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="48" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <ToolTipService.ToolTip>
                            <TextBlock Text="{x:Bind ToolTip, Mode=OneTime}" />
                        </ToolTipService.ToolTip>
                        <FontIcon
                            Margin="0,0,0,0"
                            VerticalAlignment="Center"
                            AutomationProperties.AccessibilityView="Raw"
                            FontSize="16"
                            Glyph="{x:Bind IconGlyph, Mode=OneTime}" />
                        <TextBlock
                            Grid.Column="1"
                            VerticalAlignment="Center"
                            x:Phase="1"
                            Style="{StaticResource CaptionTextBlockStyle}"
                            Text="{x:Bind Name, Mode=OneTime}" />
                        <TextBlock
                            Grid.Column="2"
                            Margin="0,0,8,0"
                            HorizontalAlignment="Right"
                            VerticalAlignment="Center"
                            Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                            Style="{StaticResource CaptionTextBlockStyle}"
                            Text="{x:Bind ShortcutText, Mode=OneTime}" />
                    </Grid>
                </DataTemplate>
            </controls:PasteFormatTemplateSelector.ItemTemplate>
            <controls:PasteFormatTemplateSelector.ItemTemplateDisabled>
                <DataTemplate x:DataType="local:PasteFormat" AutomationProperties.AccessibilityView="Raw">
                    <Grid
                        Margin="0"
                        Padding="-9,0,0,0"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        AutomationProperties.AccessibilityView="Raw"
                        Opacity="0.5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="48" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <FontIcon
                            Margin="0,0,0,0"
                            VerticalAlignment="Center"
                            FontSize="16"
                            Glyph="{x:Bind IconGlyph, Mode=OneTime}" />
                        <TextBlock
                            Grid.Column="1"
                            VerticalAlignment="Center"
                            x:Phase="1"
                            Style="{StaticResource CaptionTextBlockStyle}"
                            Text="{x:Bind Name, Mode=OneTime}" />
                    </Grid>
                </DataTemplate>
            </controls:PasteFormatTemplateSelector.ItemTemplateDisabled>
        </controls:PasteFormatTemplateSelector>
        <Style
            x:Key="PaddingLessFlyoutPresenterStyle"
            BasedOn="{StaticResource DefaultFlyoutPresenterStyle}"
            TargetType="FlyoutPresenter">
            <Style.Setters>
                <Setter Property="Padding" Value="0" />
            </Style.Setters>
        </Style>
    </Page.Resources>
    <Page.KeyboardAccelerators>
        <KeyboardAccelerator Key="Escape" Invoked="KeyboardAccelerator_Invoked" />
        <KeyboardAccelerator
            Key="Number1"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
        <KeyboardAccelerator
            Key="Number2"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
        <KeyboardAccelerator
            Key="Number3"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
        <KeyboardAccelerator
            Key="Number4"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
        <KeyboardAccelerator
            Key="Number5"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
        <KeyboardAccelerator
            Key="Number6"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
        <KeyboardAccelerator
            Key="Number7"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
        <KeyboardAccelerator
            Key="Number8"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
        <KeyboardAccelerator
            Key="Number9"
            Invoked="KeyboardAccelerator_Invoked"
            Modifiers="Control" />
    </Page.KeyboardAccelerators>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid
            Margin="8,0,8,16"
            Padding="4"
            Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
            BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
            BorderThickness="1"
            CornerRadius="20"
            Visibility="{x:Bind ViewModel.ShowClipboardPreview, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <controls:ClipboardHistoryItemPreviewControl Height="48" ClipboardItem="{x:Bind ViewModel.CurrentClipboardItem, Mode=OneWay}" />
            <Button
                x:Uid="ClipboardHistoryButton"
                Grid.Column="1"
                Margin="0,0,4,0"
                VerticalAlignment="Center"
                IsEnabled="{x:Bind ViewModel.ClipboardHistoryEnabled, Mode=TwoWay}"
                Style="{StaticResource SubtleButtonStyle}"
                Visibility="{x:Bind ViewModel.ShowClipboardHistoryButton, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}">
                <ToolTipService.ToolTip>
                    <TextBlock x:Uid="ClipboardHistoryButtonToolTip" />
                </ToolTipService.ToolTip>
                <FontIcon
                    Margin="0,0,0,0"
                    VerticalAlignment="Center"
                    AutomationProperties.AccessibilityView="Raw"
                    FontSize="16"
                    Glyph="&#xE81C;" />
                <Button.Flyout>
                    <Flyout
                        FlyoutPresenterStyle="{StaticResource PaddingLessFlyoutPresenterStyle}"
                        Placement="Right"
                        ShouldConstrainToRootBounds="False">
                        <ItemsView
                            Width="320"
                            Margin="8,8,8,0"
                            IsItemInvokedEnabled="True"
                            ItemInvoked="ClipboardHistory_ItemInvoked"
                            ItemsSource="{x:Bind clipboardHistory, Mode=OneWay}"
                            SelectionMode="None">
                            <ItemsView.Layout>
                                <StackLayout Orientation="Vertical" Spacing="8" />
                            </ItemsView.Layout>
                            <ItemsView.Transitions />
                            <ItemsView.ItemTemplate>
                                <DataTemplate x:DataType="local:ClipboardItem">
                                    <ItemContainer
                                        AutomationProperties.Name="{x:Bind Description, Mode=OneTime}"
                                        CornerRadius="16"
                                        ToolTipService.ToolTip="{x:Bind Content}">
                                        <Grid
                                            Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                            BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                            BorderThickness="1"
                                            ColumnSpacing="8"
                                            CornerRadius="16">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" MaxWidth="240" />
                                                <ColumnDefinition Width="Auto" />
                                            </Grid.ColumnDefinitions>
                                            <controls:ClipboardHistoryItemPreviewControl
                                                Height="64"
                                                x:Phase="0"
                                                ClipboardItem="{x:Bind}" />
                                            <Button
                                                x:Name="ClipboardHistoryItemMoreOptionsButton"
                                                x:Uid="ClipboardHistoryItemMoreOptionsButton"
                                                Grid.Column="1"
                                                VerticalAlignment="Center"
                                                Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                                                Style="{StaticResource SubtleButtonStyle}">
                                                <Button.Content>
                                                    <FontIcon FontSize="16" Glyph="&#xe712;" />
                                                </Button.Content>
                                                <Button.Flyout>
                                                    <MenuFlyout>
                                                        <MenuFlyoutItem
                                                            x:Uid="ClipboardHistoryItemDeleteButton"
                                                            Click="ClipboardHistoryItemDeleteButton_Click"
                                                            CommandParameter="{x:Bind (local:ClipboardItem)}"
                                                            Icon="Delete" />
                                                    </MenuFlyout>
                                                </Button.Flyout>
                                            </Button>
                                        </Grid>
                                    </ItemContainer>
                                </DataTemplate>
                            </ItemsView.ItemTemplate>
                        </ItemsView>
                    </Flyout>
                </Button.Flyout>
            </Button>
        </Grid>
        <controls:PromptBox
            x:Name="CustomFormatTextBox"
            x:Uid="CustomFormatTextBox"
            Grid.Row="1"
            Margin="20,0,20,0"
            x:FieldModifier="public"
            IsEnabled="{x:Bind ViewModel.IsCustomAIServiceEnabled, Mode=OneWay}"
            TabIndex="0">
            <controls:PromptBox.Footer>
                <StackPanel Orientation="Horizontal">
                    <TextBlock
                        x:Uid="AIMistakeNote"
                        Margin="0,0,2,0"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Center"
                        FontSize="10"
                        Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
                    <Button
                        Padding="0"
                        VerticalAlignment="Center"
                        Style="{StaticResource SubtleButtonStyle}"
                        Visibility="{x:Bind ViewModel.HasLegalLinks, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}">
                        <FontIcon FontSize="12" Glyph="&#xE946;" />
                        <Button.Flyout>
                            <Flyout>
                                <StackPanel Spacing="8">
                                    <TextBlock TextWrapping="Wrap">
                                        <Run x:Uid="AIMistakeNote" /><LineBreak /><Run
                                            x:Uid="CustomEndpointWarning"
                                            FontSize="12"
                                            Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
                                    </TextBlock>
                                    <StackPanel Orientation="Horizontal" Spacing="8">
                                        <HyperlinkButton
                                            x:Name="TermsHyperlink"
                                            x:Uid="TermsLink"
                                            Padding="0"
                                            FontSize="12"
                                            NavigateUri="{x:Bind ViewModel.TermsLinkUri, Mode=OneWay}"
                                            Visibility="{x:Bind ViewModel.HasTermsLink, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
                                        <HyperlinkButton
                                            x:Name="PrivacyHyperLink"
                                            x:Uid="PrivacyLink"
                                            Padding="0"
                                            FontSize="12"
                                            NavigateUri="{x:Bind ViewModel.PrivacyLinkUri, Mode=OneWay}"
                                            Visibility="{x:Bind ViewModel.HasPrivacyLink, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
                                    </StackPanel>
                                </StackPanel>
                            </Flyout>
                        </Button.Flyout>
                    </Button>
                </StackPanel>
            </controls:PromptBox.Footer>
        </controls:PromptBox>
        <ScrollViewer Grid.Row="2">
            <Grid RowSpacing="4">
                <Grid.RowDefinitions>
                    <RowDefinition Height="{x:Bind ViewModel.StandardPasteFormats.Count, Mode=OneWay, Converter={StaticResource standardPasteFormatsToHeightConverter}}" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" MinHeight="{x:Bind ViewModel.CustomActionPasteFormats.Count, Mode=OneWay, Converter={StaticResource customActionsToMinHeightConverter}}" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <ListView
                    x:Name="PasteOptionsListView"
                    Grid.Row="0"
                    VerticalAlignment="Bottom"
                    IsItemClickEnabled="True"
                    ItemClick="PasteFormat_ItemClick"
                    ItemContainerTransitions="{x:Null}"
                    ItemTemplateSelector="{StaticResource PasteFormatTemplateSelector}"
                    ItemsSource="{x:Bind ViewModel.StandardPasteFormats, Mode=OneWay}"
                    ScrollViewer.VerticalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollMode="Disabled"
                    SelectionMode="None"
                    TabIndex="1" />
                <Rectangle
                    Grid.Row="1"
                    Height="1"
                    HorizontalAlignment="Stretch"
                    Fill="{ThemeResource DividerStrokeColorDefaultBrush}"
                    Visibility="{x:Bind ViewModel.CustomActionPasteFormats.Count, Mode=OneWay, Converter={StaticResource countToVisibilityConverter}}" />

                <ListView
                    x:Name="CustomActionsListView"
                    Grid.Row="2"
                    VerticalAlignment="Top"
                    IsItemClickEnabled="True"
                    ItemClick="PasteFormat_ItemClick"
                    ItemContainerTransitions="{x:Null}"
                    ItemTemplateSelector="{StaticResource PasteFormatTemplateSelector}"
                    ItemsSource="{x:Bind ViewModel.CustomActionPasteFormats, Mode=OneWay}"
                    ScrollViewer.VerticalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollMode="Disabled"
                    SelectionMode="None"
                    TabIndex="2" />
            </Grid>
        </ScrollViewer>
    </Grid>
</Page>
