<Page
    x:Class="PowerToys.FileLocksmithUI.Views.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converters="using:PowerToys.FileLocksmithUI.Converters"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:interop="using:FileLocksmith.Interop"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls"
    xmlns:tkconverters="using:CommunityToolkit.WinUI.Converters"
    xmlns:ui="using:CommunityToolkit.WinUI"
    mc:Ignorable="d">
    <Page.Resources>
        <tkconverters:BoolToVisibilityConverter
            x:Key="boolToVisibilityConverter"
            FalseValue="Visible"
            TrueValue="Collapsed" />

        <tkconverters:DoubleToVisibilityConverter
            x:Key="doubleToVisibilityConverter"
            FalseValue="Visible"
            GreaterThan="0"
            TrueValue="Collapsed" />
        <converters:FileCountConverter x:Key="fileCountConverter" />
        <converters:PidToIconConverter x:Key="pidToIconConverter" />
        <converters:UserToSystemWarningVisibilityConverter x:Key="userToSystemWarningVisibilityConverter" />
        <converters:FileListToDescriptionConverter x:Key="fileListToDescriptionConverter" />
    </Page.Resources>

    <interactivity:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="Loaded">
            <core:InvokeCommandAction Command="{x:Bind ViewModel.LoadProcessesCommand}" />
        </core:EventTriggerBehavior>
    </interactivity:Interaction.Behaviors>

    <Grid RowSpacing="8">
        <Grid.RowDefinitions>
            <RowDefinition x:Name="ButtonsRow" Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Margin="16,0">
            <Button Click="ShowSelectedPathsButton_Click" Content="{x:Bind ViewModel.Paths, Converter={StaticResource fileListToDescriptionConverter}}">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <TextBlock
                            HorizontalAlignment="Left"
                            Style="{ThemeResource BodyStrongTextBlockStyle}"
                            Text="{TemplateBinding Content}" />
                    </ControlTemplate>
                </Button.Template>
                <ToolTipService.ToolTip>
                    <TextBlock x:Uid="PathsTooltipDescription" TextWrapping="WrapWholeWords" />
                </ToolTipService.ToolTip>
            </Button>

            <StackPanel
                HorizontalAlignment="Right"
                Orientation="Horizontal"
                Spacing="8">
                <Button
                    Command="{Binding LoadProcessesCommand}"
                    Content="{ui:FontIcon Glyph=&#xe72c;,
                                          FontSize=16}"
                    Style="{StaticResource SubtleButtonStyle}">
                    <ToolTipService.ToolTip>
                        <TextBlock x:Uid="Reload" />
                    </ToolTipService.ToolTip>
                </Button>
                <Button
                    x:Name="RestartAsAdminBtn"
                    Command="{Binding RestartElevatedCommand}"
                    Content="{ui:FontIcon Glyph=&#xe7ef;,
                                          FontSize=16}"
                    Style="{StaticResource SubtleButtonStyle}"
                    Visibility="{x:Bind ViewModel.IsElevated, Converter={StaticResource boolToVisibilityConverter}}">
                    <ToolTipService.ToolTip>
                        <TextBlock x:Uid="RestartAsAdmin" />
                    </ToolTipService.ToolTip>
                </Button>
            </StackPanel>
        </Grid>

        <Grid Grid.Row="1">
            <Grid Visibility="{x:Bind ViewModel.IsLoading, Converter={StaticResource boolToVisibilityConverter}, Mode=OneWay}">
                <ListView
                    x:Name="ProcessesListView"
                    Padding="0,0,0,12"
                    IncrementalLoadingThreshold="10"
                    ItemsSource="{x:Bind ViewModel.Processes}"
                    SelectionMode="None">
                    <ListView.ItemTemplate>
                        <DataTemplate x:DataType="interop:ProcessResult">
                            <tkcontrols:SettingsExpander Margin="0,0,0,4">
                                <tkcontrols:SettingsExpander.Resources>
                                    <x:Double x:Key="SettingsCardWrapThreshold">0</x:Double>
                                </tkcontrols:SettingsExpander.Resources>
                                <tkcontrols:SettingsExpander.Header>
                                    <!--  We can't use the HeaderIcon because it only support a BitmapIcon, which only supports UriSource - not a direct BitmapImage  -->
                                    <StackPanel Orientation="Horizontal" Spacing="8">
                                        <Image
                                            Width="16"
                                            Height="16"
                                            Source="{x:Bind pid, Converter={StaticResource pidToIconConverter}}" />
                                        <TextBlock IsTextSelectionEnabled="True" Text="{x:Bind name}" />
                                    </StackPanel>
                                </tkcontrols:SettingsExpander.Header>
                                <tkcontrols:SettingsExpander.Content>
                                    <StackPanel Orientation="Horizontal" Spacing="8">
                                        <FontIcon
                                            Foreground="{ThemeResource InfoBarWarningSeverityIconBackground}"
                                            Glyph="&#xE7BA;"
                                            Visibility="{x:Bind user, Mode=OneTime, Converter={StaticResource userToSystemWarningVisibilityConverter}}">
                                            <ToolTipService.ToolTip>
                                                <TextBlock x:Uid="ProcessIsSystemUserWarning" TextWrapping="Wrap" />
                                            </ToolTipService.ToolTip>
                                        </FontIcon>
                                        <Button
                                            MinWidth="128"
                                            Command="{Binding Path=DataContext.EndTaskCommand, ElementName=ProcessesListView}"
                                            CommandParameter="{Binding}">
                                            <StackPanel Orientation="Horizontal" Spacing="8">
                                                <FontIcon FontSize="16" Glyph="&#xf140;" />
                                                <TextBlock x:Uid="EndTask" />
                                            </StackPanel>
                                        </Button>
                                    </StackPanel>
                                </tkcontrols:SettingsExpander.Content>
                                <tkcontrols:SettingsExpander.Items>
                                    <tkcontrols:SettingsCard x:Uid="ProcessID">
                                        <TextBlock
                                            Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                                            IsTextSelectionEnabled="True"
                                            Text="{x:Bind pid}" />
                                    </tkcontrols:SettingsCard>
                                    <tkcontrols:SettingsCard x:Uid="User">
                                        <TextBlock
                                            Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                                            IsTextSelectionEnabled="True"
                                            Text="{x:Bind user}" />
                                    </tkcontrols:SettingsCard>
                                    <tkcontrols:SettingsCard ContentAlignment="Vertical">
                                        <tkcontrols:SettingsCard.Header>
                                            <TextBlock>
                                                <Run x:Uid="Files" />
                                                <Run Text="(" /><Run Text="{x:Bind files, Converter={StaticResource fileCountConverter}}" /><Run Text=")" />
                                            </TextBlock>
                                        </tkcontrols:SettingsCard.Header>
                                        <ItemsRepeater ItemsSource="{x:Bind files}">
                                            <ItemsRepeater.ItemTemplate>
                                                <DataTemplate x:DataType="x:String">
                                                    <TextBlock
                                                        Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                                                        IsTextSelectionEnabled="True"
                                                        Style="{StaticResource CaptionTextBlockStyle}"
                                                        Text="{Binding}"
                                                        TextTrimming="CharacterEllipsis"
                                                        TextWrapping="NoWrap"
                                                        ToolTipService.ToolTip="{Binding}" />
                                                </DataTemplate>
                                            </ItemsRepeater.ItemTemplate>
                                        </ItemsRepeater>
                                    </tkcontrols:SettingsCard>
                                </tkcontrols:SettingsExpander.Items>
                            </tkcontrols:SettingsExpander>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

                <StackPanel
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Orientation="Vertical"
                    Spacing="8"
                    Visibility="{x:Bind ViewModel.Processes.Count, Mode=OneWay, Converter={StaticResource doubleToVisibilityConverter}}">
                    <Button HorizontalAlignment="Center" Command="{Binding LoadProcessesCommand}">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <FontIcon FontSize="32" Glyph="&#xE9F3;" />
                            </ControlTemplate>
                        </Button.Template>
                        <ToolTipService.ToolTip>
                            <TextBlock x:Uid="Reload" />
                        </ToolTipService.ToolTip>
                    </Button>
                    <TextBlock x:Uid="EmptyListDescription" Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
                </StackPanel>
            </Grid>
            <ProgressRing
                Width="48"
                Height="48"
                IsActive="{x:Bind ViewModel.IsLoading, Mode=OneWay}" />
        </Grid>

        <ContentDialog x:Name="SelectedFilesListDialog" x:Uid="SelectedFilesListDialog">
            <TextBlock
                IsTextSelectionEnabled="True"
                Text="{x:Bind ViewModel.PathsToString, Mode=OneWay}"
                TextWrapping="Wrap" />
        </ContentDialog>
    </Grid>
</Page>
