<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">
            <Grid ColumnDefinitions="16,16,Auto,*"
                  Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
                  Background="Transparent"
                  DoubleTapped="OnRowDoubleTapped"
                  DataContextChanged="OnRowDataContextChanged">
              <v:ChangeTreeNodeToggleButton Grid.Column="0"
                                            Classes="tree_expander"
                                            Focusable="False"
                                            HorizontalAlignment="Center"
                                            IsChecked="{Binding IsExpanded, Mode=OneWay}"
                                            IsVisible="{Binding IsFolder}"/>

              <ToggleButton Grid.Column="1"
                            Classes="folder"
                            Focusable="False"
                            Width="14" Height="14"
                            Margin="0,1,0,0"
                            Foreground="Goldenrod"
                            IsChecked="{Binding IsExpanded}"
                            IsVisible="{Binding IsFolder}"/>

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

              <StackPanel Grid.Column="2" Orientation="Horizontal" Margin="4,0,0,0">
                <TextBlock Text="{Binding ConflictMarker, Mode=OneWay}" Foreground="DarkOrange" FontWeight="Bold" Margin="0,0,4,0" IsVisible="{Binding ShowConflictMarker}"/>
                <TextBlock Text="{Binding DisplayName, Mode=OneWay}"/>
              </StackPanel>
            </Grid>
          </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">
            <Grid ColumnDefinitions="Auto,Auto,Auto,*"
                  Background="Transparent"
                  DoubleTapped="OnRowDoubleTapped"
                  DataContextChanged="OnRowDataContextChanged">
              <v:ChangeStatusIcon Grid.Column="0"
                                  Width="14" Height="14"
                                  Margin="4,0,0,0"
                                  IsUnstagedChange="{Binding #ThisControl.IsUnstagedChange}"
                                  Change="{Binding}" />

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

              <TextBlock Grid.Column="2"
                         Text="{Binding Path, Converter={x:Static c:PathConverters.PureDirectoryName}}"
                         Foreground="{DynamicResource Brush.FG2}"/>
            </Grid>
          </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">
            <Grid ColumnDefinitions="Auto,Auto,*"
                  Background="Transparent"
                  DoubleTapped="OnRowDoubleTapped"
                  DataContextChanged="OnRowDataContextChanged">
              <v:ChangeStatusIcon Grid.Column="0"
                                  Width="14" Height="14"
                                  Margin="4,0,0,0"
                                  IsUnstagedChange="{Binding #ThisControl.IsUnstagedChange}"
                                  Change="{Binding}" />

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