<Page
    x:Class="RegistryPreviewUILib.RegistryPreviewMainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converter="using:CommunityToolkit.WinUI.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:RegistryPreviewUILib"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:tk7controls="using:CommunityToolkit.WinUI.UI.Controls"
    xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls"
    xmlns:ui="using:CommunityToolkit.WinUI"
    mc:Ignorable="d">

    <Page.Resources>
        <ResourceDictionary>
            <converter:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
        </ResourceDictionary>
    </Page.Resources>

    <Grid
        x:Name="gridPreview"
        Grid.Row="1"
        Width="Auto"
        Height="Auto"
        Margin="12"
        x:FieldModifier="public"
        Loaded="GridPreview_Loaded"
        TabFocusNavigation="Cycle">
        <Grid.Resources>
            <Style x:Key="GridCardStyle" TargetType="Border">
                <Style.Setters>
                    <Setter Property="Background" Value="{ThemeResource CardBackgroundFillColorDefaultBrush}" />
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="BorderBrush" Value="{ThemeResource CardStrokeColorDefaultBrush}" />
                    <Setter Property="CornerRadius" Value="{StaticResource OverlayCornerRadius}" />
                </Style.Setters>
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <!--  Left, Splitter, Right  -->
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="8" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <!--  CommandBar, Tree, Splitter, List  -->
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="8" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Border
            Grid.Row="0"
            Grid.Column="0"
            Grid.ColumnSpan="3"
            Margin="0,0,0,12"
            Style="{StaticResource GridCardStyle}">

            <CommandBar
                Name="commandBar"
                HorizontalAlignment="Left"
                DefaultLabelPosition="Right">

                <AppBarButton
                    x:Name="newButton"
                    x:Uid="NewButton"
                    Click="NewButton_Click"
                    Icon="{ui:FontIcon Glyph=&#xe7c3;}">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="N" Modifiers="Control" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
                <AppBarButton
                    x:Name="openButton"
                    x:Uid="OpenButton"
                    Click="OpenButton_Click"
                    Icon="{ui:FontIcon Glyph=&#xe8e5;}">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="O" Modifiers="Control" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
                <AppBarButton
                    x:Name="refreshButton"
                    x:Uid="RefreshButton"
                    Click="RefreshButton_Click"
                    Icon="{ui:FontIcon Glyph=&#xe72c;}">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="F5" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
                <AppBarSeparator />
                <AppBarButton
                    x:Name="saveButton"
                    x:Uid="SaveButton"
                    Click="SaveButton_Click"
                    Icon="{ui:FontIcon Glyph=&#xe74e;}"
                    IsEnabled="False">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="S" Modifiers="Control" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
                <AppBarButton
                    x:Name="saveAsButton"
                    x:Uid="SaveAsButton"
                    Click="SaveAsButton_Click"
                    Icon="{ui:FontIcon Glyph=&#xe792;}"
                    IsEnabled="True">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="S" Modifiers="Control,Shift" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
                <AppBarSeparator />
                <AppBarButton
                    x:Name="editButton"
                    x:Uid="EditButton"
                    Click="EditButton_Click"
                    Icon="{ui:FontIcon Glyph=&#xe8a7;}">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="E" Modifiers="Control" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
                <AppBarButton
                    x:Name="writeButton"
                    x:Uid="WriteButton"
                    Click="WriteButton_Click"
                    Icon="{ui:FontIcon Glyph=&#xe72d;}">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="W" Modifiers="Control" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
                <AppBarButton
                    x:Name="registryButton"
                    x:Uid="RegistryButton"
                    Click="RegistryButton_Click"
                    Icon="{ui:FontIcon Glyph=&#xe8ad;}">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="R" Modifiers="Control" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
                <AppBarButton
                    x:Name="registryJumpToKeyButton"
                    x:Uid="RegistryJumpToKeyButton"
                    Click="RegistryJumpToKeyButton_Click"
                    IsEnabled="True">
                    <AppBarButton.KeyboardAccelerators>
                        <KeyboardAccelerator Key="R" Modifiers="Control,Shift" />
                    </AppBarButton.KeyboardAccelerators>
                </AppBarButton>
            </CommandBar>
        </Border>

        <local:MonacoEditorControl
            x:Name="MonacoEditor"
            Grid.Row="1"
            Grid.RowSpan="3"
            Grid.Column="0"
            IsTabStop="True" />

        <Border
            Grid.Row="1"
            Grid.Column="2"
            Style="{StaticResource GridCardStyle}">
            <TreeView
                x:Name="treeView"
                AllowDrop="False"
                AllowFocusOnInteraction="True"
                CanDragItems="False"
                CanReorderItems="False"
                IsEnabled="True"
                IsTabStop="False"
                ItemInvoked="TreeView_ItemInvoked"
                ScrollViewer.BringIntoViewOnFocusChange="True"
                ScrollViewer.HorizontalScrollBarVisibility="Visible"
                ScrollViewer.HorizontalScrollMode="Enabled"
                ScrollViewer.VerticalScrollBarVisibility="Visible"
                ScrollViewer.VerticalScrollMode="Auto"
                TabIndex="1">
                <TreeView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel
                            VerticalAlignment="Center"
                            IsTabStop="False"
                            Orientation="Horizontal"
                            Spacing="8">
                            <StackPanel.ContextFlyout>
                                <MenuFlyout>
                                    <MenuFlyoutItem x:Uid="ContextMenu_CopyKeyName" Command="{Binding Content.CopyToClipboardName_Click}" />
                                    <MenuFlyoutItem x:Uid="ContextMenu_CopyKeyPath" Command="{Binding Content.CopyToClipboardKeyPath_Click}" />
                                </MenuFlyout>
                            </StackPanel.ContextFlyout>
                            <Image
                                MaxWidth="16"
                                MaxHeight="16"
                                Source="{Binding Path=Content.Image}"
                                ToolTipService.ToolTip="{Binding Path=Content.ToolTipText}" />
                            <TextBlock Text="{Binding Path=Content.Name}" />
                        </StackPanel>
                    </DataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
        </Border>

        <Border
            Grid.Row="3"
            Grid.Column="2"
            Style="{StaticResource GridCardStyle}">
            <tk7controls:DataGrid
                x:Name="dataGrid"
                AllowDrop="False"
                AreRowDetailsFrozen="True"
                AutoGenerateColumns="False"
                CanDrag="False"
                HeadersVisibility="Column"
                IsReadOnly="True"
                IsTabStop="true"
                ItemsSource="{x:Bind listRegistryValues}"
                RowDetailsVisibilityMode="Collapsed"
                SelectionMode="Single"
                TabIndex="2">
                <tk7controls:DataGrid.Columns>
                    <tk7controls:DataGridTemplateColumn
                        x:Uid="NameColumn"
                        Width="Auto"
                        IsReadOnly="True">
                        <tk7controls:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel
                                    Margin="4"
                                    VerticalAlignment="Center"
                                    Orientation="Horizontal"
                                    Spacing="8">
                                    <StackPanel.ContextFlyout>
                                        <MenuFlyout>
                                            <MenuFlyoutItem
                                                x:Uid="ContextMenu_CopyName"
                                                Command="{Binding CopyToClipboardName_Click}"
                                                Icon="Copy" />
                                            <MenuFlyoutItem x:Uid="ContextMenu_CopyValue" Command="{Binding CopyToClipboardEntry_Click}" />
                                            <MenuFlyoutItem x:Uid="ContextMenu_CopyValueWithPath" Command="{Binding CopyToClipboardWithPath_Click}" />
                                            <MenuFlyoutSeparator />
                                            <MenuFlyoutItem
                                                x:Uid="ContextMenu_DataPreview"
                                                Click="MenuExtendedPreview_Click"
                                                Icon="Zoom"
                                                IsEnabled="{Binding ShowPreviewButton}" />
                                        </MenuFlyout>
                                    </StackPanel.ContextFlyout>
                                    <Image
                                        MaxWidth="16"
                                        MaxHeight="16"
                                        IsTabStop="False"
                                        Source="{Binding ImageUri}"
                                        ToolTipService.ToolTip="{Binding ToolTipText}" />
                                    <TextBlock
                                        IsTabStop="False"
                                        Style="{StaticResource CaptionTextBlockStyle}"
                                        Text="{Binding Name}" />
                                </StackPanel>
                            </DataTemplate>
                        </tk7controls:DataGridTemplateColumn.CellTemplate>
                    </tk7controls:DataGridTemplateColumn>
                    <tk7controls:DataGridTextColumn
                        x:Uid="TypeColumn"
                        Width="Auto"
                        Binding="{Binding Type}"
                        FontSize="{StaticResource CaptionTextBlockFontSize}">
                        <tk7controls:DataGridTextColumn.CellStyle>
                            <Style TargetType="tk7controls:DataGridCell">
                                <Setter Property="ContextFlyout">
                                    <Setter.Value>
                                        <MenuFlyout>
                                            <MenuFlyoutItem
                                                x:Uid="ContextMenu_CopyType"
                                                Command="{Binding CopyToClipboardType_Click}"
                                                Icon="Copy" />
                                            <MenuFlyoutItem x:Uid="ContextMenu_CopyValue" Command="{Binding CopyToClipboardEntry_Click}" />
                                            <MenuFlyoutItem x:Uid="ContextMenu_CopyValueWithPath" Command="{Binding CopyToClipboardWithPath_Click}" />
                                            <MenuFlyoutSeparator />
                                            <MenuFlyoutItem
                                                x:Uid="ContextMenu_DataPreview"
                                                Click="MenuExtendedPreview_Click"
                                                Icon="Zoom"
                                                IsEnabled="{Binding ShowPreviewButton}" />
                                        </MenuFlyout>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </tk7controls:DataGridTextColumn.CellStyle>
                    </tk7controls:DataGridTextColumn>
                    <tk7controls:DataGridTemplateColumn
                        x:Uid="ValueColumn"
                        Width="Auto"
                        IsReadOnly="True">
                        <tk7controls:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel
                                    Margin="4"
                                    VerticalAlignment="Center"
                                    Orientation="Horizontal"
                                    Spacing="6">
                                    <StackPanel.ContextFlyout>
                                        <MenuFlyout>
                                            <MenuFlyoutItem
                                                x:Uid="ContextMenu_CopyData"
                                                Command="{Binding CopyToClipboardData_Click}"
                                                Icon="Copy" />
                                            <MenuFlyoutItem x:Uid="ContextMenu_CopyValue" Command="{Binding CopyToClipboardEntry_Click}" />
                                            <MenuFlyoutItem x:Uid="ContextMenu_CopyValueWithPath" Command="{Binding CopyToClipboardWithPath_Click}" />
                                            <MenuFlyoutSeparator />
                                            <MenuFlyoutItem
                                                x:Uid="ContextMenu_DataPreview"
                                                Click="MenuExtendedPreview_Click"
                                                Icon="Zoom"
                                                IsEnabled="{Binding ShowPreviewButton}" />
                                        </MenuFlyout>
                                    </StackPanel.ContextFlyout>
                                    <Button
                                        x:Uid="ShowDataPreviewButton"
                                        Padding="2"
                                        Click="ButtonExtendedPreview_Click"
                                        Content="&#xe71e;"
                                        FontFamily="{StaticResource SymbolThemeFontFamily}"
                                        Visibility="{Binding ShowPreviewButton, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
                                    <TextBlock
                                        VerticalAlignment="Center"
                                        IsTabStop="False"
                                        Style="{StaticResource CaptionTextBlockStyle}"
                                        Text="{Binding ValueOneLine}" />
                                </StackPanel>
                            </DataTemplate>
                        </tk7controls:DataGridTemplateColumn.CellTemplate>
                    </tk7controls:DataGridTemplateColumn>
                </tk7controls:DataGrid.Columns>
            </tk7controls:DataGrid>
        </Border>

        <tkcontrols:GridSplitter
            x:Name="verticalSplitter"
            Grid.Row="1"
            Grid.RowSpan="3"
            Grid.Column="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Stretch"
            IsTabStop="False" />
        <tkcontrols:GridSplitter
            x:Name="horizontalSplitter"
            Grid.Row="2"
            Grid.Column="2"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Center"
            IsTabStop="False" />
    </Grid>
</Page>
