<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:c="using:SourceGit.Converters"
             xmlns:v="using:SourceGit.Views"
             xmlns:vm="using:SourceGit.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="SourceGit.Views.BranchTree"
             x:Name="ThisControl">
  <DataGrid x:Name="BranchesPresenter"
            ItemsSource="{Binding #ThisControl.Rows}"
            Background="Transparent"
            RowHeight="24"
            CanUserReorderColumns="False"
            CanUserResizeColumns="False"
            CanUserSortColumns="False"
            HorizontalScrollBarVisibility="Disabled"
            VerticalScrollBarVisibility="Auto"
            HeadersVisibility="None"
            SelectionChanged="OnNodesSelectionChanged"
            ContextRequested="OnTreeContextRequested">
    <DataGrid.Styles>
      <Style Selector="DataGridRow" x:DataType="vm:BranchTreeNode">
        <Setter Property="CornerRadius" Value="{Binding CornerRadius}" />
      </Style>

      <Style Selector="DataGridRow /template/ Border#RowBorder">
        <Setter Property="ClipToBounds" Value="True" />
      </Style>

      <Style Selector="Grid.repository_leftpanel DataGridRow:pointerover /template/ Rectangle#BackgroundRectangle">
        <Setter Property="Fill" Value="{DynamicResource Brush.AccentHovered}" />
        <Setter Property="Opacity" Value=".5"/>
      </Style>
      <Style Selector="Grid.repository_leftpanel DataGridRow:selected /template/ Rectangle#BackgroundRectangle">
        <Setter Property="Fill" Value="{DynamicResource Brush.AccentHovered}" />
        <Setter Property="Opacity" Value="1"/>
      </Style>
      <Style Selector="Grid.repository_leftpanel:focus-within DataGridRow:selected /template/ Rectangle#BackgroundRectangle">
        <Setter Property="Fill" Value="{DynamicResource Brush.Accent}" />
        <Setter Property="Opacity" Value=".65"/>
      </Style>
      <Style Selector="Grid.repository_leftpanel:focus-within DataGridRow:selected:pointerover /template/ Rectangle#BackgroundRectangle">
        <Setter Property="Fill" Value="{DynamicResource Brush.Accent}" />
        <Setter Property="Opacity" Value=".8"/>
      </Style>
    </DataGrid.Styles>

    <DataGrid.Columns>
      <DataGridTemplateColumn Width="*">
        <DataGridTemplateColumn.CellTemplate>
          <DataTemplate x:DataType="vm:BranchTreeNode">
            <Grid Height="24"
                  Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
                  ColumnDefinitions="16,*"
                  ToolTip.Tip="{Binding Tooltip}">

              <!-- Tree Expander -->
              <v:BranchTreeNodeToggleButton Grid.Column="0"
                                            Classes="tree_expander"
                                            Focusable="False"
                                            HorizontalAlignment="Center"
                                            IsChecked="{Binding IsExpanded, Mode=OneWay}"
                                            IsVisible="{Binding !IsBranch}"/>

              <!-- Content Area (allows double-click) -->
              <Grid Grid.Column="1"
                    Background="Transparent"
                    ColumnDefinitions="18,*,Auto,Auto"
                    DoubleTapped="OnDoubleTappedBranchNode">
                
                <!-- Icon -->
                <v:BranchTreeNodeIcon Grid.Column="0"
                                      Node="{Binding}"
                                      IsExpanded="{Binding IsExpanded}"/>

                <!-- Name -->
                <TextBlock Grid.Column="1"
                           Text="{Binding Name}"
                           Classes="monospace"
                           FontWeight="{Binding NameFontWeight}"/>

                <!-- Tracking status -->
                <Border Grid.Column="2"
                        Margin="8,0"
                        Height="18"
                        CornerRadius="9"
                        VerticalAlignment="Center"
                        Background="{DynamicResource Brush.Badge}"
                        IsVisible="{Binding TrackStatus, Converter={x:Static StringConverters.IsNotNullOrEmpty}}">
                  <TextBlock Classes="monospace" 
                             FontSize="10" 
                             HorizontalAlignment="Center" 
                             Margin="9,0" 
                             Text="{Binding TrackStatus}" 
                             Foreground="{DynamicResource Brush.BadgeFG}"/>
                </Border>

                <!-- Filter Toggle Button -->
                <ToggleButton Grid.Column="3"
                              Classes="filter"
                              Margin="0,0,8,0"
                              Background="Transparent"
                              IsCheckedChanged="OnToggleFilter"
                              IsVisible="{Binding IsBranch}"
                              IsChecked="{Binding IsFiltered}"
                              ToolTip.Tip="{DynamicResource Text.Filter}"/>
              </Grid>
            </Grid>
          </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
  </DataGrid>
</UserControl>

