<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="AppControlManager.Pages.EventLogsPolicyCreation"
    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:animations="using:CommunityToolkit.WinUI.Animations"
    mc:Ignorable="d">


    <Page.Resources>
        <Style x:Key="PanelStyle"
               TargetType="StackPanel">
            <Setter Property="CornerRadius" Value="8" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0,8,0,8" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="BorderBrush" Value="{ThemeResource CardStrokeColorDefaultBrush}" />
            <Setter Property="Orientation" Value="Horizontal" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="Spacing" Value="8" />
            <Setter Property="animations:Implicit.HideAnimations" Value="{StaticResource ShowTransitions}" />
        </Style>


        <animations:ImplicitAnimationSet x:Name="ShowTransitions">
            <animations:OffsetAnimation EasingMode="EaseOut"
                                        From="0,24,0"
                                        To="0"
                                        Duration="0:0:0.4" />
            <animations:OpacityAnimation EasingMode="EaseOut"
                                         From="0"
                                         To="1"
                                         Duration="0:0:0.2" />
        </animations:ImplicitAnimationSet>
        <animations:ImplicitAnimationSet x:Name="HideTransitions">
            <animations:OffsetAnimation EasingMode="EaseOut"
                                        From="0"
                                        To="0,24,0"
                                        Duration="0:0:0.2" />
            <animations:OpacityAnimation EasingMode="EaseOut"
                                         From="1"
                                         To="0"
                                         Duration="0:0:0.1" />
        </animations:ImplicitAnimationSet>

    </Page.Resources>


    <!--
    Never use a ScrollViewer in a page that's gonna have a DataGrid
    It would:
    1. Defeat the virtualization feature of the DataGrid.
    2. Make data injection to the DataGrid super slow.
    3. Make the UI very slow all the time.
    4. Prevent the horizontal scrollbar from working and appearing properly.
    -->


    <Grid>
        <Grid.RowDefinitions>
            <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="6,10,6,10">

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

<Span>
    Leverage <Run Foreground="{ThemeResource SystemAccentColor}">the local system logs or EVTX log files</Run>
     to create <Italic>Application Control Policies.</Italic>
    Only Code Integrity Operational and AppLocker logs are required. Use the
    <Underline><Bold>Scan button</Bold></Underline> to populate the data grid.
    Once populated, use the <Underline><Bold>Create Policy button</Bold></Underline>
    to define output configuration and generate the XML policy file.
</Span>
            </TextBlock>

            <HyperlinkButton Content="Guide" NavigateUri="https://github.com/HotCakeX/Harden-Windows-Security/wiki/Create-Policy-From-Event-Logs" />

        </controls:WrapPanel>

        <Border
    Grid.Row="1"
    Margin="0,10,0,10"
    Style="{StaticResource GridCardStyle}" Padding="8">

            <controls:WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalSpacing="10" VerticalSpacing="10">

                <ProgressRing x:Name="ScanLogsProgressRing" Visibility="Collapsed" IsActive="False" />

                <!-- Scan button -->
                <Button x:Name="ScanLogs" ToolTipService.ToolTip="Scan the system or the selected EVTX files for Code Integrity/AppLocker logs" Style="{StaticResource AccentButtonStyle}" Click="ScanLogs_Click" >
                    <Button.Content>
                        <StackPanel Orientation="Horizontal">
                            <FontIcon Glyph="&#xEE6F;" />
                            <TextBlock Text="Scan Logs" Margin="5,0,0,0" />

                        </StackPanel>
                    </Button.Content>
                </Button>

                <SplitButton x:Name="CreatePolicyButton" ToolTipService.ToolTip="Create the supplemental policy" Click="CreatePolicyButton_Click">

                    <SplitButton.Content>
                        <StackPanel Orientation="Horizontal">
                            <FontIcon Glyph="&#xECCD;" />
                            <TextBlock Text="Create Policy" Margin="5,0,0,0" />
                        </StackPanel>
                    </SplitButton.Content>

                    <SplitButton.Flyout>
                        <Flyout Placement="Bottom">

                            <Flyout.FlyoutPresenterStyle>
                                <Style TargetType="FlyoutPresenter">
                                    <Setter Property="Padding" Value="0" />
                                    <!-- Same corner radius as the one in PanelStyle  -->
                                    <Setter Property="CornerRadius" Value="8" />

                                    <!--
                                    Important: the "resolution order" for widths (at least in winui) is minwidth, maxwidth, width
                                    Width does not override MaxWidth
                                    https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.frameworkelement.maxwidth?view=winrt-26100#remarks
                                    -->
                                    <Setter Property="MaxWidth" Value="1234" />
                                </Style>
                            </Flyout.FlyoutPresenterStyle>


                            <StackPanel
  VerticalAlignment="Top"
  Orientation="Vertical"
  Spacing="8">
                                <controls:Segmented x:Name="segmentedControl"
              HorizontalAlignment="Stretch"
              SelectedIndex="0">
                                    <controls:SegmentedItem Content="Add To Policy"
                      Tag="AddToPolicy" Width="160" Icon="{ui:FontIcon Glyph=&#xEB49;}" />
                                    <controls:SegmentedItem Content="Base Policy File"
                      Tag="BasePolicyFile" Width="160" Icon="{ui:FontIcon Glyph=&#xEB49;}"/>
                                    <controls:SegmentedItem Content="Base GUID"
                      Tag="BaseGUID" Width="130" Icon="{ui:FontIcon Glyph=&#xEB49;}"/>
                                </controls:Segmented>
                                <controls:SwitchPresenter Value="{Binding SelectedItem.Tag, ElementName=segmentedControl}" HorizontalAlignment="Center">

                                    <controls:Case Value="AddToPolicy">
                                        <StackPanel animations:Implicit.HideAnimations="{StaticResource HideTransitions}"
              animations:Implicit.ShowAnimations="{StaticResource ShowTransitions}"
              Style="{StaticResource PanelStyle}">

                                            <TextBlock Margin="5" VerticalAlignment="Center" TextWrapping="WrapWholeWords">

                                       <Span>Browse for a XML policy file to add <Bold>the logs</Bold> to </Span>

                                            </TextBlock>

                                            <Button Margin="5" x:Name="AddToPolicyButton" Content="Browse" ToolTipService.ToolTip="Select a XML file to add the scanned logs to" Style="{StaticResource AccentButtonStyle}" Click="AddToPolicyButton_Click" />

                                        </StackPanel>
                                    </controls:Case>
                                    <controls:Case Value="BasePolicyFile">
                                        <StackPanel animations:Implicit.HideAnimations="{StaticResource HideTransitions}"
              animations:Implicit.ShowAnimations="{StaticResource ShowTransitions}"
              Style="{StaticResource PanelStyle}">


                                            <TextBlock Margin="5" VerticalAlignment="Center" TextWrapping="WrapWholeWords" Width="290" >

                                                 <Span>Browse for a <Bold>base</Bold>
       policy XML file that this <Underline>Supplemental</Underline> policy will belong to </Span>

                                            </TextBlock>

                                            <Button Margin="5" x:Name="BasePolicyFileButton" Content="Browse" ToolTipService.ToolTip="Select a base policy that this supplemental policy will belong to." Style="{StaticResource AccentButtonStyle}" Click="BasePolicyFileButton_Click" />


                                        </StackPanel>
                                    </controls:Case>

                                    <controls:Case Value="BaseGUID">
                                        <StackPanel animations:Implicit.HideAnimations="{StaticResource HideTransitions}"
              animations:Implicit.ShowAnimations="{StaticResource ShowTransitions}"
              Style="{StaticResource PanelStyle}">

                                            <TextBox x:Name="BaseGUIDTextBox" PlaceholderText="Enter a Base policy GUID" Width="300" Margin="10" VerticalAlignment="Center" VerticalContentAlignment="Center" ToolTipService.ToolTip="Enter the Base GUID" />

                                            <Button Margin="5" x:Name="BaseGUIDSubmitButton" Content="Submit" ToolTipService.ToolTip="Submit the GUID" Style="{StaticResource AccentButtonStyle}" Click="BaseGUIDSubmitButton_Click" />

                                        </StackPanel>
                                    </controls:Case>
                                </controls:SwitchPresenter>
                            </StackPanel>

                        </Flyout>
                    </SplitButton.Flyout>

                </SplitButton>


                <DropDownButton Content="Browse for EVTX" ToolTipService.ToolTip="Select EVTX files">

                    <DropDownButton.Flyout>

                        <MenuFlyout Placement="Bottom">

                            <MenuFlyoutItem Text="Select Code Integrity EVTX Files" Click="SelectCodeIntegrityEVTXFiles_Click" ToolTipService.ToolTip="Browse for Code Integrity Operational EVTX logs">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon Glyph="&#xEC50;" />
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutItem Text="Select AppLocker EVTX Files" Click="SelectAppLockerEVTXFiles_Click" ToolTipService.ToolTip="Browse for AppLocker EVTX logs">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon Glyph="&#xEC50;" />
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                        </MenuFlyout>

                    </DropDownButton.Flyout>
                </DropDownButton>


                <DropDownButton Content="Actions" ToolTipService.ToolTip="Multiple actions to take on the logs">
                    <DropDownButton.Flyout>

                        <MenuFlyout Placement="Bottom">

                            <MenuFlyoutItem Text="Select All" Click="SelectAll_Click">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon Glyph="&#xE762;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutItem Text="De-Select All" Click="DeSelectAll_Click">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon Glyph="&#xE8E6;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutItem Text="Clear Data" Click="ClearDataButton_Click" ToolTipService.ToolTip="Clears all of the displayed data">
                                <MenuFlyoutItem.Icon>
                                    <SymbolIcon Symbol="Delete" />
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutSeparator/>

                            <ToggleMenuFlyoutItem x:Name="DeployPolicyToggle" Text="Deploy Policy After Creation" IsChecked="False">
                                <ToggleMenuFlyoutItem.Icon>
                                    <FontIcon Glyph="&#xE896;"/>
                                </ToggleMenuFlyoutItem.Icon>


                            </ToggleMenuFlyoutItem>


                        </MenuFlyout>

                    </DropDownButton.Flyout>
                </DropDownButton>


                <TextBox PlaceholderText="Total logs: 0"
                 x:Name="TotalCountOfTheFilesTextBox"
                 IsReadOnly="True"
                 VerticalAlignment="Center"
                 VerticalContentAlignment="Center" ToolTipService.ToolTip="The total number of the logs"/>

                <TextBox x:Name="SearchBox" Width="300" PlaceholderText="Search data..." TextChanged="SearchBox_TextChanged" VerticalAlignment="Center" VerticalContentAlignment="Center" ToolTipService.ToolTip="Search the data" />

                <Button Content="Policy Name">
                    <Button.Flyout>
                        <Flyout>
                            <Flyout.FlyoutPresenterStyle>
                                <Style TargetType="FlyoutPresenter">
                                    <Setter Property="CornerRadius" Value="8" />
                                    <Setter Property="MaxWidth" Value="1234" />
                                </Style>
                            </Flyout.FlyoutPresenterStyle>

                            <controls:WrapPanel Orientation="Vertical" HorizontalSpacing="3" VerticalSpacing="8">
                                <TextBox Width="300" Header="Optional: Choose a policy name" PlaceholderText="Policy Name..." x:Name="PolicyNameTextBox"/>
                            </controls:WrapPanel>
                        </Flyout>
                    </Button.Flyout>
                </Button>

                <CalendarDatePicker x:Name="FilterByDateCalendarPicker" PlaceholderText="Filter logs by date" ToolTipService.ToolTip="Will only show logs that are newer than the selected date" />

                <ComboBox SelectionChanged="ScanLevelComboBox_SelectionChanged" x:Name="ScanLevelComboBox" ToolTipService.ToolTip="Pick a level based on which the selected logs will be scanned" PlaceholderText="Scan level" >
                    <ComboBoxItem>FilePublisher</ComboBoxItem>
                    <ComboBoxItem>Publisher</ComboBoxItem>
                    <ComboBoxItem>Hash</ComboBoxItem>
                </ComboBox>

            </controls:WrapPanel>
        </Border>




        <!-- DataGrid for FileIdentity Outputs -->
        <Border Grid.Row="2" Style="{StaticResource GridCardStyle}" Margin="0,0,0,25" Padding="5">
            <tk7controls:DataGrid
        ItemsSource="{x:Bind FileIdentities, Mode=OneWay}"
        x:Name="FileIdentitiesDataGrid"
        AllowDrop="False"
        AreRowDetailsFrozen="True"
        AutoGenerateColumns="False"
        CanDrag="False"
        HeadersVisibility="Column"
        IsReadOnly="True"
        IsTabStop="True"
        RowDetailsVisibilityMode="Collapsed"
        SelectionMode="Extended"
        TabIndex="2"
        ScrollViewer.HorizontalScrollBarVisibility="Auto"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        ScrollViewer.VerticalScrollMode="Auto"
        ScrollViewer.HorizontalScrollMode="Auto"
        Sorting="FileIdentitiesDataGrid_Sorting"
        CanUserSortColumns="True"
        Loaded="FileIdentitiesDataGrid_Loaded">
                <tk7controls:DataGrid.ContextFlyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Text="Delete Row" Click="DataGridFlyoutMenuDelete_Click">
                            <MenuFlyoutItem.Icon>
                                <FontIcon Glyph="&#xE74D;" />
                            </MenuFlyoutItem.Icon>
                        </MenuFlyoutItem>

                        <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="File Name" Binding="{Binding FileName}" Width="Auto" Tag="FileName"/>
                    <tk7controls:DataGridTextColumn Header="Time Created" Binding="{Binding TimeCreated}" Width="Auto" Tag="TimeCreated"/>
                    <tk7controls:DataGridTextColumn Header="Signature Status" Binding="{Binding SignatureStatus}" Tag="SignatureStatus" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Action" Binding="{Binding Action}" Width="Auto" Tag="Action"/>
                    <tk7controls:DataGridTextColumn Header="Original File Name" Binding="{Binding OriginalFileName}" Width="Auto" Tag="OriginalFileName"/>
                    <tk7controls:DataGridTextColumn Header="InternalName" Binding="{Binding InternalName}" Width="Auto" Tag="InternalName"/>
                    <tk7controls:DataGridTextColumn Header="File Description" Binding="{Binding FileDescription}" Width="Auto" Tag="FileDescription"/>
                    <tk7controls:DataGridTextColumn Header="Product Name" Binding="{Binding ProductName}" Width="Auto" Tag="ProductName"/>
                    <tk7controls:DataGridTextColumn Header="File Version" Binding="{Binding FileVersion}" Width="Auto" Tag="FileVersion"/>
                    <tk7controls:DataGridTextColumn Header="Package Family Name" Binding="{Binding PackageFamilyName}" Width="Auto" Tag="PackageFamilyName"/>
                    <tk7controls:DataGridTextColumn Header="SHA256 Hash" Binding="{Binding SHA256Hash}" Width="Auto" Tag="SHA256Hash"/>
                    <tk7controls:DataGridTextColumn Header="SHA1 Hash" Binding="{Binding SHA1Hash}" Width="Auto" Tag="SHA1Hash"/>
                    <tk7controls:DataGridTextColumn Header="SHA256 Flat Hash" Binding="{Binding SHA256FlatHash}" Width="Auto" Tag="SHA256FlatHash"/>
                    <tk7controls:DataGridTextColumn Header="SHA1 Flat Hash" Binding="{Binding SHA1FlatHash}" Width="Auto" Tag="SHA1FlatHash"/>
                    <tk7controls:DataGridTextColumn Header="Signing Scenario" Binding="{Binding SISigningScenario}" Width="Auto" Tag="SISigningScenario"/>
                    <tk7controls:DataGridTextColumn Header="File Path" Binding="{Binding FilePath}" Width="Auto" Tag="FilePath"/>
                    <tk7controls:DataGridTextColumn Header="Computer Name" Binding="{Binding ComputerName}" Width="Auto" Tag="ComputerName"/>
                    <tk7controls:DataGridTextColumn Header="Policy GUID" Binding="{Binding PolicyGUID}" Width="Auto" Tag="PolicyGUID"/>
                    <tk7controls:DataGridTextColumn Header="Policy Name" Binding="{Binding PolicyName}" Width="Auto" Tag="PolicyName"/>
                    <tk7controls:DataGridTextColumn Header="File Publishers" Binding="{Binding FilePublishersToDisplay}" Width="Auto" Tag="FilePublishersToDisplay"/>

                </tk7controls:DataGrid.Columns>
            </tk7controls:DataGrid>
        </Border>

    </Grid>

</Page>
