<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="AppControlManager.Pages.ViewFileCertificates"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppControlManager.Pages"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ui="using:CommunityToolkit.WinUI"
    xmlns:controls="using:CommunityToolkit.WinUI.Controls"
    xmlns:tk7controls="using:CommunityToolkit.WinUI.UI.Controls"
    xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    mc:Ignorable="d">

    <Page.Resources>
        <!--  These styles can be referenced to create a consistent SettingsPage layout  -->
        <!--  Spacing between cards  -->
        <x:Double x:Key="SettingsCardSpacing">4</x:Double>
        <!--  Style (inc. the correct spacing) of a section header  -->
        <Style x:Key="SettingsSectionHeaderTextBlockStyle"
          BasedOn="{StaticResource BodyStrongTextBlockStyle}"
          TargetType="TextBlock">
            <Style.Setters>
                <Setter Property="Margin" Value="1,30,0,6" />
            </Style.Setters>
        </Style>
    </Page.Resources>

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

        <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>

        <controls:WrapPanel Grid.Row="0" VerticalSpacing="4" HorizontalSpacing="4" Orientation="Vertical" Margin="5,5,5,5">

            <TextBlock
              TextWrapping="WrapWholeWords"
              Style="{StaticResource BodyTextBlockStyle}">

                  <Span>
                      View <Run Foreground="{ThemeResource SystemAccentColor}">Comprehensive</Run> information about the certificates used to sign files.
                        Details that are typically unavailable in File Explorer properties or PowerShell cmdlets are presented here in full.
                  </Span>
            </TextBlock>

            <HyperlinkButton Content="Guide" NavigateUri="https://github.com/HotCakeX/Harden-Windows-Security/wiki/View-File-Certificates" />

        </controls:WrapPanel>


        <StackPanel HorizontalAlignment="Stretch"
                      Spacing="{StaticResource SettingsCardSpacing}" Grid.Row="1" Margin="0,40,0,0">

            <win:StackPanel.ChildrenTransitions>
                <win:EntranceThemeTransition FromVerticalOffset="50" />
                <win:RepositionThemeTransition IsStaggeringEnabled="False" />
            </win:StackPanel.ChildrenTransitions>

            <controls:SettingsCard Description="Browse for a file to see all of its available certificates"
                                     x:Name="BrowseForFilesSettingsCard"
                  Header="Select File" Click="BrowseForFilesSettingsCard_Click" IsClickEnabled="True" IsActionIconVisible="False">

                <Button Content="Browse" x:Name="BrowseForFilesButton" Click="BrowseForFilesButton_Click"  />

            </controls:SettingsCard>

        </StackPanel>


        <TextBox Margin="15" Grid.Row="2" x:Name="SearchBox" Width="300" PlaceholderText="Search certificate details..." TextChanged="SearchBox_TextChanged" VerticalAlignment="Center" VerticalContentAlignment="Center" ToolTipService.ToolTip="Search among the certificate details data" />


        <Border Grid.Row="3" Style="{StaticResource GridCardStyle}" Margin="0" Padding="5">

            <tk7controls:DataGrid
                x:Name="FileCertificatesDataGrid"
                ItemsSource="{x:Bind FileCertificates, Mode=OneWay}"
                AllowDrop="False"
                AreRowDetailsFrozen="True"
                AutoGenerateColumns="False"
                CanDrag="False"
                HeadersVisibility="Column"
                IsReadOnly="True"
                IsTabStop="True"
                RowDetailsVisibilityMode="Collapsed"
                SelectionMode="Single"
                TabIndex="2"
                ScrollViewer.HorizontalScrollBarVisibility="Visible"
                ScrollViewer.VerticalScrollBarVisibility="Visible"
                ScrollViewer.VerticalScrollMode="Auto"
                ScrollViewer.HorizontalScrollMode="Auto"
                Loaded="FileCertificatesDataGrid_Loaded">
                <tk7controls:DataGrid.ContextFlyout>
                    <MenuFlyout>

                        <MenuFlyoutItem Text="Copy Row" Click="DataGridFlyoutMenuCopy_Click">
                            <MenuFlyoutItem.Icon>
                                <FontIcon Glyph="&#xE8C8;" />
                            </MenuFlyoutItem.Icon>
                        </MenuFlyoutItem>

                        <MenuFlyoutSubItem x:Name="CopyIndividualItemsSubMenu" Text="Copy Individual Items">
                            <MenuFlyoutSubItem.Icon>
                                <FontIcon Glyph="&#xE8C8;" />
                            </MenuFlyoutSubItem.Icon>
                            <!-- Dynamic items will be added here in the Loaded event handler -->
                        </MenuFlyoutSubItem>

                    </MenuFlyout>
                </tk7controls:DataGrid.ContextFlyout>
                <tk7controls:DataGrid.Columns>
                    <tk7controls:DataGridTextColumn Header="Signer Number" Binding="{Binding SignerNumber}" Tag="SignerNumber" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Type" Binding="{Binding Type}" Tag="Type" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Subject Common Name" Binding="{Binding SubjectCN}" Tag="SubjectCN" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Issuer Common Name" Binding="{Binding IssuerCN}" Tag="IssuerCN" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Not Before" Binding="{Binding NotBefore}" Tag="NotBefore" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Not After" Binding="{Binding NotAfter}" Tag="NotAfter" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Hashing Algorithm" Binding="{Binding HashingAlgorithm}" Tag="HashingAlgorithm" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Serial Number" Binding="{Binding SerialNumber}" Tag="SerialNumber" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Thumbprint" Binding="{Binding Thumbprint}" Tag="Thumbprint" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="TBS Hash" Binding="{Binding TBSHash}" Tag="TBSHash" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Extension OIDs" Binding="{Binding OIDs}" Tag="OIDs" Width="Auto"/>
                </tk7controls:DataGrid.Columns>
            </tk7controls:DataGrid>
        </Border>

    </Grid>

</Page>
