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

    <v:LayoutableGrid.ColumnDefinitions>
      <ColumnDefinition Width="{Binding LeftArea, Mode=TwoWay}" MinWidth="100"/>
      <ColumnDefinition Width="3"/>
      <ColumnDefinition Width="{Binding RightArea, Mode=TwoWay}" MinWidth="100"/>
    </v:LayoutableGrid.ColumnDefinitions>
    
    <Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
      <Grid RowDefinitions="24,*">
        <!-- Headers -->
        <Border Grid.Row="0"
                Background="{DynamicResource Brush.Window}"
                BorderThickness="0,0,0,1"
                BorderBrush="{DynamicResource Brush.Border0}">
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" MinWidth="100"/>
              <ColumnDefinition Width="3"/>
              <ColumnDefinition Width="{Binding #ThisControl.AuthorNameColumnWidth, Mode=TwoWay}" MinWidth="80"/>
              <ColumnDefinition Width="100" MaxWidth="100" MinWidth="100"/>
              <ColumnDefinition Width="170" MaxWidth="170" MinWidth="170"/>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Classes="table_header" Text="{DynamicResource Text.Histories.Header.GraphAndSubject}" HorizontalAlignment="Center"/>
            <GridSplitter Grid.Column="1" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            <Border Grid.Column="2" BorderThickness="1,0,0,0" BorderBrush="{DynamicResource Brush.Border0}" ClipToBounds="True">
              <TextBlock Classes="table_header" Text="{DynamicResource Text.Histories.Header.Author}" HorizontalAlignment="Center"/>
            </Border>
            <Border Grid.Column="3" BorderThickness="1,0" BorderBrush="{DynamicResource Brush.Border0}" ClipToBounds="True">
              <TextBlock Classes="table_header" Text="{DynamicResource Text.Histories.Header.SHA}" HorizontalAlignment="Center"/>
            </Border>
            <StackPanel Grid.Column="4" Orientation="Horizontal" 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}"/>
            </StackPanel>
          </Grid>
        </Border>

        <!-- Commit Lists & Graph -->
        <Grid Grid.Row="1">
          <ListBox x:Name="CommitListContainer"
                   Background="{DynamicResource Brush.Contents}"
                   ItemsSource="{Binding Commits}"
                   SelectionMode="Multiple"
                   SelectedItem="{Binding AutoSelectedCommit, Mode=OneWay}"
                   LayoutUpdated="OnCommitListLayoutUpdated"
                   SelectionChanged="OnCommitListSelectionChanged"
                   ContextRequested="OnCommitListContextRequested"
                   DoubleTapped="OnCommitListDoubleTapped"
                   KeyDown="OnCommitListKeyDown">
            <ListBox.Styles>
              <Style Selector="ListBoxItem">
                <Setter Property="Margin" Value="0"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Height" Value="28"/>
                <Setter Property="Template">
                  <ControlTemplate>
                    <Grid>
                      <Border Name="PART_Background"
                              Background="Transparent"
                              CornerRadius="{TemplateBinding CornerRadius}"
                              Opacity="0"/>

                      <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
                                        Content="{TemplateBinding Content}"
                                        Padding="{TemplateBinding Padding}"
                                        VerticalContentAlignment="Center"
                                        HorizontalContentAlignment="Stretch" />
                    </Grid>
                  </ControlTemplate>
                </Setter>

                <Style Selector="^:pointerover /template/ Border#PART_Background">
                  <Setter Property="Background" Value="{DynamicResource Brush.AccentHovered}" />
                  <Setter Property="Opacity" Value=".5"/>
                </Style>
                <Style Selector="^:selected /template/ Border#PART_Background">
                  <Setter Property="Background" Value="{DynamicResource Brush.Accent}" />
                  <Setter Property="Opacity" Value=".65"/>
                </Style>
                <Style Selector="^:selected:pointerover /template/ Border#PART_Background">
                  <Setter Property="Background" Value="{DynamicResource Brush.Accent}" />
                  <Setter Property="Opacity" Value=".8"/>
                </Style>
              </Style>
            </ListBox.Styles>

            <ListBox.ItemsPanel>
              <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Vertical"/>
              </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

            <ListBox.ItemTemplate>
              <DataTemplate DataType="m:Commit">
                <Grid Height="26">
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="{Binding #ThisControl.AuthorNameColumnWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="100" MaxWidth="100" MinWidth="100"/>
                    <ColumnDefinition Width="170" MaxWidth="170" MinWidth="170"/>
                  </Grid.ColumnDefinitions>

                  <!-- Subject & REFS -->
                  <Border Grid.Column="0" Padding="{Binding Margin}" ClipToBounds="True">
                    <Grid ColumnDefinitions="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:CommitRefsPresenter Grid.Column="1"
                                             Background="{DynamicResource Brush.Contents}"
                                             TagBackground="{DynamicResource Brush.DecoratorTag}"
                                             Foreground="{DynamicResource Brush.FG1}"
                                             FontFamily="{DynamicResource Fonts.Primary}"
                                             FontSize="11"
                                             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="2"
                                                Classes="primary"
                                                Subject="{Binding Subject}"
                                                IssueTrackerRules="{Binding $parent[v:Histories].IssueTrackerRules}"
                                                FontWeight="{Binding FontWeight}"
                                                Opacity="{Binding Opacity}"/>
                    </Grid>
                  </Border>

                  <!-- Author -->
                  <Grid Grid.Column="1" ColumnDefinitions="20,*" IsHitTestVisible="False">
                    <v:Avatar Grid.Column="0"
                              Width="16" Height="16"
                              Margin="4,0,0,0"
                              HorizontalAlignment="Left"
                              VerticalAlignment="Center"
                              IsHitTestVisible="False"
                              User="{Binding Author}"
                              Opacity="{Binding Opacity}"/>

                    <Border Grid.Column="1" Padding="8,0,0,0" ClipToBounds="True">
                      <TextBlock Classes="primary" 
                                 Text="{Binding Author.Name}"
                                 FontWeight="{Binding FontWeight}"
                                 HorizontalAlignment="Left"
                                 Opacity="{Binding Opacity}"/>
                    </Border>
                  </Grid>

                  <!-- SHA -->
                  <Border Grid.Column="2" Padding="4,0" IsHitTestVisible="False" ClipToBounds="True">
                    <TextBlock Classes="primary"
                               Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
                               HorizontalAlignment="Center"
                               FontWeight="{Binding FontWeight}"
                               Opacity="{Binding Opacity}"/>
                  </Border>

                  <!-- TIME -->
                  <Border Grid.Column="3" Padding="4,0" ClipToBounds="True" IsHitTestVisible="False">
                    <v:CommitTimeTextBlock Classes="primary"
                                           HorizontalAlignment="Center"
                                           FontWeight="{Binding FontWeight}"
                                           Opacity="{Binding Opacity}"
                                           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>
                </Grid>
              </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox>

          <v:CommitGraph x:Name="CommitGraph"
                         Graph="{Binding Graph}"
                         DotBrush="{DynamicResource Brush.Contents}"
                         OnlyHighlightCurrentBranch="{Binding $parent[v:Histories].OnlyHighlightCurrentBranch}"
                         HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                         IsHitTestVisible="False"
                         ClipToBounds="True"/>
        </Grid>
      </Grid>
      
      <!-- 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">
      <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="x:Int32">
            <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 Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/>
            </StackPanel>
          </DataTemplate>
        </ContentControl.DataTemplates>
      </ContentControl>
    </Grid>
  </v:LayoutableGrid>
</UserControl>
