<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:m="using:SourceGit.Models"
             xmlns:v="using:SourceGit.Views"
             mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450"
             x:Class="SourceGit.Views.BranchSelector">
  <UserControl.Styles>
    <Style Selector="v|BranchSelector">
      <Setter Property="FocusAdorner">
        <FocusAdornerTemplate>
          <Border/>
        </FocusAdornerTemplate>
      </Setter>

      <Setter Property="Template">
        <ControlTemplate>
          <Grid Background="Transparent">
            <Border x:Name="PART_Background"
                    Background="{DynamicResource Brush.Contents}"
                    BorderThickness="1"
                    BorderBrush="{DynamicResource Brush.Border1}"
                    CornerRadius="3"/>

            <Grid x:Name="PART_Selected"
                  Background="Transparent"
                  ColumnDefinitions="Auto,*,32"
                  PointerPressed="OnToggleDropDown">
              <Path Grid.Column="0"
                    Margin="8,0,0,0"
                    Width="14" Height="14"
                    Data="{StaticResource Icons.Branch}"
                    IsHitTestVisible="False"/>

              <ContentControl Grid.Column="1"
                              Margin="8,0,0,0"
                              Content="{TemplateBinding SelectedBranch, Mode=OneWay}">
                <ContentControl.DataTemplates>
                  <DataTemplate DataType="m:Branch">
                    <TextBlock Text="{Binding FriendlyName, Mode=OneWay}" VerticalAlignment="Center"/>
                  </DataTemplate>
                </ContentControl.DataTemplates>
              </ContentControl>

              <Path Grid.Column="2"
                    Width="12" Height="12"
                    Margin="0,0,10,0"
                    HorizontalAlignment="Right" VerticalAlignment="Center"
                    Data="M0 0 M1939 486L2029 576L1024 1581L19 576L109 486L1024 1401L1939 486Z"
                    IsHitTestVisible="False"/>
            </Grid>

            <Popup x:Name="PART_Popup"
                   WindowManagerAddShadowHint="False"
                   IsOpen="{TemplateBinding IsDropDownOpened, Mode=TwoWay}"
                   Width="{Binding Bounds.Width, ElementName=PART_Background}"
                   MaxHeight="600"
                   PlacementTarget="PART_Background"
                   Placement="BottomEdgeAlignedLeft"
                   VerticalOffset="2"
                   IsLightDismissEnabled="True"
                   InheritsTransform="True">
              <Border Background="{DynamicResource Brush.Contents}"
                      BorderThickness="1"
                      BorderBrush="{DynamicResource Brush.Accent}"
                      CornerRadius="4"
                      Padding="4"
                      HorizontalAlignment="Stretch">
                <Grid RowDefinitions="36,Auto">
                  <TextBox Grid.Row="0"
                           x:Name="PART_TextFilter"
                           Height="24"
                           Margin="6,0"
                           BorderThickness="1"
                           CornerRadius="12"
                           Text="{TemplateBinding SearchFilter, Mode=TwoWay}"
                           BorderBrush="{DynamicResource Brush.Border2}"
                           VerticalContentAlignment="Center"
                           KeyDown="OnSearchBoxKeyDown">
                    <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"
                              Click="OnClearSearchFilter"
                              IsVisible="{Binding ElementName=PART_TextFilter, Path=Text, 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>

                  <ListBox Grid.Row="1"
                           Focusable="True"
                           Margin="0,4"
                           MaxHeight="360"
                           BorderThickness="0"
                           Background="Transparent"
                           ItemsSource="{TemplateBinding VisibleBranches, Mode=OneWay}"
                           SelectedItem="{TemplateBinding SelectedBranch, Mode=TwoWay}"
                           KeyDown="OnDropDownListKeyDown">
                    <ListBox.ItemsPanel>
                      <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Vertical"/>
                      </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>

                    <ListBox.Styles>
                      <Style Selector="ListBoxItem">
                        <Setter Property="Height" Value="28"/>
                        <Setter Property="CornerRadius" Value="3"/>
                      </Style>
                    </ListBox.Styles>

                    <ListBox.ItemTemplate>
                      <DataTemplate DataType="m:Branch">
                        <StackPanel Orientation="Horizontal" Background="Transparent" Height="28" PointerPressed="OnDropDownItemPointerPressed">
                          <Path Width="14" Height="14" Fill="{DynamicResource Brush.FG1}" Data="{StaticResource Icons.Branch}"/>
                          <TextBlock Margin="8,0,0,0" Text="{Binding FriendlyName, Mode=OneWay}" VerticalAlignment="Center"/>
                        </StackPanel>
                      </DataTemplate>
                    </ListBox.ItemTemplate>
                  </ListBox>
                </Grid>
              </Border>
            </Popup>
          </Grid>
        </ControlTemplate>
      </Setter>

      <Style Selector="^:pointerover /template/ Border#PART_Background">
        <Setter Property="BorderBrush" Value="{DynamicResource Brush.Accent}" />
      </Style>

      <Style Selector="^:focus-visible">
        <Style Selector="^ /template/ Border#PART_Background">
          <Setter Property="Background" Value="{DynamicResource ComboBoxBackgroundUnfocused}" />
        </Style>
      </Style>
    </Style>
  </UserControl.Styles>
</UserControl>
