<ContentDialog
    x:Class="JumpPoint.Uwp.Dialogs.NewAppLinkDialog"
    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:winui="using:Microsoft.UI.Xaml.Controls"
    xmlns:launcher="using:NittyGritty.Platform.Launcher"
               xmlns:items="using:JumpPoint.Platform.Items"
               mc:Ignorable="d"
    Title="New App Link"
    PrimaryButtonText="Create"
    SecondaryButtonText="Cancel"
    DefaultButton="Primary"
    IsPrimaryButtonEnabled="{x:Bind ViewModel.IsValid, Mode=OneWay}">

    <ContentDialog.Resources>
        <Style TargetType="local:NewAppLinkDialog"
               BasedOn="{StaticResource DefaultContentDialogStyle}" />
        <XamlUICommand x:Key="RemoveQueryKeyCommand"
                       Command="{x:Bind ViewModel.RemoveQueryKeyCommand}" />
        <XamlUICommand x:Key="RemoveInputKeyCommand"
                       Command="{x:Bind ViewModel.RemoveInputKeyCommand}" />
    </ContentDialog.Resources>

    <ScrollViewer>
        <StackPanel Spacing="4"
                    Margin="0,0,20,0">
            <TextBox Header="Display Name:"
                     Text="{x:Bind ViewModel.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <TextBox Header="Path:"
                     Text="{x:Bind ViewModel.Link, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <TextBox Header="Description:"
                     Text="{x:Bind ViewModel.Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <Button Content="Find App Handlers"
                    Command="{x:Bind ViewModel.FindAppsCommand}"/>
            <ComboBox HorizontalAlignment="Stretch"
                      ItemsSource="{x:Bind ViewModel.AppHandlers, Mode=OneWay}"
                      SelectedItem="{Binding AppHandler, Mode=TwoWay}"
                      Visibility="{x:Bind ViewModel.AppHandlers.Count, Mode=OneWay, Converter={StaticResource VisibleWhenMoreThanZeroConverter}}"
                      Header="App Handlers:">
                <ComboBox.ItemTemplate>
                    <DataTemplate x:DataType="launcher:NGAppInfo">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Border Background="{StaticResource SystemAccentBrush}">
                                <Image Source="{x:Bind Logo, Converter={StaticResource AppLinkGridTilesLogoConverter}, Mode=OneWay}"
                                       Width="48"
                                       Height="48" />
                            </Border>
                            <StackPanel Grid.Column="1">
                                <TextBlock Text="{x:Bind DisplayName, Mode=OneWay}"
                                           FontSize="16" />
                                <TextBlock Text="{x:Bind PackageFamilyName, Mode=OneWay}"
                                           Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}"
                                           FontSize="16" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <StackPanel Visibility="{x:Bind ViewModel.AppHandler, Converter={StaticResource CollapsedWhenNullConverter}, Mode=OneWay}">
                <TextBlock Text="SUPPORTED LAUNCH TYPES:"
                           FontWeight="SemiBold"
                           Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}" />
                <StackPanel Orientation="Horizontal"
                            Spacing="8"
                            Margin="0,0,0,12">
                    <Grid>
                        <FontIcon FontFamily="Segoe MDL2 Assets"
                                  Glyph="&#xE002;"
                                  Foreground="{StaticResource SystemAccentBrush}" />
                        <FontIcon FontFamily="Segoe MDL2 Assets"
                                  Glyph="&#xE001;"
                                  Foreground="White"
                                  Visibility="{x:Bind converters:AppLinkUtilityConverter.IsLaunchTypeVisible(ViewModel.LaunchTypes, items:AppLinkLaunchTypes.Uri), Mode=OneWay}" />
                    </Grid>
                    <TextBlock Text="Uri"
                               VerticalAlignment="Center"
                               Margin="0,0,8,0"
                               FontSize="16" />

                    <Grid>
                        <FontIcon FontFamily="Segoe MDL2 Assets"
                                  Glyph="&#xE002;"
                                  Foreground="{StaticResource SystemAccentBrush}" />
                        <FontIcon FontFamily="Segoe MDL2 Assets"
                                  Glyph="&#xE001;"
                                  Foreground="White"
                                  Visibility="{x:Bind converters:AppLinkUtilityConverter.IsLaunchTypeVisible(ViewModel.LaunchTypes, items:AppLinkLaunchTypes.UriForResults), Mode=OneWay}" />
                    </Grid>
                    <TextBlock Text="Uri for Results"
                               VerticalAlignment="Center"
                               FontSize="16" />
                </StackPanel>

                <TextBlock Text="QUERY KEYS:"
                           FontWeight="SemiBold"
                           Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}" />
                <Button Command="{x:Bind ViewModel.AddQueryKeyCommand}">
                    <StackPanel Orientation="Horizontal"
                                Spacing="4">
                        <FontIcon FontFamily="Segoe MDL2 Assets"
                                  Glyph="&#xE109;"
                                  FontSize="14" />
                        <TextBlock Text="Add Query Key" />
                    </StackPanel>
                </Button>
                <winui:ItemsRepeater Margin="0,0,0,12"
                                     ItemsSource="{x:Bind ViewModel.QueryKeys, Mode=OneWay}">
                    <winui:ItemsRepeater.ItemTemplate>
                        <DataTemplate x:DataType="items:ValueInfo">
                            <Grid ColumnSpacing="8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBox VerticalAlignment="Center"
                                         Text="{x:Bind Key, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                         PlaceholderText="Required" />
                                <AppBarButton Grid.Column="1"
                                              Icon="Delete"
                                              Width="40"
                                              Height="40"
                                              CommandParameter="{x:Bind Mode=OneWay}"
                                              Command="{Binding Source={StaticResource RemoveQueryKeyCommand}}" />
                            </Grid>
                        </DataTemplate>
                    </winui:ItemsRepeater.ItemTemplate>
                </winui:ItemsRepeater>

                <TextBlock Text="INPUT KEYS:"
                           FontWeight="SemiBold"
                           Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}" />
                <Button Command="{x:Bind ViewModel.AddInputKeyCommand}">
                    <StackPanel Orientation="Horizontal"
                                Spacing="4">
                        <FontIcon FontFamily="Segoe MDL2 Assets"
                                  Glyph="&#xE109;"
                                  FontSize="14" />
                        <TextBlock Text="Add Input Key" />
                    </StackPanel>
                </Button>
                <winui:ItemsRepeater Margin="0,0,0,12"
                                     ItemsSource="{x:Bind ViewModel.InputKeys, Mode=OneWay}">
                    <winui:ItemsRepeater.ItemTemplate>
                        <DataTemplate x:DataType="items:ValueInfo">
                            <Grid ColumnSpacing="8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBox VerticalAlignment="Center"
                                         Text="{x:Bind Key, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                         PlaceholderText="Required" />
                                <AppBarButton Grid.Column="1"
                                              Icon="Delete"
                                              Width="40"
                                              Height="40"
                                              CommandParameter="{x:Bind Mode=OneWay}"
                                              Command="{Binding Source={StaticResource RemoveInputKeyCommand}}" />
                            </Grid>
                        </DataTemplate>
                    </winui:ItemsRepeater.ItemTemplate>
                </winui:ItemsRepeater>
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
</ContentDialog>
