<ui:FluentWindow
    x:Class="PowerLauncher.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:PowerLauncher"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:p="clr-namespace:PowerLauncher.Properties"
    xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
    xmlns:vm="clr-namespace:PowerLauncher.ViewModel"
    Title="PowerToys Run"
    Width="640"
    MinHeight="0"
    d:DataContext="{d:DesignInstance vm:MainViewModel}"
    ui:ExtendsContentIntoTitleBar="True"
    AllowDrop="True"
    Closed="OnClosed"
    Closing="OnClosing"
    Deactivated="OnDeactivated"
    IsVisibleChanged="OnVisibilityChanged"
    Loaded="OnLoaded"
    LocationChanged="OnLocationChanged"
    ResizeMode="NoResize"
    ShowInTaskbar="False"
    SizeToContent="Height"
    SourceInitialized="OnSourceInitialized"
    Topmost="True"
    Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    WindowStartupLocation="Manual"
    WindowStyle="None"
    mc:Ignorable="d">

    <Grid x:Name="RootGrid" MouseDown="OnMouseDown">
        <!--  We set the background here because the Acrylic can be too translucent / background too bright on Light theme  -->
        <Grid.Background>
            <SolidColorBrush Opacity="0.8" Color="{DynamicResource ApplicationBackgroundColor}" />
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" MaxHeight="{Binding Results.MaxHeight}" />
        </Grid.RowDefinitions>
        <Border
            x:Name="SearchBoxBorder"
            Grid.Row="0"
            Padding="12,4,12,3">
            <local:LauncherControl x:Name="SearchBox" />
        </Border>

        <!--  Have to use a Grid instead of a StackPanel for scrolling to work?  -->
        <Grid
            x:Name="KeywordsOverviewGrid"
            Grid.Row="1"
            MaxHeight="256"
            Visibility="{Binding PluginsOverviewVisibility}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Rectangle
                Height="1"
                VerticalAlignment="Top"
                Fill="{DynamicResource DividerStrokeColorDefaultBrush}" />
            <TextBlock
                Margin="22,12,0,4"
                FontWeight="SemiBold"
                Foreground="{DynamicResource TextFillColorSecondaryBrush}"
                Style="{StaticResource CaptionTextBlockStyle}"
                Text="{x:Static p:Resources.PluginKeywords}" />

            <ListView
                x:Name="pluginsHintsList"
                Grid.Row="1"
                Margin="16,0,0,0"
                Padding="0,0,32,0"
                ItemContainerStyle="{StaticResource PluginsListViewItemStyle}"
                ItemsSource="{Binding Plugins}"
                PreviewMouseLeftButtonUp="PluginsHintsList_PreviewMouseLeftButtonUp"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                ScrollViewer.VerticalScrollBarVisibility="Auto"
                SelectedItem="{Binding SelectedPlugin, Mode=TwoWay}"
                SelectionMode="Single">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel
                            Margin="16,0"
                            IsVirtualizing="{TemplateBinding VirtualizingPanel.IsVirtualizing}"
                            VirtualizationMode="{TemplateBinding VirtualizingPanel.VirtualizationMode}" />
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Border
                                Width="32"
                                Height="32"
                                Padding="2,0,2,0"
                                Background="{DynamicResource ControlFillColorDefaultBrush}"
                                BorderBrush="{DynamicResource CardStrokeColorDefaultBrush}"
                                BorderThickness="1"
                                CornerRadius="4"
                                ToolTipService.ToolTip="{Binding Metadata.ActionKeyword}">
                                <TextBlock
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    FontWeight="SemiBold"
                                    Foreground="{DynamicResource TextFillColorPrimaryBrush}"
                                    Text="{Binding Metadata.ActionKeyword}"
                                    TextAlignment="Left"
                                    TextTrimming="WordEllipsis" />
                            </Border>
                            <TextBlock
                                Grid.Column="1"
                                Margin="12,0,0,0"
                                VerticalAlignment="Center"
                                Foreground="{DynamicResource TextFillColorPrimaryBrush}"
                                Text="{Binding Plugin.Description}"
                                TextTrimming="CharacterEllipsis"
                                TextWrapping="Wrap" />
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>

        <local:ResultList
            x:Name="ListBox"
            Grid.Row="2"
            VerticalAlignment="Stretch"
            BorderBrush="{DynamicResource DividerStrokeColorDefaultBrush}"
            BorderThickness="0,1,0,0"
            PreviewMouseDown="ListBox_PreviewMouseDown"
            Visibility="{Binding Results.Visibility}" />

    </Grid>
    <ui:FluentWindow.InputBindings>
        <KeyBinding Key="Escape" Command="{Binding EscCommand}" />
        <KeyBinding Key="Enter" Command="{Binding OpenResultWithKeyboardCommand}" />
        <KeyBinding
            Key="F4"
            Command="{Binding IgnoreCommand}"
            Modifiers="Alt" />
    </ui:FluentWindow.InputBindings>
</ui:FluentWindow>
