<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.CommitBaseInfo"
             x:Name="ThisControl">
  <UserControl.DataTemplates>
    <DataTemplate DataType="m:Commit">
      <StackPanel Orientation="Vertical">
        <!-- Author & Committer -->
        <UniformGrid Rows="1" Margin="0,8">
          <!-- Author -->
          <Grid ColumnDefinitions="96,*">
            <v:Avatar Grid.Column="0" Width="64" Height="64" HorizontalAlignment="Right" User="{Binding Author}"/>
            <StackPanel Grid.Column="1" Margin="16,0,8,0" Orientation="Vertical">
              <TextBlock Classes="group_header_label" Text="{DynamicResource Text.CommitDetail.Info.Author}"/>
              <Border Margin="0,10,0,8" Background="Transparent" ToolTip.Tip="{Binding Author}" Tag="{Binding Author}" ContextRequested="OnUserContextRequested">
                <TextBlock Foreground="{DynamicResource Brush.FG2}" TextTrimming="CharacterEllipsis">
                  <Run Text="{Binding Author.Name, Mode=OneWay}" Foreground="{DynamicResource Brush.FG1}"/>
                  <Run Text="{Binding Author.Email, Mode=OneWay}"/>
                </TextBlock>
              </Border>
              <TextBlock Text="{Binding AuthorTimeStr}"
                         Margin="2,0,0,0"
                         FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
                         Foreground="{DynamicResource Brush.FG2}"/>
            </StackPanel>
          </Grid>

          <!-- Committer -->
          <Grid ColumnDefinitions="96,*" IsVisible="{Binding IsCommitterVisible}">
            <v:Avatar Grid.Column="0" Width="64" Height="64" HorizontalAlignment="Right" User="{Binding Committer}"/>
            <StackPanel Grid.Column="1" Margin="16,0,8,0" Orientation="Vertical">
              <TextBlock Classes="group_header_label" Text="{DynamicResource Text.CommitDetail.Info.Committer}"/>
              <Border Margin="0,10,0,8" Background="Transparent" ToolTip.Tip="{Binding Committer}" Tag="{Binding Committer}" ContextRequested="OnUserContextRequested">
                <TextBlock Foreground="{DynamicResource Brush.FG2}" TextTrimming="CharacterEllipsis">
                  <Run Text="{Binding Committer.Name, Mode=OneWay}" Foreground="{DynamicResource Brush.FG1}"/>
                  <Run Text="{Binding Committer.Email, Mode=OneWay}"/>
                </TextBlock>
              </Border>
              <TextBlock Text="{Binding CommitterTimeStr}"
                         Margin="2,0,0,0"
                         FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
                         Foreground="{DynamicResource Brush.FG2}"/>
            </StackPanel>
          </Grid>
        </UniformGrid>

        <!-- Line -->
        <Rectangle Height=".65" Margin="8" Fill="{DynamicResource Brush.Border2}" VerticalAlignment="Center"/>

        <!-- Base Information -->
        <Grid RowDefinitions="24,Auto,Auto,Auto,Auto" ColumnDefinitions="96,*">
          <!-- SHA -->
          <TextBlock Grid.Row="0" Grid.Column="0" Classes="info_label" VerticalAlignment="Top" Margin="0,4,0,0" Text="{DynamicResource Text.CommitDetail.Info.SHA}" />
          <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" Height="24">
            <TextBlock Text="{Binding SHA}" Margin="12,0,4,0" VerticalAlignment="Center"/>

            <Button Classes="icon_button" Width="24" Cursor="Hand" Click="OnCopyCommitSHA" ToolTip.Tip="{DynamicResource Text.Copy}">
              <Path Width="12" Height="12" Data="{StaticResource Icons.Copy}"/>
            </Button>

            <Button Classes="icon_button" Width="24" Cursor="Hand" Click="OnOpenContainsIn" IsVisible="{Binding #ThisControl.SupportsContainsIn}" ToolTip.Tip="{DynamicResource Text.CommitDetail.Info.ContainsIn}">
              <Path Width="14" Height="14" Margin="0,1,0,0" Data="{StaticResource Icons.Relation}"/>
            </Button>

            <Button Classes="icon_button" Width="24" Cursor="Hand" Click="OnOpenWebLink" IsVisible="{Binding #ThisControl.WebLinks, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}" ToolTip.Tip="{DynamicResource Text.CommitDetail.Info.WebLinks}">
              <Path Width="12" Height="12" Data="{StaticResource Icons.Link}" Fill="{DynamicResource Brush.Link}"/>
            </Button>

            <ContentControl Content="{Binding #ThisControl.SignInfo}">
              <ContentControl.DataTemplates>
                <DataTemplate DataType="m:CommitSignInfo">
                  <Border Width="24" Background="Transparent">
                    <ToolTip.Tip>
                      <StackPanel Orientation="Vertical">
                        <TextBlock Margin="0,0,0,12" Text="{Binding ToolTip}"/>
                        <Grid ColumnDefinitions="Auto,8,Auto" RowDefinitions="Auto,Auto">
                          <TextBlock Grid.Row="0" Grid.Column="0" Text="{DynamicResource Text.CommitDetail.Info.Signer}" IsVisible="{Binding HasSigner}" VerticalAlignment="Center"/>
                          <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding Signer}" IsVisible="{Binding HasSigner}" VerticalAlignment="Center"/>
                          <TextBlock Grid.Row="1" Grid.Column="0" Text="{DynamicResource Text.CommitDetail.Info.Key}" VerticalAlignment="Center"/>
                          <TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding Key}" VerticalAlignment="Center"/>
                        </Grid>
                      </StackPanel>
                    </ToolTip.Tip>
                    <Path Width="14" Height="14" Data="{StaticResource Icons.Verified}" Fill="{Binding Brush}"/>
                  </Border>
                </DataTemplate>
              </ContentControl.DataTemplates>
            </ContentControl>
          </StackPanel>

          <!-- PARENTS -->
          <TextBlock Grid.Row="1" Grid.Column="0" Classes="info_label" VerticalAlignment="Top" Margin="0,4,0,0" Text="{DynamicResource Text.CommitDetail.Info.Parents}" IsVisible="{Binding Parents, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>
          <ItemsControl Grid.Row="1" Grid.Column="1" Height="24" Margin="12,0,0,0" ItemsSource="{Binding Parents}" IsVisible="{Binding Parents, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
            <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center"/>
              </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
              <DataTemplate>
                <TextBlock Text="{Binding Converter={x:Static c:StringConverters.ToShortSHA}}"
                           Foreground="DarkOrange"
                           TextDecorations="Underline"
                           Cursor="Hand"
                           Margin="0,0,16,0"
                           PointerEntered="OnSHAPointerEntered"
                           PointerPressed="OnSHAPressed"
                           ToolTip.ShowDelay="0">
                  <ToolTip.IsOpen>
                    <MultiBinding Converter="{x:Static BoolConverters.And}">
                      <Binding Path="$self.IsPointerOver"/>
                      <Binding Path="$self.(ToolTip.Tip)" Converter="{x:Static ObjectConverters.IsNotNull}"/>
                    </MultiBinding>
                  </ToolTip.IsOpen>

                  <TextBlock.DataTemplates>
                    <DataTemplate DataType="m:Commit">
                      <StackPanel MinWidth="400" Orientation="Vertical">
                        <Grid ColumnDefinitions="Auto,*,Auto">
                          <v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
                          <TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
                          <TextBlock Grid.Column="2" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
                        </Grid>

                        <TextBlock Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>
                      </StackPanel>
                    </DataTemplate>
                  </TextBlock.DataTemplates>
                </TextBlock>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>

          <!-- CHILDREN -->
          <TextBlock Grid.Row="2" Grid.Column="0" Classes="info_label" VerticalAlignment="Top" Margin="0,4,0,0" Text="{DynamicResource Text.CommitDetail.Info.Children}" IsVisible="{Binding #ThisControl.Children, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>
          <ItemsControl Grid.Row="2" Grid.Column="1" Margin="12,0,0,0" ItemsSource="{Binding #ThisControl.Children}" IsVisible="{Binding #ThisControl.Children, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
            <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" VerticalAlignment="Center" ItemHeight="24"/>
              </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
              <DataTemplate>
                <TextBlock Text="{Binding Converter={x:Static c:StringConverters.ToShortSHA}}"
                           Foreground="DarkOrange"
                           TextDecorations="Underline"
                           Cursor="Hand"
                           Margin="0,0,16,0"
                           PointerEntered="OnSHAPointerEntered"
                           PointerPressed="OnSHAPressed"
                           ToolTip.ShowDelay="0">
                  <ToolTip.IsOpen>
                    <MultiBinding Converter="{x:Static BoolConverters.And}">
                      <Binding Path="$self.IsPointerOver"/>
                      <Binding Path="$self.(ToolTip.Tip)" Converter="{x:Static ObjectConverters.IsNotNull}"/>
                    </MultiBinding>
                  </ToolTip.IsOpen>

                  <TextBlock.DataTemplates>
                    <DataTemplate DataType="m:Commit">
                      <StackPanel MinWidth="400" Orientation="Vertical">
                        <Grid ColumnDefinitions="Auto,*,Auto">
                          <v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
                          <TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
                          <TextBlock Grid.Column="2" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
                        </Grid>

                        <TextBlock Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>
                      </StackPanel>
                    </DataTemplate>
                  </TextBlock.DataTemplates>
                </TextBlock>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>

          <!-- REFS -->
          <TextBlock Grid.Row="3" Grid.Column="0" Classes="info_label" VerticalAlignment="Top" Margin="0,4,0,0" Text="{DynamicResource Text.CommitDetail.Info.Refs}" IsVisible="{Binding HasDecorators}"/>
          <Border Grid.Row="3" Grid.Column="1" Margin="12,0,0,0" MinHeight="24" IsVisible="{Binding HasDecorators}">
            <v:CommitRefsPresenter Foreground="{DynamicResource Brush.FG1}"
                                   FontSize="12"
                                   AllowWrap="True"
                                   Margin="0,4,0,0"
                                   UseGraphColor="False"/>
          </Border>

          <!-- Messages -->
          <TextBlock Grid.Row="4" Grid.Column="0" Classes="info_label" VerticalAlignment="Top" Margin="0,4,0,0" Text="{DynamicResource Text.CommitDetail.Info.Message}" />
          <v:CommitMessagePresenter Grid.Row="4" Grid.Column="1"
                                    Margin="12,4,8,0"
                                    FullMessage="{Binding #ThisControl.FullMessage}"
                                    HorizontalAlignment="Stretch"
                                    TextWrapping="Wrap">
            <ToolTip.IsOpen>
              <MultiBinding Converter="{x:Static BoolConverters.And}">
                <Binding Path="$self.IsPointerOver"/>
                <Binding Path="$self.(ToolTip.Tip)" Converter="{x:Static ObjectConverters.IsNotNull}"/>
              </MultiBinding>
            </ToolTip.IsOpen>

            <v:CommitMessagePresenter.DataTemplates>
              <DataTemplate DataType="m:Commit">
                <StackPanel MinWidth="400" Orientation="Vertical">
                  <Grid ColumnDefinitions="Auto,*,Auto">
                    <v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
                    <TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
                    <TextBlock Grid.Column="2" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
                  </Grid>

                  <TextBlock Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>
                </StackPanel>
              </DataTemplate>
            </v:CommitMessagePresenter.DataTemplates>
          </v:CommitMessagePresenter>
        </Grid>
      </StackPanel>
    </DataTemplate>
  </UserControl.DataTemplates>
</UserControl>
