<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:vm="using:SourceGit.ViewModels"
             xmlns:v="using:SourceGit.Views"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="SourceGit.Views.TextDiffView"
             x:DataType="vm:TextDiffContext"
             Background="{DynamicResource Brush.Contents}">
  <Grid>
    <ContentControl Content="{Binding}">
      <ContentControl.DataTemplates>
        <DataTemplate DataType="vm:CombinedTextDiff">
          <Grid ColumnDefinitions="*,1,12">
            <v:CombinedTextDiffPresenter Grid.Column="0"
                                         FileName="{Binding Option.Path}"
                                         Foreground="{DynamicResource Brush.FG1}"
                                         LineBrush="{DynamicResource Brush.Border2}"
                                         EmptyContentBackground="{DynamicResource Brush.Diff.EmptyBG}"
                                         AddedContentBackground="{DynamicResource Brush.Diff.AddedBG}"
                                         DeletedContentBackground="{DynamicResource Brush.Diff.DeletedBG}"
                                         AddedHighlightBrush="{DynamicResource Brush.Diff.AddedHighlight}"
                                         DeletedHighlightBrush="{DynamicResource Brush.Diff.DeletedHighlight}"
                                         IndicatorForeground="{DynamicResource Brush.FG2}"
                                         BlockBorderHighlightBrush="{DynamicResource Brush.Diff.BlockBorderHighlight}"
                                         FontFamily="{DynamicResource Fonts.Monospace}"
                                         FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
                                         TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
                                         UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
                                         WordWrap="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableDiffViewWordWrap}"
                                         ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"
                                         SelectedChunk="{Binding SelectedChunk, Mode=TwoWay}"
                                         BlockNavigation="{Binding BlockNavigation, Mode=OneWay}"/>

            <Rectangle Grid.Column="1" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>

            <v:TextDiffViewMinimap Grid.Column="2"
                                   DisplayRange="{Binding DisplayRange, Mode=OneWay}"
                                   AddedLineBrush="{DynamicResource Brush.Diff.AddedBG}"
                                   DeletedLineBrush="{DynamicResource Brush.Diff.DeletedBG}"/>
          </Grid>
        </DataTemplate>

        <DataTemplate DataType="vm:TwoSideTextDiff">
          <Grid ColumnDefinitions="*,1,*,1,12">
            <v:SingleSideTextDiffPresenter Grid.Column="0"
                                           IsOld="True"
                                           FileName="{Binding Option.Path}"
                                           Foreground="{DynamicResource Brush.FG1}"
                                           LineBrush="{DynamicResource Brush.Border2}"
                                           EmptyContentBackground="{DynamicResource Brush.Diff.EmptyBG}"
                                           AddedContentBackground="{DynamicResource Brush.Diff.AddedBG}"
                                           DeletedContentBackground="{DynamicResource Brush.Diff.DeletedBG}"
                                           AddedHighlightBrush="{DynamicResource Brush.Diff.AddedHighlight}"
                                           DeletedHighlightBrush="{DynamicResource Brush.Diff.DeletedHighlight}"
                                           IndicatorForeground="{DynamicResource Brush.FG2}"
                                           BlockBorderHighlightBrush="{DynamicResource Brush.Diff.BlockBorderHighlight}"
                                           FontFamily="{DynamicResource Fonts.Monospace}"
                                           FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
                                           TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
                                           UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
                                           WordWrap="False"
                                           ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"
                                           SelectedChunk="{Binding SelectedChunk, Mode=TwoWay}"
                                           BlockNavigation="{Binding BlockNavigation, Mode=OneWay}"/>

            <Rectangle Grid.Column="1" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>

            <v:SingleSideTextDiffPresenter Grid.Column="2"
                                           IsOld="False"
                                           FileName="{Binding Option.Path}"
                                           Foreground="{DynamicResource Brush.FG1}"
                                           LineBrush="{DynamicResource Brush.Border2}"
                                           EmptyContentBackground="{DynamicResource Brush.Diff.EmptyBG}"
                                           AddedContentBackground="{DynamicResource Brush.Diff.AddedBG}"
                                           DeletedContentBackground="{DynamicResource Brush.Diff.DeletedBG}"
                                           AddedHighlightBrush="{DynamicResource Brush.Diff.AddedHighlight}"
                                           DeletedHighlightBrush="{DynamicResource Brush.Diff.DeletedHighlight}"
                                           IndicatorForeground="{DynamicResource Brush.FG2}"
                                           BlockBorderHighlightBrush="{DynamicResource Brush.Diff.BlockBorderHighlight}"
                                           FontFamily="{DynamicResource Fonts.Monospace}"
                                           FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
                                           TabWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorTabWidth}"
                                           UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
                                           WordWrap="False"
                                           ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"
                                           SelectedChunk="{Binding SelectedChunk, Mode=TwoWay}"
                                           BlockNavigation="{Binding BlockNavigation, Mode=OneWay}"/>

            <Rectangle Grid.Column="3" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>

            <v:TextDiffViewMinimap Grid.Column="4"
                                   DisplayRange="{Binding DisplayRange, Mode=OneWay}"
                                   AddedLineBrush="{DynamicResource Brush.Diff.AddedBG}"
                                   DeletedLineBrush="{DynamicResource Brush.Diff.DeletedBG}"/>
          </Grid>
        </DataTemplate>
      </ContentControl.DataTemplates>
    </ContentControl>

    <StackPanel x:Name="Popup" IsVisible="False" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" Effect="drop-shadow(0 0 8 #80000000)">
      <Button Classes="flat" BorderBrush="{DynamicResource Brush.FlatButton.FloatingBorder}" Click="OnStageChunk" HotKey="{OnPlatform Ctrl+S, macOS=⌘+S}" IsVisible="{Binding Option.IsUnstaged}">
        <TextBlock>
          <Run Text="{DynamicResource Text.Hunk.Stage}"/>
          <Run Text=" "/>
          <Run Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"
               FontWeight="Normal"
               FontSize="11"
               Text="{OnPlatform Ctrl+S, macOS=⌘+S}"/>
        </TextBlock>
      </Button>

      <Button Classes="flat" BorderBrush="{DynamicResource Brush.FlatButton.FloatingBorder}" Click="OnUnstageChunk" HotKey="{OnPlatform Ctrl+U, macOS=⌘+U}" IsVisible="{Binding Option.IsUnstaged, Converter={x:Static BoolConverters.Not}}">
        <TextBlock>
          <Run Text="{DynamicResource Text.Hunk.Unstage}"/>
          <Run Text=" "/>
          <Run Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"
               FontWeight="Normal"
               FontSize="11"
               Text="{OnPlatform Ctrl+U, macOS=⌘+U}"/>
        </TextBlock>
      </Button>

      <Button Classes="flat" BorderBrush="{DynamicResource Brush.FlatButton.FloatingBorder}" Margin="8,0,0,0" HotKey="{OnPlatform Ctrl+D, macOS=⌘+D}" Click="OnDiscardChunk" IsVisible="{Binding Option.IsUnstaged}">
        <TextBlock>
          <Run Text="{DynamicResource Text.Hunk.Discard}"/>
          <Run Text=" "/>
          <Run Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"
               FontWeight="Normal"
               FontSize="11"
               Text="{OnPlatform Ctrl+D, macOS=⌘+D}"/>
        </TextBlock>
      </Button>
    </StackPanel>
  </Grid>
</UserControl>
