<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"
             x:Name="ThisControl">
  <v:HistoriesLayout UseHorizontal="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseTwoColumnsLayoutInHistories}">
    <v:HistoriesLayout.RowDefinitions>
      <RowDefinition Height="{Binding TopArea, Mode=TwoWay}" MinHeight="100"/>
      <RowDefinition Height="3"/>
      <RowDefinition Height="{Binding BottomArea, Mode=TwoWay}" MinHeight="200"/>
    </v:HistoriesLayout.RowDefinitions>

    <v:HistoriesLayout.ColumnDefinitions>
      <ColumnDefinition Width="{Binding LeftArea, Mode=TwoWay}" MinWidth="100"/>
      <ColumnDefinition Width="3"/>
      <ColumnDefinition Width="{Binding RightArea, Mode=TwoWay}" MinWidth="100"/>
    </v:HistoriesLayout.ColumnDefinitions>

    <Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
      <DataGrid x:Name="CommitListContainer"
                Classes="static_scrollbar"
                ScrollViewer.AllowAutoHide="False"
                Background="{DynamicResource Brush.Window}"
                SelectionMode="Extended"
                ItemsSource="{Binding Commits, Mode=OneWay}"
                SelectedItem="{Binding AutoSelectedCommit, Mode=OneWay}"
                CanUserReorderColumns="False"
                CanUserResizeColumns="True"
                CanUserSortColumns="False"
                AutoGenerateColumns="False"
                IsReadOnly="True"
                HeadersVisibility="Column"
                ColumnHeaderHeight="24"
                ClipboardCopyMode="None"
                Focusable="False"
                RowHeight="26"
                HorizontalScrollBarVisibility="Disabled"
                VerticalScrollBarVisibility="Auto"
                Loaded="OnCommitListLoaded"
                LayoutUpdated="OnCommitListLayoutUpdated"
                SelectionChanged="OnCommitListSelectionChanged"
                ContextRequested="OnCommitListContextRequested"
                DoubleTapped="OnCommitListDoubleTapped"
                KeyDown="OnCommitListKeyDown">
        <DataGrid.Resources>
          <SolidColorBrush x:Key="DataGridGridLinesBrush" Color="{DynamicResource Color.Border0}" />
        </DataGrid.Resources>

        <DataGrid.Styles>
          <Style Selector="DataGrid.static_scrollbar">
            <Style Selector="^ /template/ DataGridRowsPresenter#PART_RowsPresenter">
              <Setter Property="Grid.RowSpan" Value="1" />
              <Setter Property="Grid.ColumnSpan" Value="2" />
            </Style>
            <Style Selector="^ /template/ DataGridColumnHeadersPresenter#PART_ColumnHeadersPresenter">
              <Setter Property="Grid.Column" Value="0" />
            </Style>
            <Style Selector="^ /template/ ScrollBar">
              <Setter Property="Background" Value="{DynamicResource Brush.Contents}"/>
            </Style>
          </Style>
          
          <Style Selector="DataGridColumnHeader">
            <Setter Property="MinHeight" Value="24"/>
            <Setter Property="Background" Value="{DynamicResource Brush.Window}"/>
            <Setter Property="Template">
              <ControlTemplate>
                <Border x:Name="HeaderBorder"
                        Background="{TemplateBinding Background}"
                        BorderThickness="0"
                        CornerRadius="0">
                  <Grid ColumnDefinitions="*,4">
                    <ContentPresenter x:Name="PART_ContentPresenter"
                                      Grid.Column="0"
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}" />

                    <Rectangle Name="VerticalSeparator"
                               Grid.Column="1"
                               Width="1"
                               HorizontalAlignment="Center" VerticalAlignment="Stretch"
                               Fill="{DynamicResource Brush.Border0}"
                               IsVisible="{TemplateBinding AreSeparatorsVisible}" />
                  </Grid>
                </Border>
              </ControlTemplate>
            </Setter>
          </Style>

          <Style Selector="DataGridCell">
            <Setter Property="Template">
              <ControlTemplate>
                <Grid x:Name="PART_CellRoot">
                  <ContentPresenter Margin="0"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    Content="{TemplateBinding Content}"
                                    ContentTemplate="{TemplateBinding ContentTemplate}"
                                    Foreground="{TemplateBinding Foreground}" />
                </Grid>
              </ControlTemplate>
            </Setter>
          </Style>

          <Style Selector="DataGridRowsPresenter">
            <Setter Property="Background" Value="{DynamicResource Brush.Contents}"/>
          </Style>
        </DataGrid.Styles>

        <DataGrid.Columns>
          <DataGridTemplateColumn Width="*" IsReadOnly="True">
            <DataGridTemplateColumn.Header>
              <TextBlock Classes="table_header" Text="{DynamicResource Text.Histories.Header.GraphAndSubject}" HorizontalAlignment="Center"/>
            </DataGridTemplateColumn.Header>

            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate DataType="m:Commit">
                <Border Padding="{Binding LeftMargin, Converter={x:Static c:DoubleConverters.ToLeftMargin}}" ClipToBounds="True" Background="Transparent">
                  <Grid ColumnDefinitions="Auto,Auto,Auto,*" Margin="2,0,4,0" ClipToBounds="True">
                    <v:CommitStatusIndicator Grid.Column="0"
                                             CurrentBranch="{Binding $parent[v:Histories].CurrentBranch}"
                                             AheadBrush="{DynamicResource Brush.Accent}"
                                             BehindBrush="{DynamicResource Brush.FG1}"
                                             VerticalAlignment="Center"/>

                    <v:BisectStateIndicator Grid.Column="1"
                                            Background="{DynamicResource Brush.Contents}"
                                            Foreground="{DynamicResource Brush.FG1}"
                                            Bisect="{Binding $parent[v:Histories].Bisect}"
                                            VerticalAlignment="Center"/>

                    <v:CommitRefsPresenter Grid.Column="2"
                                           Background="{DynamicResource Brush.Contents}"
                                           Foreground="{DynamicResource Brush.FG1}"
                                           ShowTags="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowTagsInGraph}"
                                           FontSize="12"
                                           VerticalAlignment="Center">
                      <v:CommitRefsPresenter.UseGraphColor>
                        <MultiBinding Converter="{x:Static BoolConverters.Or}">
                          <Binding Path="IsMerged"/>
                          <Binding Path="$parent[v:Histories].OnlyHighlightCurrentBranch" Converter="{x:Static BoolConverters.Not}"/>
                        </MultiBinding>
                      </v:CommitRefsPresenter.UseGraphColor>
                    </v:CommitRefsPresenter>

                    <v:CommitSubjectPresenter Grid.Column="3"
                                              FontFamily="{DynamicResource Fonts.Default}"
                                              CodeFontFamily="{DynamicResource Fonts.Monospace}"
                                              InlineCodeBackground="{DynamicResource Brush.InlineCode}"
                                              Foreground="{DynamicResource Brush.FG1}"
                                              LinkForeground="{DynamicResource Brush.Link}"
                                              Subject="{Binding Subject}"
                                              IssueTrackers="{Binding $parent[v:Histories].IssueTrackers}"
                                              FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
                                              Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
                  </Grid>
                </Border>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>

          <DataGridTemplateColumn MinWidth="80" CanUserResize="True" IsReadOnly="True">
            <DataGridTemplateColumn.Width>
              <Binding Source="{x:Static vm:Preferences.Instance}"
                       Path="Layout.AuthorColumnWidth"
                       Mode="TwoWay"/>
            </DataGridTemplateColumn.Width>
            <DataGridTemplateColumn.Header>
              <TextBlock Classes="table_header" Text="{DynamicResource Text.Histories.Header.Author}" HorizontalAlignment="Center"/>
            </DataGridTemplateColumn.Header>

            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate DataType="m:Commit">
                <Grid ColumnDefinitions="20,*" Background="Transparent">
                  <v:Avatar Grid.Column="0"
                            Width="16" Height="16"
                            Margin="4,0,0,0"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Center"
                            User="{Binding Author}"
                            Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
                            IsHitTestVisible="False"/>

                  <Border Grid.Column="1" Padding="8,0" ClipToBounds="True">
                    <TextBlock Text="{Binding Author.Name}"
                               FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
                               Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
                               HorizontalAlignment="Left"/>
                  </Border>
                </Grid>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>

          <DataGridTemplateColumn MinWidth="100" CanUserResize="False" IsReadOnly="True">
            <DataGridTemplateColumn.Header>
              <TextBlock Classes="table_header" Text="{DynamicResource Text.Histories.Header.SHA}" HorizontalAlignment="Center"/>
            </DataGridTemplateColumn.Header>

            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate DataType="m:Commit">
                <Border Padding="8,0,0,0" Background="Transparent" ClipToBounds="True">
                  <TextBlock Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
                             FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
                             Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
                </Border>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>

          <DataGridTemplateColumn MinWidth="160" CanUserResize="False" IsReadOnly="True">
            <DataGridTemplateColumn.Header>
              <Grid HorizontalAlignment="Center">
                <TextBlock Classes="table_header"
                           Margin="6,0,0,0"
                           Text="{DynamicResource Text.Histories.Header.Time}"
                           IsVisible="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowAuthorTimeInGraph, Converter={x:Static BoolConverters.Not}, Mode=OneWay}"/>
                <TextBlock Classes="table_header"
                           Margin="6,0,0,0"
                           Text="{DynamicResource Text.Histories.Header.AuthorTime}"
                           IsVisible="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowAuthorTimeInGraph, Mode=OneWay}"/>
              </Grid>
            </DataGridTemplateColumn.Header>

            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate DataType="m:Commit">
                <Border Padding="8,0" Background="Transparent" ClipToBounds="True">
                  <v:CommitTimeTextBlock FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
                                         Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
                                         UseAuthorTime="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowAuthorTimeInGraph, Mode=OneWay}"
                                         ShowAsDateTime="{Binding Source={x:Static vm:Preferences.Instance}, Path=!DisplayTimeAsPeriodInHistories}"
                                         DateTimeFormat="{Binding Source={x:Static vm:Preferences.Instance}, Path=DateTimeFormat}"/>
                </Border>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
        </DataGrid.Columns>
      </DataGrid>

      <v:CommitGraph x:Name="CommitGraph"
                     Margin="0,24,0,0"
                     Graph="{Binding Graph}"
                     DotBrush="{DynamicResource Brush.Contents}"
                     OnlyHighlightCurrentBranch="{Binding $parent[v:Histories].OnlyHighlightCurrentBranch}"
                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                     IsHitTestVisible="False"
                     ClipToBounds="True"/>

      <Border Width="32" Height="32"
              Margin="0,0,16,8"
              CornerRadius="16"
              Background="{DynamicResource Brush.Accent}"
              HorizontalAlignment="Right" VerticalAlignment="Bottom"
              Effect="drop-shadow(0 0 8 #80000000)"
              PointerPressed="OnScrollToTopPointerPressed"
              IsVisible="{Binding $parent[v:Histories].IsScrollToTopVisible}">
        <Path Width="16" Height="16" Data="{StaticResource Icons.ScrollToTop}" Fill="White"/>
      </Border>

      <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">
      <Grid IsVisible="{Binding DetailContext, Converter={x:Static ObjectConverters.IsNull}}">
        <Path Width="128" Height="128"
              Data="{StaticResource Icons.Detail}"
              HorizontalAlignment="Center"
              Fill="{DynamicResource Brush.FG2}"/>

        <StackPanel Margin="0,8" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom">
          <Path Width="12" Height="12" VerticalAlignment="Center" Data="{StaticResource Icons.Info}" Fill="{DynamicResource Brush.FG2}"/>
          <TextBlock Margin="4,0" Text="{DynamicResource Text.Histories.Tips.Prefix}" FontWeight="Bold" Foreground="{DynamicResource Brush.FG2}"/>
          <TextBlock Text="{DynamicResource Text.Histories.Tips}" Foreground="{DynamicResource Brush.FG2}" IsVisible="{OnPlatform True, macOS=False}"/>
          <TextBlock Text="{DynamicResource Text.Histories.Tips.MacOS}" Foreground="{DynamicResource Brush.FG2}" IsVisible="{OnPlatform False, macOS=True}"/>
        </StackPanel>
      </Grid>

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

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

          <DataTemplate DataType="m:Count">
            <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 Value, Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/>
            </StackPanel>
          </DataTemplate>
        </ContentControl.DataTemplates>
      </ContentControl>
    </Grid>
  </v:HistoriesLayout>
</UserControl>
