<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="using:SourceGit.ViewModels"
             xmlns:v="using:SourceGit.Views"
             xmlns:c="using:SourceGit.Converters"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="SourceGit.Views.LauncherPagesCommandPalette"
             x:DataType="vm:LauncherPagesCommandPalette">
  <Grid RowDefinitions="Auto,Auto,Auto,Auto,Auto">
    <TextBox Grid.Row="0"
             x:Name="FilterTextBox"
             Height="24"
             Margin="4,8,4,0"
             BorderThickness="1"
             CornerRadius="12"
             Text="{Binding SearchFilter, Mode=TwoWay}"
             BorderBrush="{DynamicResource Brush.Border2}"
             VerticalContentAlignment="Center"
             v:AutoFocusBehaviour.IsEnabled="True">
      <TextBox.InnerLeftContent>
        <Path Width="14" Height="14"
              Margin="6,0,0,0"
              Fill="{DynamicResource Brush.FG2}"
              Data="{StaticResource Icons.Search}"/>
      </TextBox.InnerLeftContent>

      <TextBox.InnerRightContent>
        <Button Classes="icon_button"
                Width="16"
                Margin="0,0,6,0"
                Command="{Binding ClearFilter}"
                IsVisible="{Binding SearchFilter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
                HorizontalAlignment="Right">
          <Path Width="14" Height="14"
                Margin="0,1,0,0"
                Fill="{DynamicResource Brush.FG1}"
                Data="{StaticResource Icons.Clear}"/>
        </Button>
      </TextBox.InnerRightContent>
    </TextBox>

    <TextBlock Grid.Row="1"
               Margin="6,8,0,4"
               Text="{DynamicResource Text.Launcher.Pages}"
               FontWeight="Bold"
               Foreground="{DynamicResource Brush.FG2}"
               IsVisible="{Binding VisiblePages, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>

    <ListBox Grid.Row="2"
             x:Name="PageListBox"
             MaxHeight="250"
             Margin="4,0"
             BorderThickness="0"
             SelectionMode="Single"
             Background="Transparent"
             Focusable="True"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ScrollViewer.VerticalScrollBarVisibility="Auto"
             ItemsSource="{Binding VisiblePages, Mode=OneWay}"
             SelectedItem="{Binding SelectedPage, Mode=TwoWay}"
             IsVisible="{Binding VisiblePages, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
      <ListBox.Styles>
        <Style Selector="ListBoxItem">
          <Setter Property="Padding" Value="8,0"/>
          <Setter Property="MinHeight" Value="26"/>
          <Setter Property="CornerRadius" Value="4"/>
        </Style>

        <Style Selector="ListBox">
          <Setter Property="FocusAdorner">
            <FocusAdornerTemplate>
              <Grid/>
            </FocusAdornerTemplate>
          </Setter>
        </Style>
      </ListBox.Styles>

      <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
          <VirtualizingStackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
      </ListBox.ItemsPanel>

      <ListBox.ItemTemplate>
        <DataTemplate DataType="vm:LauncherPage">
          <Grid ColumnDefinitions="Auto,6,*" Background="Transparent" Tapped="OnItemTapped">
            <Path Grid.Column="0"
                  Width="12" Height="12"
                  Fill="{Binding Node.Bookmark, Converter={x:Static c:IntConverters.ToBookmarkBrush}}"
                  Data="{StaticResource Icons.Bookmark}"
                  IsVisible="{Binding Node.IsRepository}"
                  IsHitTestVisible="False"/>
            <Path Grid.Column="0"
                  Width="12" Height="12"
                  Fill="{DynamicResource Brush.FG1}"
                  Data="{StaticResource Icons.Repositories}"
                  IsVisible="{Binding !Node.IsRepository}"
                  IsHitTestVisible="False"/>
            <TextBlock Grid.Column="2"
                       VerticalAlignment="Center"
                       Text="{Binding Node.Name}"
                       IsVisible="{Binding Node.IsRepository}"
                       IsHitTestVisible="False"/>
            <TextBlock Grid.Column="2"
                       VerticalAlignment="Center"
                       Text="{DynamicResource Text.PageTabBar.Welcome.Title}"
                       IsVisible="{Binding !Node.IsRepository}"
                       IsHitTestVisible="False"/>
          </Grid>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>

    <TextBlock Grid.Row="3"
               Margin="6,8,0,4"
               Text="{DynamicResource Text.Launcher.OpenRepository}"
               FontWeight="Bold"
               Foreground="{DynamicResource Brush.FG2}"
               IsVisible="{Binding VisibleRepos, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>

    <ListBox Grid.Row="4"
             x:Name="RepoListBox"
             MaxHeight="300"
             Margin="4,0"
             BorderThickness="0"
             SelectionMode="Single"
             Background="Transparent"
             Focusable="True"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ScrollViewer.VerticalScrollBarVisibility="Auto"
             ItemsSource="{Binding VisibleRepos, Mode=OneWay}"
             SelectedItem="{Binding SelectedRepo, Mode=TwoWay}"
             IsVisible="{Binding VisibleRepos, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
      <ListBox.Styles>
        <Style Selector="ListBoxItem">
          <Setter Property="Padding" Value="8,0"/>
          <Setter Property="MinHeight" Value="26"/>
          <Setter Property="CornerRadius" Value="4"/>
        </Style>

        <Style Selector="ListBox">
          <Setter Property="FocusAdorner">
            <FocusAdornerTemplate>
              <Grid/>
            </FocusAdornerTemplate>
          </Setter>
        </Style>
      </ListBox.Styles>

      <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
          <VirtualizingStackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
      </ListBox.ItemsPanel>

      <ListBox.ItemTemplate>
        <DataTemplate DataType="vm:RepositoryNode">
          <Grid ColumnDefinitions="Auto,*" Background="Transparent" Tapped="OnItemTapped">
            <Path Grid.Column="0"
                  Width="12" Height="12"
                  Fill="{Binding Bookmark, Converter={x:Static c:IntConverters.ToBookmarkBrush}}"
                  Data="{StaticResource Icons.Bookmark}"
                  IsHitTestVisible="False"/>
            <TextBlock Grid.Column="1"
                       Margin="6,0,0,0"
                       VerticalAlignment="Center"
                       IsHitTestVisible="False">
              <Run Text="{Binding Name, Mode=OneWay}"/>
              <Run Text="{Binding Id, Mode=OneWay, StringFormat=({0}), Converter={x:Static c:PathConverters.RelativeToHome}}" Foreground="{DynamicResource Brush.FG2}"/>
            </TextBlock>
          </Grid>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </Grid>
</UserControl>
