<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.DiffView"
             x:DataType="vm:DiffContext">
  <Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}" Background="{DynamicResource Brush.Window}">
    <Grid RowDefinitions="26,*">
      <!-- Toolbar -->
      <Border Grid.Row="0" BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}">
        <Grid ColumnDefinitions="Auto,Auto,Auto,*,Auto">
          <!-- File Icon -->
          <Path Grid.Column="0" Width="12" Height="12" Data="{StaticResource Icons.File}" Margin="8,0,0,0"/>

          <!-- File Mode Change -->
          <Border Grid.Column="1"
                  Margin="4,0,0,0"
                  Height="18"
                  CornerRadius="4"
                  VerticalAlignment="Center"
                  Background="{DynamicResource Brush.Badge}"
                  IsVisible="{Binding FileModeChange, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
                  ToolTip.Tip="{DynamicResource Text.Diff.FileModeChanged}">
            <TextBlock FontSize="10" HorizontalAlignment="Center" Margin="4,0" Text="{Binding FileModeChange}" Foreground="{DynamicResource Brush.BadgeFG}"/>
          </Border>
          
          <!-- Line Changes Summary -->
          <ContentControl Grid.Column="2" Margin="4,0,0,0" Content="{Binding Content, Mode=OneWay}" IsVisible="{Binding IsTextDiff}">
            <ContentControl.DataTemplates>
              <DataTemplate DataType="vm:TextDiffContext">
                <StackPanel Orientation="Horizontal" Spacing="4">
                  <TextBlock FontSize="11"
                             Foreground="Green"
                             Text="{Binding Data.AddedLines, Mode=OneWay, StringFormat=+{0}}"
                             IsVisible="{Binding Data.AddedLines, Converter={x:Static c:IntConverters.IsGreaterThanZero}}"/>
                  <TextBlock FontSize="11"
                             Foreground="Red"
                             Text="{Binding Data.DeletedLines, Mode=OneWay, StringFormat=-{0}}"
                             IsVisible="{Binding Data.DeletedLines, Converter={x:Static c:IntConverters.IsGreaterThanZero}}"/>
                </StackPanel>
              </DataTemplate>
            </ContentControl.DataTemplates>
          </ContentControl>

          <!-- Title -->
          <TextBlock Grid.Column="3"
                     Margin="4,0,0,0"
                     Text="{Binding Title}"
                     ToolTip.Tip="{Binding Title}"
                     FontSize="11"
                     TextTrimming="CharacterEllipsis"/>

          <!-- Toolbar Buttons -->
          <StackPanel Grid.Column="4" Margin="8,0,0,0" Orientation="Horizontal" VerticalAlignment="Center">
            <Button Classes="icon_button"
                    Width="28"
                    Click="OnGotoFirstChange"
                    IsVisible="{Binding IsTextDiff}"
                    HotKey="{OnPlatform Ctrl+Alt+Home, macOS=⌘+Alt+Home}">
              <ToolTip.Tip>
                <TextBlock>
                  <Run Text="{DynamicResource Text.Diff.First}"/>
                  <Run Text=" "/>
                  <Run Text="{OnPlatform Ctrl+Alt+Home, macOS=⌘+⌥+Home}" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
                </TextBlock>
              </ToolTip.Tip>
              <Path Width="12" Height="12" Stretch="Uniform" Margin="0,6,0,0" Data="{StaticResource Icons.Top}"/>
            </Button>

            <Button Classes="icon_button"
                    Width="28"
                    Click="OnGotoPrevChange"
                    IsVisible="{Binding IsTextDiff}"
                    HotKey="{OnPlatform Ctrl+Alt+Up, macOS=⌘+Alt+Up}">
              <ToolTip.Tip>
                <TextBlock>
                  <Run Text="{DynamicResource Text.Diff.Prev}"/>
                  <Run Text=" "/>
                  <Run Text="{OnPlatform Ctrl+Alt+Up, macOS=⌘+⌥+Up}" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
                </TextBlock>
              </ToolTip.Tip>
              <Path Width="12" Height="12" Stretch="Uniform" Margin="0,6,0,0" Data="{StaticResource Icons.Up}"/>
            </Button>

            <ContentControl Content="{Binding Content, Mode=OneWay}" IsVisible="{Binding IsTextDiff}">
              <ContentControl.DataTemplates>
                <DataTemplate DataType="vm:TextDiffContext">
                  <TextBlock Margin="0,0,0,0" FontSize="11" Text="{Binding BlockNavigation.Indicator}"/>
                </DataTemplate>
              </ContentControl.DataTemplates>
            </ContentControl>

            <Button Classes="icon_button"
                    Width="28"
                    Click="OnGotoNextChange"
                    IsVisible="{Binding IsTextDiff}"
                    HotKey="{OnPlatform Ctrl+Alt+Down, macOS=⌘+Alt+Down}">
              <ToolTip.Tip>
                <TextBlock>
                  <Run Text="{DynamicResource Text.Diff.Next}"/>
                  <Run Text=" "/>
                  <Run Text="{OnPlatform Ctrl+Alt+Down, macOS=⌘+⌥+Down}" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
                </TextBlock>
              </ToolTip.Tip>
              <Path Width="12" Height="12" Stretch="Uniform" Margin="0,6,0,0" Data="{StaticResource Icons.Down}"/>
            </Button>

            <Button Classes="icon_button"
                    Width="28"
                    Click="OnGotoLastChange"
                    IsVisible="{Binding IsTextDiff}"
                    HotKey="{OnPlatform Ctrl+Alt+End, macOS=⌘+Alt+End}">
              <ToolTip.Tip>
                <TextBlock>
                  <Run Text="{DynamicResource Text.Diff.Last}"/>
                  <Run Text=" "/>
                  <Run Text="{OnPlatform Ctrl+Alt+End, macOS=⌘+⌥+End}" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
                </TextBlock>
              </ToolTip.Tip>
              <Path Width="12" Height="12" Stretch="Uniform" Margin="0,6,0,0" Data="{StaticResource Icons.Bottom}"/>
            </Button>

            <Button Classes="icon_button"
                    Width="28"
                    Command="{Binding IncrUnified}"
                    IsVisible="{Binding IsTextDiff}"
                    IsEnabled="{Binding ShowEntireFile, Mode=OneWay, Converter={x:Static BoolConverters.Not}}"
                    ToolTip.Tip="{DynamicResource Text.Diff.VisualLines.Incr}">
              <Path Width="12" Height="12" Stretch="Uniform" Margin="0,6,0,0" Data="{StaticResource Icons.Lines.Incr}"/>
            </Button>

            <Button Classes="icon_button"
                    Width="28"
                    Command="{Binding DecrUnified}"
                    IsVisible="{Binding IsTextDiff}"
                    ToolTip.Tip="{DynamicResource Text.Diff.VisualLines.Decr}">
              <Button.IsEnabled>
                <MultiBinding Converter="{x:Static BoolConverters.And}">
                  <Binding Path="UnifiedLines" Mode="OneWay" Converter="{x:Static c:IntConverters.IsGreaterThanFour}"/>
                  <Binding Path="ShowEntireFile" Mode="OneWay" Converter="{x:Static BoolConverters.Not}"/>
                </MultiBinding>
              </Button.IsEnabled>
              <Path Width="12" Height="12" Stretch="Uniform" Margin="0,6,0,0" Data="{StaticResource Icons.Lines.Decr}"/>
            </Button>

            <ToggleButton Classes="line_path"
                          Width="28"
                          IsChecked="{Binding ShowEntireFile, Mode=TwoWay}"
                          IsVisible="{Binding IsTextDiff}"
                          ToolTip.Tip="{DynamicResource Text.Diff.VisualLines.All}">
              <Path Width="13" Height="13" Data="{StaticResource Icons.Lines.All}" Margin="0,3,0,0"/>
            </ToggleButton>

            <ToggleButton Classes="line_path"
                          Width="28"
                          Background="Transparent"
                          IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting, Mode=TwoWay}"
                          IsVisible="{Binding IsTextDiff}"
                          ToolTip.Tip="{DynamicResource Text.Diff.SyntaxHighlight}">
              <Path Width="13" Height="13" Data="{StaticResource Icons.SyntaxHighlight}" Margin="0,3,0,0"/>
            </ToggleButton>

            <ToggleButton Classes="line_path"
                          Width="28"
                          IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableDiffViewWordWrap, Mode=TwoWay}"
                          ToolTip.Tip="{DynamicResource Text.Diff.ToggleWordWrap}">
              <ToggleButton.IsVisible>
                <MultiBinding Converter="{x:Static BoolConverters.And}">
                  <Binding Path="IsTextDiff" Mode="OneWay"/>
                  <Binding Path="UseSideBySide" Mode="OneWay" Converter="{x:Static BoolConverters.Not}"/>
                </MultiBinding>
              </ToggleButton.IsVisible>

              <Path Width="12" Height="12" Data="{StaticResource Icons.WordWrap}" Margin="0,2,0,0"/>
            </ToggleButton>

            <ToggleButton Classes="line_path"
                          Width="28"
                          IsChecked="{Binding IgnoreWhitespace, Mode=TwoWay}"
                          ToolTip.Tip="{DynamicResource Text.Diff.IgnoreWhitespace}">
              <Path Width="14" Height="14" Stretch="Uniform" Data="{StaticResource Icons.Whitespace}"/>
            </ToggleButton>

            <ToggleButton Classes="line_path"
                          Width="28"
                          IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView, Mode=TwoWay}"
                          IsVisible="{Binding IsTextDiff}"
                          ToolTip.Tip="{DynamicResource Text.Diff.ShowHiddenSymbols}">
              <Path Width="11" Height="11" Stretch="Uniform" Data="{StaticResource Icons.HiddenSymbol}" Margin="0,1,0,0"/>
            </ToggleButton>

            <ToggleButton Classes="line_path"
                          Width="28" Height="18"
                          IsChecked="{Binding UseSideBySide, Mode=TwoWay}"
                          IsVisible="{Binding IsTextDiff}"
                          ToolTip.Tip="{DynamicResource Text.Diff.SideBySide}">
              <Path Width="12" Height="12" Data="{StaticResource Icons.Layout}" Margin="0,2,0,0"/>
            </ToggleButton>

            <Button Classes="icon_button"
                    Width="28"
                    Command="{Binding OpenExternalMergeTool}"
                    HotKey="{OnPlatform Ctrl+Shift+D, macOS=⌘+Shift+D}">
              <ToolTip.Tip>
                <TextBlock>
                  <Run Text="{DynamicResource Text.Diff.UseMerger}"/>
                  <Run Text=" "/>
                  <Run Text="{OnPlatform Ctrl+Shift+D, macOS=⌘+⇧+D}" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
                </TextBlock>
              </ToolTip.Tip>
              <Path Width="12" Height="12" Stretch="Uniform" Data="{StaticResource Icons.OpenWith}"/>
            </Button>
          </StackPanel>
        </Grid>
      </Border>

      <!-- Diff Contents -->
      <ContentControl Grid.Row="1" Content="{Binding Content}">
        <ContentControl.DataTemplates>
          <!-- Binary Diff -->
          <DataTemplate DataType="m:BinaryDiff">
            <StackPanel Orientation="Vertical" VerticalAlignment="Center">
              <TextBlock Text="{DynamicResource Text.Diff.Binary}"
                         Margin="0,0,0,32"
                         FontSize="18" FontWeight="Bold"
                         Foreground="{DynamicResource Brush.FG2}"
                         HorizontalAlignment="Center"/>
              <Path Width="64" Height="64" Data="{StaticResource Icons.Binary}" Fill="{DynamicResource Brush.FG2}"/>
              <Grid Margin="0,16,0,0" HorizontalAlignment="Center" RowDefinitions="32,32" ColumnDefinitions="Auto,Auto">
                <Border Grid.Row="0" Grid.Column="0" Height="16" Background="{DynamicResource Brush.Badge}" CornerRadius="8" VerticalAlignment="Center">
                  <TextBlock Text="{DynamicResource Text.Diff.Old}" Margin="8,0" FontSize="10" Foreground="{DynamicResource Brush.BadgeFG}"/>
                </Border>

                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding OldSize, Converter={x:Static c:LongConverters.ToFileSize}}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>

                <Border Grid.Row="1" Grid.Column="0" Height="16" Background="Green" CornerRadius="8" VerticalAlignment="Center">
                  <TextBlock Text="{DynamicResource Text.Diff.New}" Margin="8,0" FontSize="10" Foreground="White"/>
                </Border>

                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding NewSize, Converter={x:Static c:LongConverters.ToFileSize}}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
              </Grid>
            </StackPanel>
          </DataTemplate>

          <!-- LFS Diff -->
          <DataTemplate DataType="m:LFSDiff">
            <StackPanel Orientation="Vertical" VerticalAlignment="Center">
              <TextBlock Text="{DynamicResource Text.Diff.LFS}"
                         Margin="0,0,0,32"
                         FontSize="18" FontWeight="Bold"
                         Foreground="{DynamicResource Brush.FG2}"
                         HorizontalAlignment="Center"/>
              <Path Width="64" Height="64" Data="{StaticResource Icons.LFS}" Fill="{DynamicResource Brush.FG2}"/>
              <Grid Margin="0,16,0,0" HorizontalAlignment="Center" RowDefinitions="32,32" ColumnDefinitions="Auto,Auto">
                <Border Grid.Row="0" Grid.Column="0" Height="16" Background="{DynamicResource Brush.Badge}" CornerRadius="8" VerticalAlignment="Center">
                  <TextBlock Text="{DynamicResource Text.Diff.Old}" Margin="8,0" FontSize="10" Foreground="{DynamicResource Brush.BadgeFG}"/>
                </Border>

                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Old.Size, Converter={x:Static c:LongConverters.ToFileSize}}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>

                <Border Grid.Row="1" Grid.Column="0" Height="16" Background="Green" CornerRadius="8" VerticalAlignment="Center">
                  <TextBlock Text="{DynamicResource Text.Diff.New}" Margin="8,0" FontSize="10" Foreground="White"/>
                </Border>

                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding New.Size, Converter={x:Static c:LongConverters.ToFileSize}}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Right" FontSize="16" Margin="8,0"/>
              </Grid>
            </StackPanel>
          </DataTemplate>

          <!-- Submodule Diff -->
          <DataTemplate DataType="m:SubmoduleDiff">
            <Grid Background="{DynamicResource Brush.Contents}" RowDefinitions="Auto,*">
              <TextBlock Grid.Row="0"
                         Text="{DynamicResource Text.Diff.Submodule}"
                         Margin="8,8,8,8"
                         FontSize="18" FontWeight="Bold"
                         Foreground="{DynamicResource Brush.FG2}"
                         HorizontalAlignment="Center"/>

              <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                <StackPanel Orientation="Vertical" Margin="8,0">
                  <Border Height="16" HorizontalAlignment="Center" Background="Red" CornerRadius="8" IsVisible="{Binding New, Converter={x:Static ObjectConverters.IsNull}}">
                    <TextBlock Text="{DynamicResource Text.Diff.Submodule.Deleted}" Margin="8,0" FontSize="10" Foreground="White"/>
                  </Border>

                  <Border Margin="0,8"
                          BorderThickness="1" BorderBrush="{DynamicResource Brush.Border1}"
                          Background="{DynamicResource Brush.Window}"
                          IsVisible="{Binding Old, Converter={x:Static ObjectConverters.IsNotNull}}">
                    <ContentControl Content="{Binding Old}">
                      <ContentControl.DataTemplates>
                        <DataTemplate DataType="m:RevisionSubmodule">
                          <v:CommitBaseInfo Margin="0,4,8,6" Content="{Binding Commit}" FullMessage="{Binding FullMessage}"/>
                        </DataTemplate>
                      </ContentControl.DataTemplates>
                    </ContentControl>
                  </Border>

                  <Path Width="16" Height="16" Data="{StaticResource Icons.DoubleDown}" HorizontalAlignment="Center">
                    <Path.IsVisible>
                      <MultiBinding Converter="{x:Static BoolConverters.And}">
                        <Binding Path="Old" Converter="{x:Static ObjectConverters.IsNotNull}"/>
                        <Binding Path="New" Converter="{x:Static ObjectConverters.IsNotNull}"/>
                      </MultiBinding>
                    </Path.IsVisible>
                  </Path>

                  <Border Height="16" HorizontalAlignment="Center" Background="Green" CornerRadius="8" IsVisible="{Binding Old, Converter={x:Static ObjectConverters.IsNull}}">
                    <TextBlock Text="{DynamicResource Text.Diff.Submodule.New}" Margin="8,0" FontSize="10" Foreground="White"/>
                  </Border>

                  <Border Margin="0,8"
                          BorderThickness="1" BorderBrush="Green"
                          Background="{DynamicResource Brush.Window}"
                          IsVisible="{Binding New, Converter={x:Static ObjectConverters.IsNotNull}}">
                    <ContentControl Content="{Binding New}">
                      <ContentControl.DataTemplates>
                        <DataTemplate DataType="m:RevisionSubmodule">
                          <StackPanel Orientation="Vertical">
                            <TextBlock Margin="0,8,0,4"
                                       HorizontalAlignment="Center"
                                       Foreground="OrangeRed"
                                       FontFamily="{DynamicResource Fonts.Monospace}"
                                       Text="{Binding UncommittedChanges, Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Diff.Submodule.UncommittedChanges'}"
                                       IsVisible="{Binding UncommittedChanges, Converter={x:Static c:IntConverters.IsGreaterThanZero}}"/>
                            <v:CommitBaseInfo Margin="0,4,8,6" Content="{Binding Commit}" FullMessage="{Binding FullMessage}"/>
                          </StackPanel>
                        </DataTemplate>
                      </ContentControl.DataTemplates>
                    </ContentControl>
                  </Border>
                  
                  <Button Classes="flat"
                          Margin="0,8"
                          HorizontalAlignment="Center"
                          Content="{DynamicResource Text.SubmoduleRevisionCompare.OpenDetails}"
                          Click="OnOpenSubmoduleRevisionCompare"
                          IsEnabled="{Binding CanOpenDetails, Mode=OneWay}"/>
                </StackPanel>
              </ScrollViewer>
            </Grid>
          </DataTemplate>

          <!-- Image Diff -->
          <DataTemplate DataType="m:ImageDiff">
            <v:ImageDiffView/>
          </DataTemplate>

          <!-- LFS Image Diff -->
          <DataTemplate DataType="vm:LFSImageDiff">
            <TabControl Margin="0,8,0,0" SelectedIndex="{Binding Source={x:Static vm:Preferences.Instance}, Path=LFSImageActiveIdx, Mode=TwoWay}">
              <TabControl.Styles>
                <Style Selector="TabControl /template/ ItemsPresenter#PART_ItemsPresenter > WrapPanel">
                  <Setter Property="HorizontalAlignment" Value="Center"/>
                </Style>
              </TabControl.Styles>

              <TabItem>
                <TabItem.Header>
                  <TextBlock Text="LFS" FontWeight="Bold"/>
                </TabItem.Header>

                <ContentControl Content="{Binding LFS}"/>
              </TabItem>

              <TabItem>
                <TabItem.Header>
                  <TextBlock Text="IMAGE" FontWeight="Bold"/>
                </TabItem.Header>

                <ContentControl Content="{Binding Image}"/>
              </TabItem>
            </TabControl>
          </DataTemplate>

          <!-- Text Diff -->
          <DataTemplate DataType="vm:TextDiffContext">
            <v:TextDiffView SelectedChunk="{Binding SelectedChunk, Mode=OneWay}"/>
          </DataTemplate>

          <!-- Empty or only EOL changes -->
          <DataTemplate DataType="m:NoOrEOLChange">
            <StackPanel Orientation="Vertical" VerticalAlignment="Center">
              <Path Width="64" Height="64" Data="{StaticResource Icons.Check}" Fill="{DynamicResource Brush.FG2}"/>
              <TextBlock Margin="0,16,0,0"
                         Text="{DynamicResource Text.Diff.NoChange}"
                         FontSize="18" FontWeight="Bold"
                         Foreground="{DynamicResource Brush.FG2}"
                         HorizontalAlignment="Center"/>
            </StackPanel>
          </DataTemplate>
        </ContentControl.DataTemplates>
      </ContentControl>
    </Grid>
  </Border>
</UserControl>
