<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="AppControlManager.Pages.Simulation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppControlManager.Pages"
    xmlns:localbase="using:AppControlManager"
    xmlns:AppControlManager="using:AppControlManager"
    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"
    mc:Ignorable="d">

    <Grid TabFocusNavigation="Cycle">
        <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>
    Perform <Italic>App Control for Business</Italic>
        <Run Foreground="{ThemeResource SystemAccentColor}">Simulation</Run>
    by selecting files or folders, and a XML policy file, then use the
    <Underline><Bold>Begin Simulation</Bold></Underline> button to populate the data grid.
     The results will show you whether the selected files would be allowed or blocked by your App Control policy if it was actually deployed on a system and those files were run.
</Span>
            </TextBlock>

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

        </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">
                <!-- Begin Simulation Button -->
                <Button x:Name="BeginSimulationButton" ToolTipService.ToolTip="Start the Simulation using the selected configurations" Style="{StaticResource AccentButtonStyle}" Click="BeginSimulationButton_Click">
                    <StackPanel Orientation="Horizontal">
                        <SymbolIcon Symbol="Play" />
                        <TextBlock Text="Begin Simulation" Margin="5,0,0,0"/>
                    </StackPanel>
                </Button>

                <!-- Select XML File Button and TextBox for File Path -->
                <Button Click="SelectXmlFileButton_Click" ToolTipService.ToolTip="Browse for the App Control policy XML file">
                    <StackPanel Orientation="Horizontal">
                        <FontIcon Glyph="&#xEC50;" />
                        <TextBlock Text="Select XML File" Margin="5,0,0,0"/>
                    </StackPanel>
                </Button>
                <TextBox x:Name="XmlFilePathTextBox" IsReadOnly="True" PlaceholderText="XML File Path..." VerticalAlignment="Center" MaxWidth="300" />

                <!-- Select Files Button -->
                <Button Click="SelectFilesButton_Click" ToolTipService.ToolTip="Browse for files to include in the simulation">
                    <StackPanel Orientation="Horizontal">
                        <FontIcon Glyph="&#xEC50;" />
                        <TextBlock Text="Select Files" Margin="5,0,0,0"/>
                    </StackPanel>
                </Button>

                <!-- Select Folders Button -->
                <Button Click="SelectFoldersButton_Click" ToolTipService.ToolTip="Browse for a folder to include in the simulation">
                    <StackPanel Orientation="Horizontal">
                        <FontIcon Glyph="&#xED25;" />
                        <TextBlock Text="Select Folders" Margin="5,0,0,0"/>
                    </StackPanel>
                </Button>




                <DropDownButton Content="Options" ToolTipService.ToolTip="Multiple Extra Options">
                    <DropDownButton.Flyout>

                        <MenuFlyout Placement="Bottom">

                            <!-- Cat Root Paths Button -->
                            <MenuFlyoutItem Text="Cat Root Paths" Click="CatRootPathsButton_Click" ToolTipService.ToolTip="Provide path(s) to directories where security catalog .cat files are located. If not provided, the default path is C:\Windows\System32\CatRoot">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon Glyph="&#xED25;"/>
                                </MenuFlyoutItem.Icon>
                            </MenuFlyoutItem>

                            <MenuFlyoutSeparator/>

                            <!-- Toggle Button for No Cat Root Scanning -->
                            <ToggleMenuFlyoutItem IsChecked="True" x:Name="NoCatRootScanningToggle" Text="No Cat Root Scanning" Click="ClearDataButton_Click"  ToolTipService.ToolTip="Will not scan the security catalogs on the system">
                                <MenuFlyoutItem.Icon>
                                    <FontIcon Glyph="&#xEB95;" />
                                </MenuFlyoutItem.Icon>
                            </ToggleMenuFlyoutItem>

                            <MenuFlyoutSeparator/>

                            <!-- Toggle Button for CSV Output -->
                            <ToggleMenuFlyoutItem x:Name="CSVOutputToggle" Text="Save output to CSV" IsChecked="False" ToolTipService.ToolTip="Exports the output to a CSV file in the AppControlManager directory in the Program Files directory at the end of the simulation job.">
                                <ToggleMenuFlyoutItem.Icon>
                                    <FontIcon Glyph="&#xE9F9;" />
                                </ToggleMenuFlyoutItem.Icon>

                            </ToggleMenuFlyoutItem>

                            <MenuFlyoutSeparator/>

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

                        </MenuFlyout>

                    </DropDownButton.Flyout>
                </DropDownButton>


                <!-- Scalability Button with RadialGauge Flyout -->
                <Button x:Name="ScalabilityButton" Content="Scalability" ToolTipService.ToolTip="The number of the concurrent/parallel tasks to use when performing App Control Simulation. By default it uses 2 parallel tasks.">
                    <Button.Flyout>
                        <Flyout>
                            <StackPanel>
                                <controls:RadialGauge x:Name="ScalabilityRadialGauge"
                                      StepSize="1"
                                      TickSpacing="2"
                                      ScaleWidth="12"
                                      NeedleWidth="5"
                                      NeedleLength="80"
                                      TickLength="8"
                                      TickWidth="0"
                                      ScalePadding="0"
                                      TickPadding="24"
                                      Width="200"
                                      Maximum="120"
                                      Minimum="2"
                                      ValueChanged="ScalabilityRadialGauge_ValueChanged"
                                      ValueStringFormat="N0"/>
                            </StackPanel>
                        </Flyout>
                    </Button.Flyout>
                </Button>

                <ProgressBar x:Name="SimulationProgressBar" Width="200" Value="0" ToolTipService.ToolTip="The Simulation progress" />

                <TextBox PlaceholderText="Total count of the files"
                         x:Name="TotalCountOfTheFilesTextBox"
                         IsReadOnly="True"
                         VerticalAlignment="Center"
                         VerticalContentAlignment="Center" ToolTipService.ToolTip="The total number of the files that participated in the last Simulation job"/>

                <TextBox x:Name="SearchBox" Width="300" PlaceholderText="Search data..." TextChanged="SearchBox_TextChanged" VerticalAlignment="Center" VerticalContentAlignment="Center" ToolTipService.ToolTip="Search the data, not all fields are included. To perform search in all fields such as hashes, export the results to CSV and then use VS Code." />

            </controls:WrapPanel>
        </Border>

        <!-- DataGrid for Simulation Outputs -->
        <Border Grid.Row="2" Style="{StaticResource GridCardStyle}" Margin="0,0,0,25" Padding="5">
            <tk7controls:DataGrid
                x:Name="SimulationDataGrid"
                ItemsSource="{x:Bind SimulationOutputs, Mode=OneWay}"
                AllowDrop="False"
                AreRowDetailsFrozen="True"
                AutoGenerateColumns="False"
                CanDrag="False"
                HeadersVisibility="Column"
                IsReadOnly="True"
                IsTabStop="True"
                RowDetailsVisibilityMode="Collapsed"
                SelectionMode="Single"
                TabIndex="2"
                ScrollViewer.HorizontalScrollBarVisibility="Auto"
                ScrollViewer.VerticalScrollBarVisibility="Auto"
                ScrollViewer.VerticalScrollMode="Auto"
                ScrollViewer.HorizontalScrollMode="Auto"
                Sorting="SimulationDataGrid_Sorting"
                CanUserSortColumns="True"
                Loaded="SimulationDataGrid_Loaded">

                <!-- Context Menu for Copy/Delete Options -->
                <tk7controls:DataGrid.ContextFlyout>
                    <MenuFlyout>

                        <MenuFlyoutItem Text="Copy Row" Click="SimulationDataGrid_CopyRow_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>

                <!-- DataGrid Columns -->
                <tk7controls:DataGrid.Columns>
                    <tk7controls:DataGridTextColumn Header="Path" Binding="{Binding Path}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Source" Binding="{Binding Source}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Is Authorized" Binding="{Binding IsAuthorized}" Tag="IsAuthorized" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Match Criteria" Binding="{Binding MatchCriteria}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Specific File Name Criteria" Binding="{Binding SpecificFileNameLevelMatchCriteria}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Signer ID" Binding="{Binding SignerID}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Signer Name" Binding="{Binding SignerName}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Signer Cert Root" Binding="{Binding SignerCertRoot}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Signer Cert Publisher" Binding="{Binding SignerCertPublisher}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Signer Scope" Binding="{Binding SignerScope}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Cert Subject CN" Binding="{Binding CertSubjectCN}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Cert Issuer CN" Binding="{Binding CertIssuerCN}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Cert Not After" Binding="{Binding CertNotAfter}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="Cert TBS Value" Binding="{Binding CertTBSValue}" Width="Auto"/>
                    <tk7controls:DataGridTextColumn Header="File Path" Binding="{Binding FilePath}" Width="Auto"/>
                </tk7controls:DataGrid.Columns>
            </tk7controls:DataGrid>
        </Border>
    </Grid>
</Page>
