<controls:PageBase
    x:Class="JumpPoint.Uwp.Views.AppLinksPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:JumpPoint.Uwp.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:nguwp="using:NittyGritty.Uwp"
    xmlns:controls="using:JumpPoint.Uwp.Controls"
    xmlns:winui="using:Microsoft.UI.Xaml.Controls"
    xmlns:extensions="using:JumpPoint.Platform.Extensions"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:icore="using:Microsoft.Xaml.Interactions.Core"
    xmlns:actions="using:NittyGritty.Uwp.Interactivity.Actions"
    mc:Ignorable="d">

    <controls:PageBase.Resources>
        <XamlUICommand x:Key="AppLinkProviderCommand"
                       Command="{x:Bind ViewModel.ProviderCommand}" />
    </controls:PageBase.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <controls:JumpPointViewer Grid.Row="1"
                                  Context="{x:Bind ViewModel, Mode=OneWay}"
                                  SortBy="{x:Bind ViewModel.Settings.SortBy, Mode=OneWay}"
                                  IsSortAscending="{x:Bind ViewModel.Settings.IsSortAscending, Mode=OneWay}"
                                  GroupBy="{x:Bind ViewModel.Settings.GroupBy, Mode=OneWay}"
                                  IsGroupAscending="{x:Bind ViewModel.Settings.IsGroupAscending, Mode=OneWay}"
                                  IsLoading="{x:Bind ViewModel.IsLoading, Mode=OneWay}"
                                  SelectedItems="{x:Bind ViewModel.SelectedItems, Mode=OneWay}"
                                  ItemsSource="{x:Bind ViewModel.Items, Mode=OneWay}">
            <controls:JumpPointViewer.Header>
                <StackPanel HorizontalAlignment="Stretch">
                    <TextBlock Text="App Link Providers"
                               Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}"
                               Margin="16,8"
                               FontSize="16"
                               FontWeight="SemiBold"/>
                    <winui:InfoBar Title="Nothing to show. You can enable them in Settings > Extensions"
                                   IsOpen="True"
                                   IsClosable="False"
                                   Margin="16,4"
                                   Visibility="{x:Bind ViewModel.Providers.Count, Mode=OneWay, Converter={StaticResource VisibleWhenZeroConverter}}"/>
                    <ScrollViewer HorizontalScrollMode="Enabled"
                                  HorizontalScrollBarVisibility="Auto"
                                  VerticalScrollMode="Disabled"
                                  VerticalScrollBarVisibility="Hidden"
                                  Visibility="{x:Bind ViewModel.Providers.Count, Mode=OneWay, Converter={StaticResource VisibleWhenMoreThanZeroConverter}}">
                        <winui:ItemsRepeater ItemsSource="{x:Bind ViewModel.Providers, Mode=OneWay}"
                                             Margin="4,4,4,16">
                            <winui:ItemsRepeater.Layout>
                                <winui:StackLayout Orientation="Horizontal"
                                                   Spacing="4" />
                            </winui:ItemsRepeater.Layout>
                            <DataTemplate x:DataType="extensions:AppLinkProvider">
                                <Button CommandParameter="{x:Bind Mode=OneWay}"
                                        Command="{StaticResource AppLinkProviderCommand}"
                                        Background="Transparent"
                                        BorderThickness="0"
                                        Padding="0"
                                        HorizontalAlignment="Stretch"
                                        HorizontalContentAlignment="Stretch"
                                        VerticalAlignment="Stretch"
                                        VerticalContentAlignment="Stretch">
                                    <Grid Width="120"
                                          Background="Transparent">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="56" />
                                            <RowDefinition Height="*" />
                                        </Grid.RowDefinitions>
                                        <Image Source="{x:Bind Logo, Converter={StaticResource AppLinkGridTilesLogoConverter}, Mode=OneWay}"
                                               Width="36"
                                               Height="36"
                                               HorizontalAlignment="Center"
                                               VerticalAlignment="Center"
                                               Margin="8"
                                               Loaded="OnItemLoaded"/>

                                        <TextBlock Text="{x:Bind Name, Mode=OneWay}"
                                                   FontSize="12"
                                                   HorizontalTextAlignment="Center"
                                                   TextTrimming="CharacterEllipsis"
                                                   TextWrapping="Wrap"
                                                   MaxLines="2"
                                                   VerticalAlignment="Top"
                                                   Margin="8,0,8,8"
                                                   Grid.Row="1" />
                                    </Grid>
                                </Button>
                            </DataTemplate>
                        </winui:ItemsRepeater>
                    </ScrollViewer>
                    <MenuFlyoutSeparator Padding="16,4" />
                </StackPanel>
            </controls:JumpPointViewer.Header>
            <controls:JumpPointViewer.QuickActions>
                <StackPanel Orientation="Horizontal"
                            Spacing="8"
                            Height="48"
                            Margin="0,8,0,0"
                            Padding="8"
                            CornerRadius="6"
                            Background="{ThemeResource ExpanderHeaderBackground}">
                    <controls:IconButton CommandParameter="{Binding Source={StaticResource Locator}, Path=TabbedShell.CurrentTab}"
                                         Command="{StaticResource NewAppLinkCommand}" />
                </StackPanel>
            </controls:JumpPointViewer.QuickActions>
        </controls:JumpPointViewer>
        <Grid Padding="12,8"
              Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
              x:Load="{x:Bind ViewModel.IsSearching, Mode=OneWay}"
              ColumnSpacing="8"
              x:Name="paneSearch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <TextBox Text="{x:Bind ViewModel.Search, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                     PlaceholderText="Search..."
                     HorizontalAlignment="Stretch">
                <i:Interaction.Behaviors>
                    <icore:EventTriggerBehavior EventName="Loaded">
                        <actions:FocusAction />
                    </icore:EventTriggerBehavior>
                </i:Interaction.Behaviors>
            </TextBox>
            <Button Grid.Column="1"
                    Padding="4"
                    BorderThickness="0"
                    Background="Transparent"
                    VerticalAlignment="Center"
                    Click="{x:Bind ViewModel.CloseSearch}">
                <IconSourceElement IconSource="{StaticResource CloseTabIcon}" />
            </Button>
        </Grid>
    </Grid>
</controls:PageBase>
