<ContentDialog
    x:Class="JumpPoint.Uwp.Dialogs.ToolPicker"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:JumpPoint.Uwp.Dialogs"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converters="using:JumpPoint.Uwp.Converters"
    xmlns:ngconv="using:NittyGritty.Uwp.Converters"
    xmlns:winui="using:Microsoft.UI.Xaml.Controls"
    xmlns:extensions="using:JumpPoint.Platform.Extensions"
    mc:Ignorable="d"
    Title="Tools"
    PrimaryButtonText="Pick"
    SecondaryButtonText="Cancel"
    DefaultButton="Primary"
    IsPrimaryButtonEnabled="{x:Bind ViewModel.IsValid, Mode=OneWay}">

    <ContentDialog.Resources>
        <Style TargetType="local:ToolPicker"
               BasedOn="{StaticResource DefaultContentDialogStyle}" />
        <CollectionViewSource x:Key="ToolsViewSource"
                              x:Name="ToolsViewSource"
                              IsSourceGrouped="True"
                              ItemsPath="Items"
                              Source="{x:Bind ViewModel.Tools, Mode=OneWay}" />
        <converters:NothingToShowConverter x:Name="NothingToShowConverter">
            <ngconv:Binding Value="{x:Bind ViewModel.IsLoading, Mode=OneWay}" />
            <ngconv:Binding Value="{x:Bind ViewModel.Tools.Count, Mode=OneWay}" />
        </converters:NothingToShowConverter>
    </ContentDialog.Resources>

    <GridView ItemsSource="{x:Bind ToolsViewSource.View, Mode=OneWay}"
              SelectedItem="{x:Bind ViewModel.Tool, Mode=TwoWay}"
              SelectionMode="Single"
              IsSynchronizedWithCurrentItem="False">
        <GridView.Header>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <winui:ProgressBar IsIndeterminate="{x:Bind ViewModel.IsLoading, Mode=OneWay}"
                                   Visibility="{x:Bind ViewModel.IsLoading, Mode=OneWay}" />
                <StackPanel Margin="0,12"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            Visibility="{x:Bind NothingToShowConverter.Output, Mode=OneWay}">
                    <Image HorizontalAlignment="Center"
                           Width="96"
                           Height="96">
                        <Image.Source>
                            <BitmapImage DecodePixelHeight="96"
                                         DecodePixelWidth="96"
                                         DecodePixelType="Logical"
                                         UriSource="/Assets/Icons/Empty.png" />
                        </Image.Source>
                    </Image>
                    <TextBlock Text="Nothing to show"
                               Style="{StaticResource SubtitleTextBlockStyle}"
                               HorizontalAlignment="Center" />
                    <TextBlock Text="You can enable them in Settings > Extensions"
                               Style="{StaticResource CaptionTextBlockStyle}"
                               TextWrapping="WrapWholeWords"
                               HorizontalAlignment="Center" />
                </StackPanel>
            </Grid>
        </GridView.Header>
        <GridView.GroupStyle>
            <GroupStyle HidesIfEmpty="True">
                <GroupStyle.HeaderContainerStyle>
                    <Style TargetType="GridViewHeaderItem">
                        <Setter Property="FontSize"
                                Value="16" />
                        <Setter Property="FontWeight"
                                Value="SemiBold" />
                        <Setter Property="Foreground"
                                Value="{ThemeResource SystemControlPageTextBaseMediumBrush}" />
                        <Setter Property="Margin"
                                Value="0,1,0,0" />
                        <Setter Property="Padding"
                                Value="0,8" />
                        <Setter Property="HorizontalAlignment"
                                Value="Stretch" />
                        <Setter Property="HorizontalContentAlignment"
                                Value="Stretch" />
                        <Setter Property="VerticalContentAlignment"
                                Value="Stretch" />
                        <Setter Property="MinHeight"
                                Value="0" />
                        <Setter Property="UseSystemFocusVisuals"
                                Value="{StaticResource UseSystemFocusVisuals}" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="GridViewHeaderItem">
                                    <ContentPresenter x:Name="ContentPresenter"
                                                      Background="{TemplateBinding Background}"
                                                      BorderBrush="{TemplateBinding BorderBrush}"
                                                      BorderThickness="{TemplateBinding BorderThickness}"
                                                      CornerRadius="{TemplateBinding CornerRadius}"
                                                      Margin="{TemplateBinding Margin}"
                                                      Padding="{TemplateBinding Padding}"
                                                      Content="{TemplateBinding Content}"
                                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                                      ContentTransitions="{TemplateBinding ContentTransitions}"
                                                      HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.HeaderContainerStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"
                                    Spacing="8">
                            <TextBlock Text="{Binding Key}" />
                            <TextBlock Text="{Binding Items.Count, Converter={StaticResource StringFormatConverter}, ConverterParameter='({0})'}" />
                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </GridView.GroupStyle>
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="extensions:Tool">
                <Grid Width="120"
                      ToolTipService.ToolTip="{x:Bind Description, Mode=OneWay}"
                      Background="Transparent">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Image Source="{x:Bind Logo, Converter={StaticResource AppLinkGridTilesLogoConverter}, Mode=OneWay}"
                           Width="48"
                           Height="48"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           Margin="8" />

                    <TextBlock Text="{x:Bind Name, Mode=OneWay}"
                               FontSize="14"
                               HorizontalTextAlignment="Center"
                               TextTrimming="CharacterEllipsis"
                               TextWrapping="Wrap"
                               MaxLines="2"
                               VerticalAlignment="Top"
                               Margin="8,0,8,8"
                               Grid.Row="1" />
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
</ContentDialog>
