<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 ItemsSource="{Binding Rows}"
                                   SelectedItems="{Binding SelectedRows, Mode=TwoWay}"
                                   SelectionMode="{Binding #ThisControl.SelectionMode}"
                                   SelectionChanged="OnRowSelectionChanged">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="vm:ChangeTreeNode">
            <Grid ColumnDefinitions="16,Auto,Auto,*"
                  Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
                  Background="Transparent"
                  DoubleTapped="OnRowDoubleTapped">
              <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}"/>
              <TextBlock Grid.Column="2" Classes="primary" Text="{Binding FullPath, Converter={x:Static c:PathConverters.PureFileName}}" Margin="6,0,0,0"/>
            </Grid>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </v:ChangeCollectionContainer>
    </DataTemplate>

    <DataTemplate DataType="vm:ChangeCollectionAsGrid">
      <v:ChangeCollectionContainer ItemsSource="{Binding Changes}"
                                   SelectedItems="{Binding SelectedChanges, Mode=TwoWay}"
                                   SelectionMode="{Binding #ThisControl.SelectionMode}"
                                   SelectionChanged="OnRowSelectionChanged">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="m:Change">
            <Grid ColumnDefinitions="Auto,Auto,Auto,*" Background="Transparent" DoubleTapped="OnRowDoubleTapped">
              <v:ChangeStatusIcon Grid.Column="0"
                                  Width="14" Height="14"
                                  Margin="4,0,0,0"
                                  IsUnstagedChange="{Binding #ThisControl.IsUnstagedChange}"
                                  Change="{Binding}" />

              <TextBlock Grid.Column="1"
                         Classes="primary"
                         Text="{Binding Path, Converter={x:Static c:PathConverters.PureFileName}}"
                         Margin="4,0"/>

              <TextBlock Grid.Column="2"
                         Classes="primary"
                         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 ItemsSource="{Binding Changes}"
                                   SelectedItems="{Binding SelectedChanges, Mode=TwoWay}"
                                   SelectionMode="{Binding #ThisControl.SelectionMode}"
                                   SelectionChanged="OnRowSelectionChanged">
        <ListBox.ItemTemplate>
          <DataTemplate DataType="m:Change">
            <Grid ColumnDefinitions="Auto,Auto,*" Background="Transparent" DoubleTapped="OnRowDoubleTapped">
              <v:ChangeStatusIcon Grid.Column="0"
                                  Width="14" Height="14"
                                  Margin="4,0,0,0"
                                  IsUnstagedChange="{Binding #ThisControl.IsUnstagedChange}"
                                  Change="{Binding}" />

              <TextBlock Grid.Column="1"
                         Classes="primary"
                         Text="{Binding Path}"
                         Margin="4,0"/>
            </Grid>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </v:ChangeCollectionContainer>
    </DataTemplate>
  </UserControl.DataTemplates>
</UserControl>
