<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.RevisionCompare"
             x:DataType="vm:RevisionCompare">
  <UserControl.DataTemplates>
    <DataTemplate DataType="m:Null">
      <Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="{DynamicResource Brush.Accent}" CornerRadius="4">
        <TextBlock Text="{DynamicResource Text.Worktree}" Margin="4,2" Foreground="#FFDDDDDD"/>
      </Border>
    </DataTemplate>

    <DataTemplate DataType="m:Commit">
      <Grid RowDefinitions="Auto,*">
        <Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto,Auto,Auto">
          <v:Avatar Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
          <TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
          <Border Grid.Column="2" Background="{DynamicResource Brush.Accent}" CornerRadius="4" IsVisible="{Binding IsCurrentHead}">
            <TextBlock Text="HEAD" Margin="4,0" Foreground="#FFDDDDDD"/>
          </Border>
          <TextBlock Grid.Column="3" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" Cursor="Hand" PointerPressed="OnPressedSHA" />
          <TextBlock Grid.Column="4" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
        </Grid>

        <TextBlock Grid.Row="1" Text="{Binding Subject}" Margin="0,6,0,0"/>
      </Grid>
    </DataTemplate>
  </UserControl.DataTemplates>

  <Grid RowDefinitions="Auto,*" Margin="4">
    <!-- Compare Revision Info -->
    <Grid Grid.Row="0" Margin="0,0,0,6" ColumnDefinitions="*,32,*,Auto">
      <!-- Base Revision -->
      <Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Diff.DeletedHighlight}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
        <ContentControl Content="{Binding StartPoint}"/>
      </Border>

      <!-- Swap Buttons -->
      <Button Grid.Column="1" Classes="icon_button" Command="{Binding Swap}" HorizontalAlignment="Center" ToolTip.Tip="{DynamicResource Text.Diff.SwapCommits}">
        <Path Width="16" Height="16" Data="{DynamicResource Icons.Compare}"/>
      </Button>

      <!-- Right Revision -->
      <Border Grid.Column="2" BorderBrush="{DynamicResource Brush.Diff.AddedHighlight}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
        <ContentControl Content="{Binding EndPoint}"/>
      </Border>

      <!-- Save As Patch Button -->
      <Button Grid.Column="3" Classes="icon_button" Width="32" Click="OnSaveAsPatch" IsVisible="{Binding CanSaveAsPatch}" ToolTip.Tip="{DynamicResource Text.Diff.SaveAsPatch}">
        <Path Width="16" Height="16" Data="{DynamicResource Icons.Diff}"/>
      </Button>
    </Grid>

    <!-- Changes View -->
    <Grid Grid.Row="1">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="256" MinWidth="200" MaxWidth="480"/>
        <ColumnDefinition Width="4"/>
        <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>

      <Grid Grid.Column="0" RowDefinitions="26,*,26">
        <!-- Search & Display Mode -->
        <Grid Grid.Row="0" ColumnDefinitions="*,18">
          <TextBox Grid.Column="0"
                   Height="26"
                   BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}"
                   Background="Transparent"
                   CornerRadius="4"
                   Watermark="{DynamicResource Text.CommitDetail.Changes.Search}"
                   Text="{Binding SearchFilter, Mode=TwoWay}">
            <TextBox.InnerLeftContent>
              <Path Width="14" Height="14" Margin="4,0,0,0" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Search}"/>
            </TextBox.InnerLeftContent>

            <TextBox.InnerRightContent>
              <Button Classes="icon_button"
                      IsVisible="{Binding SearchFilter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
                      Command="{Binding ClearSearchFilter}">
                <Path Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Clear}"/>
              </Button>
            </TextBox.InnerRightContent>
          </TextBox>

          <v:ChangeViewModeSwitcher Grid.Column="1"
                                    Width="14" Height="14"
                                    HorizontalAlignment="Right"
                                    ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=CommitChangeViewMode, Mode=TwoWay}"/>
        </Grid>

        <!-- Changes -->
        <Border Grid.Row="1" Margin="0,4,0,0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}">
          <v:ChangeCollectionView ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=CommitChangeViewMode}"
                                  EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
                                  Changes="{Binding VisibleChanges}"
                                  SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
                                  ContextRequested="OnChangeContextRequested"
                                  KeyDown="OnChangeCollectionViewKeyDown"/>
        </Border>

        <!-- Loading Status Icon -->
        <v:LoadingIcon Grid.Row="1"
                       Width="48" Height="48"
                       HorizontalAlignment="Center" VerticalAlignment="Center"
                       IsVisible="{Binding IsLoading}"/>

        <!-- Summary -->
        <Border Grid.Row="2" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1,0,1,1" Background="Transparent">
          <TextBlock Margin="4,0,0,0"
                     Foreground="{DynamicResource Brush.FG2}"
                     HorizontalAlignment="Left" VerticalAlignment="Center">
            <Run Text="{Binding TotalChanges, Mode=OneWay}" FontWeight="Bold"/>
            <Run Text="{DynamicResource Text.CommitDetail.Changes.Count}"/>
          </TextBlock>
        </Border>
      </Grid>

      <GridSplitter Grid.Column="1"
                    MinWidth="1"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                    Background="Transparent"/>

      <Grid Grid.Column="2">
        <Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
          <StackPanel Orientation="Vertical" VerticalAlignment="Center">
            <Path Width="64" Height="64" Data="{StaticResource Icons.Diff}" Fill="{DynamicResource Brush.FG2}"/>
            <TextBlock Margin="0,16,0,0"
                       Text="{DynamicResource Text.Diff.Welcome}"
                       FontSize="18" FontWeight="Bold"
                       Foreground="{DynamicResource Brush.FG2}"
                       HorizontalAlignment="Center"/>
          </StackPanel>
        </Border>

        <ContentControl Content="{Binding DiffContext}">
          <ContentControl.DataTemplates>
            <DataTemplate DataType="vm:DiffContext">
              <v:DiffView/>
            </DataTemplate>
          </ContentControl.DataTemplates>
        </ContentControl>
      </Grid>
    </Grid>
  </Grid>
</UserControl>
