<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"
             xmlns:vm="using:SourceGit.ViewModels"
             xmlns:c="using:SourceGit.Converters"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="SourceGit.Views.ChangeCollectionView"
             x:Name="ThisControl">
  <UserControl.Styles>
    <Style Selector="ListBox">
      <Setter Property="Background" Value="Transparent"/>
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
      <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
      <Setter Property="ItemsPanel">
        <ItemsPanelTemplate>
          <VirtualizingStackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
      </Setter>
    </Style>

    <Style Selector="ListBoxItem">
      <Setter Property="Height" Value="24"/>
      <Setter Property="Margin" Value="0"/>
      <Setter Property="Padding" Value="0"/>
    </Style>
  </UserControl.Styles>

  <UserControl.DataTemplates>
    <DataTemplate DataType="vm:ChangeCollectionAsTree">
      <v:ChangeCollectionContainer Focusable="True"
                                   ItemsSource="{Binding Rows}"
                                   SelectedItems="{Binding SelectedRows, Mode=TwoWay}"
                                   SelectionMode="Multiple"
                                   SelectionChanged="OnRowSelectionChanged">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="vm:ChangeTreeNode">
            <StackPanel Orientation="Horizontal"
                        Height="24"
                        Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
                        Background="Transparent"
                        DoubleTapped="OnRowDoubleTapped"
                        DataContextChanged="OnRowDataContextChanged">
              <Border Width="16">
                <v:ChangeTreeNodeToggleButton Classes="tree_expander"
                                              Focusable="False"
                                              HorizontalAlignment="Center"
                                              IsChecked="{Binding IsExpanded, Mode=OneWay}"
                                              IsVisible="{Binding IsFolder}"/>
              </Border>
              
              <ToggleButton Classes="folder"
                            Focusable="False"
                            Width="14" Height="14"
                            Margin="1,1,1,0"
                            Foreground="Goldenrod"
                            IsChecked="{Binding IsExpanded}"
                            IsVisible="{Binding IsFolder}"/>

              <v:ChangeStatusIcon Width="14" Height="14"
                                  Margin="1,0"
                                  IsUnstagedChange="{Binding #ThisControl.IsUnstagedChange}"
                                  Change="{Binding Change}"
                                  IsVisible="{Binding !IsFolder}"/>

              <TextBlock Text="{Binding ConflictMarker, Mode=OneWay}"
                         Foreground="DarkOrange"
                         FontWeight="Bold"
                         Margin="4,0,0,0"
                         IsVisible="{Binding ShowConflictMarker}"/>
              
              <TextBlock Text="{Binding DisplayName, Mode=OneWay}"
                         Margin="4,0,0,0"/>
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </v:ChangeCollectionContainer>
    </DataTemplate>

    <DataTemplate DataType="vm:ChangeCollectionAsGrid">
      <v:ChangeCollectionContainer Focusable="True"
                                   ItemsSource="{Binding Changes}"
                                   SelectedItems="{Binding SelectedChanges, Mode=TwoWay}"
                                   SelectionMode="Multiple"
                                   SelectionChanged="OnRowSelectionChanged">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="m:Change">
            <StackPanel Orientation="Horizontal"
                        Height="24"
                        Background="Transparent"
                        DoubleTapped="OnRowDoubleTapped"
                        DataContextChanged="OnRowDataContextChanged">
              <v:ChangeStatusIcon Width="14" Height="14"
                                  Margin="4,0,0,0"
                                  IsUnstagedChange="{Binding #ThisControl.IsUnstagedChange}"
                                  Change="{Binding}" />

              <TextBlock Text="{Binding ConflictMarker}"
                         Foreground="DarkOrange"
                         FontWeight="Bold"
                         Margin="4,0,0,0" IsVisible="{Binding IsConflicted}"/>
              
              <TextBlock Text="{Binding Path, Converter={x:Static c:PathConverters.PureFileName}}"
                         Margin="4,0,0,0"/>

              <TextBlock Text="{Binding Path, Converter={x:Static c:PathConverters.PureDirectoryName}}"
                         Foreground="{DynamicResource Brush.FG2}"
                         Margin="4,0,0,0"/>
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </v:ChangeCollectionContainer>
    </DataTemplate>

    <DataTemplate DataType="vm:ChangeCollectionAsList">
      <v:ChangeCollectionContainer Focusable="True"
                                   ItemsSource="{Binding Changes}"
                                   SelectedItems="{Binding SelectedChanges, Mode=TwoWay}"
                                   SelectionMode="Multiple"
                                   SelectionChanged="OnRowSelectionChanged">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="m:Change">
            <StackPanel Orientation="Horizontal"
                        Height="24"
                        Background="Transparent"
                        DoubleTapped="OnRowDoubleTapped"
                        DataContextChanged="OnRowDataContextChanged">
              <v:ChangeStatusIcon Width="14" Height="14"
                                  Margin="4,0,0,0"
                                  IsUnstagedChange="{Binding #ThisControl.IsUnstagedChange}"
                                  Change="{Binding}" />

              <TextBlock Text="{Binding ConflictMarker}"
                         Foreground="DarkOrange"
                         FontWeight="Bold"
                         Margin="4,0,0,0"
                         IsVisible="{Binding IsConflicted}"/>
              
              <TextBlock Margin="4,0,0,0"
                         Text="{Binding Path}"/>
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </v:ChangeCollectionContainer>
    </DataTemplate>
  </UserControl.DataTemplates>
</UserControl>
