<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:vm="using:SourceGit.ViewModels"
             xmlns:v="using:SourceGit.Views"
             xmlns:c="using:SourceGit.Converters"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="SourceGit.Views.Histories"
             x:DataType="vm:Histories">
  <v:LayoutableGrid RowDefinitions="*,3,*" ColumnDefinitions="*,3,*"
                    UseHorizontal="{Binding Source={x:Static vm:Preference.Instance}, Path=UseTwoColumnsLayoutInHistories}">
    <Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
      <DataGrid x:Name="commitDataGrid"
                Background="{DynamicResource Brush.Contents}"
                ItemsSource="{Binding Commits}"
                SelectionMode="Extended"
                SelectedItem="{Binding AutoSelectedCommit, Mode=OneWay}"
                CanUserReorderColumns="False"
                CanUserResizeColumns="False"
                CanUserSortColumns="False"
                IsReadOnly="True"
                HeadersVisibility="None"
                Focusable="False"
                RowHeight="28"
                HorizontalScrollBarVisibility="Disabled"
                VerticalScrollBarVisibility="Auto"
                LayoutUpdated="OnCommitDataGridLayoutUpdated"
                SelectionChanged="OnCommitDataGridSelectionChanged"
                ContextRequested="OnCommitDataGridContextRequested">
        <DataGrid.Columns>
          <DataGridTemplateColumn Width="*" Header="GRAPH">
            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate x:DataType="{x:Type m:Commit}">
                <Border Margin="{Binding Margin}">
                  <StackPanel Orientation="Horizontal" Margin="2,0,0,0">
                    <ItemsControl ItemsSource="{Binding Decorators}" IsVisible="{Binding HasDecorators}">
                      <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                          <WrapPanel Orientation="Horizontal" VerticalAlignment="Center"/>
                        </ItemsPanelTemplate>
                      </ItemsControl.ItemsPanel>

                      <ItemsControl.ItemTemplate>
                        <DataTemplate DataType="{x:Type m:Decorator}">
                          <Border Height="16" Margin="0,0,4,0" CornerRadius="2" ClipToBounds="True">
                            <StackPanel Orientation="Horizontal">
                              <Border Background="{DynamicResource Brush.Decorator}" Width="16">
                                <Path Width="8" Height="8" 
                                      Stretch="Uniform" 
                                      Data="{Binding Type, Converter={x:Static c:DecoratorTypeConverters.ToIcon}}" 
                                      Fill="{DynamicResource Brush.DecoratorIcon}"
                                      VerticalAlignment="Center"/>
                              </Border>
                              <Border Background="{Binding Type, Converter={x:Static c:DecoratorTypeConverters.ToBackground}}">
                                <TextBlock Classes="monospace" 
                                           Text="{Binding Name}" 
                                           FontSize="10" 
                                           Margin="4,0" 
                                           Foreground="Black"
                                           FontWeight="{Binding Type, Converter={x:Static c:DecoratorTypeConverters.ToFontWeight}}"/>
                              </Border>
                            </StackPanel>
                          </Border>
                        </DataTemplate>
                      </ItemsControl.ItemTemplate>
                    </ItemsControl>

                    <TextBlock Classes="monospace" 
                               Text="{Binding Subject}"
                               Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.HalfIfFalse}}"
                               FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.BoldIfTrue}}"/>
                  </StackPanel>
                </Border>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>

          <DataGridTemplateColumn Header="AVATAR">
            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate x:DataType="{x:Type m:Commit}">
                <v:Avatar Width="16" Height="16"
                          Margin="16,0,8,0"
                          VerticalAlignment="Center"
                          IsHitTestVisible="False"
                          User="{Binding Author}"
                          Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.HalfIfFalse}}"/>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>

          <DataGridTemplateColumn MaxWidth="100" Header="AUTHOR">
            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate x:DataType="{x:Type m:Commit}">
                <TextBlock Classes="monospace"
                           Text="{Binding Author.Name}"
                           Margin="0,0,8,0"
                           Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.HalfIfFalse}}"
                           FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.BoldIfTrue}}"/>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>

          <DataGridTemplateColumn Header="SHA">
            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate x:DataType="{x:Type m:Commit}">
                <TextBlock Classes="monospace" 
                           Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
                           Margin="12,0"
                           Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.HalfIfFalse}}"
                           FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.BoldIfTrue}}"/>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>

          <DataGridTemplateColumn Header="TIME">
            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate x:DataType="{x:Type m:Commit}">
                <TextBlock Classes="monospace" 
                           Text="{Binding CommitterTimeStr}"
                           Margin="8,0"
                           Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.HalfIfFalse}}"
                           FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.BoldIfTrue}}"/>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
        </DataGrid.Columns>
      </DataGrid>

      <v:CommitGraph x:Name="commitGraph"
                     Graph="{Binding Graph}"
                     DotBrush="{DynamicResource Brush.Contents}"
                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                     IsHitTestVisible="False"
                     ClipToBounds="True"/>

      <!-- Fix memory leak -->
      <v:LoadingIcon Width="48" Height="48" HorizontalAlignment="Center" VerticalAlignment="Center" IsVisible="{Binding IsLoading}"/>
    </Grid>

    <GridSplitter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3"
                  MinWidth="1" MinHeight="1"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                  Background="{DynamicResource Brush.Window}"
                  BorderBrush="{DynamicResource Brush.Border0}"/>

    <Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
      <Border Background="{DynamicResource Brush.Window}">
        <Path Width="128" Height="128"
              Data="{StaticResource Icons.Detail}"
              HorizontalAlignment="Center"
              Fill="{DynamicResource Brush.FG2}"/>
      </Border>

      <ContentControl Content="{Binding DetailContext}">
        <ContentControl.DataTemplates>
          <DataTemplate DataType="vm:CommitDetail">
            <v:CommitDetail/>
          </DataTemplate>

          <DataTemplate DataType="vm:RevisionCompare">
            <v:RevisionCompare/>
          </DataTemplate>

          <DataTemplate DataType="vm:CountSelectedCommits">
            <Grid Background="{DynamicResource Brush.Window}">
              <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Path Width="128" Height="128"
                      Data="{StaticResource Icons.Detail}"
                      HorizontalAlignment="Center"
                      Fill="{DynamicResource Brush.FG2}"/>

                <TextBlock HorizontalAlignment="Center"
                           Margin="0,16"
                           FontSize="24" FontWeight="Bold"
                           Foreground="{DynamicResource Brush.FG2}"
                           Text="{Binding Count, Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/>
              </StackPanel>
            </Grid>
          </DataTemplate>
        </ContentControl.DataTemplates>
      </ContentControl>
    </Grid>
  </v:LayoutableGrid>
</UserControl>
