<v:ChromelessWindow 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.FileHistories"
                    x:DataType="vm:FileHistories"
                    x:Name="ThisControl"
                    Icon="/App.ico"
                    Title="{DynamicResource Text.FileHistory}"
                    MinWidth="1280" MinHeight="720">
  <Grid RowDefinitions="Auto,28,1,*">
    <!-- TitleBar -->
    <Grid Grid.Row="0" Height="28" IsVisible="{Binding !#ThisControl.UseSystemWindowFrame}">
      <!-- Bottom border -->
      <Border Background="{DynamicResource Brush.TitleBar}"
              BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
              DoubleTapped="MaximizeOrRestoreWindow"
              PointerPressed="BeginMoveWindow"/>

      <Path Width="12" Height="12"
            Margin="10,0,0,0"
            HorizontalAlignment="Left"
            Data="{StaticResource Icons.Histories}"
            IsVisible="{OnPlatform True, macOS=False}"/>

      <TextBlock Classes="bold"
                 Text="{DynamicResource Text.FileHistory}"
                 HorizontalAlignment="Center" VerticalAlignment="Center"
                 IsHitTestVisible="False"/>

      <!-- Caption Buttons (Windows/Linux) -->
      <v:CaptionButtons HorizontalAlignment="Right" IsVisible="{OnPlatform True, macOS=False}"/>
    </Grid>

    <!-- Info -->
    <Grid Grid.Row="1" ColumnDefinitions="Auto,*" Background="{DynamicResource Brush.Window}">
      <Path Grid.Column="0" Width="14" Height="14" Margin="8,0,6,0" Data="{StaticResource Icons.File}"/>
      <TextBlock Grid.Column="1" Text="{Binding Title, Mode=OneWay}"/>
    </Grid>

    <!-- Line -->
    <Rectangle Grid.Row="2" Height="0.8" HorizontalAlignment="Stretch" VerticalAlignment="Top" Fill="{DynamicResource Brush.Border0}"/>

    <!-- Body -->
    <Grid Grid.Row="3">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300" MinWidth="300" MaxWidth="600"/>
        <ColumnDefinition Width="4"/>
        <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>

      <ListBox Grid.Column="0"
               Background="{DynamicResource Brush.Contents}"
               BorderThickness="1"
               Margin="8,4,4,8"
               BorderBrush="{DynamicResource Brush.Border2}"
               ItemsSource="{Binding Revisions, Mode=OneWay}"
               SelectionMode="Multiple"
               SelectionChanged="OnRevisionsSelectionChanged"
               PropertyChanged="OnRevisionsPropertyChanged"
               ScrollViewer.HorizontalScrollBarVisibility="Disabled"
               ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ListBox.Styles>
          <Style Selector="ListBoxItem">
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Height" Value="50"/>
          </Style>
        </ListBox.Styles>

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

        <ListBox.ItemTemplate>
          <DataTemplate DataType="m:FileVersion">
            <Border BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="0,0,0,1" Padding="4">
              <Grid RowDefinitions="Auto,*">
                <Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto,96">
                  <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" TextTrimming="CharacterEllipsis"/>
                  <TextBlock Grid.Column="2"
                             Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
                             Cursor="Hand"
                             Background="Transparent"
                             Foreground="DarkOrange"
                             TextDecorations="Underline"
                             Margin="8,0,0,0"
                             PointerPressed="OnPressCommitSHA"/>
                  <v:DateTimePresenter Grid.Column="3"
                                       Margin="8,0,0,0"
                                       ShowDateOnly="True"
                                       Timestamp="{Binding AuthorTime}"
                                       Foreground="{DynamicResource Brush.FG2}"
                                       HorizontalAlignment="Right"/>
                </Grid>

                <Border Grid.Row="1" Background="Transparent" DataContextChanged="OnCommitSubjectDataContextChanged" PointerMoved="OnCommitSubjectPointerMoved">
                  <TextBlock Text="{Binding Subject}" VerticalAlignment="Bottom"/>
                </Border>
              </Grid>
            </Border>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

      <GridSplitter Grid.Column="1"
                    MinWidth="1"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                    Background="Transparent"
                    BorderThickness="1,0,0,0"
                    BorderBrush="{DynamicResource Brush.Border0}"
                    Focusable="False"/>

      <ContentControl Grid.Column="2" Content="{Binding ViewContent}">
        <ContentControl.DataTemplates>
          <DataTemplate DataType="vm:FileHistoriesSingleRevision">
            <Grid RowDefinitions="Auto,*,Auto">
              <StackPanel Grid.Row="0" Margin="0,8" Height="28" HorizontalAlignment="Center" Orientation="Horizontal">
                <RadioButton Classes="switch_button"
                             GroupName="SearchGroup"
                             IsChecked="{Binding IsDiffMode, Mode=OneWay}">
                  <TextBlock Margin="16,0" Text="{DynamicResource Text.FileHistory.FileChange}" FontWeight="Bold"/>
                </RadioButton>

                <RadioButton Classes="switch_button"
                             GroupName="SearchGroup"
                             IsChecked="{Binding !IsDiffMode, Mode=TwoWay}">
                  <TextBlock Margin="16,0" Text="{DynamicResource Text.FileHistory.FileContent}" FontWeight="Bold"/>
                </RadioButton>
              </StackPanel>

              <ContentControl Grid.Row="1" Margin="4,4,8,8" Content="{Binding ViewContent}">
                <ContentControl.DataTemplates>
                  <DataTemplate DataType="vm:DiffContext">
                    <v:DiffView/>
                  </DataTemplate>

                  <DataTemplate DataType="vm:FileHistoriesRevisionFile">
                    <Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
                      <Grid RowDefinitions="26,*">
                        <Border Grid.Row="0"
                                BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border2}"
                                Background="{DynamicResource Brush.Window}">
                          <Grid ColumnDefinitions="Auto,*,Auto,Auto">
                            <Path Grid.Column="0" Width="12" Height="12" Data="{StaticResource Icons.File}" Margin="8,0,0,0"/>
                            <TextBlock Grid.Column="1"
                                       Margin="4,0,0,0"
                                       Text="{Binding Path}"
                                       FontSize="11"
                                       TextTrimming="CharacterEllipsis"/>
                            <ToggleButton Grid.Column="2"
                                          Classes="line_path"
                                          Width="28"
                                          Background="Transparent"
                                          IsChecked="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting, Mode=TwoWay}"
                                          ToolTip.Tip="{DynamicResource Text.Diff.SyntaxHighlight}">
                              <ToggleButton.IsVisible>
                                <Binding Path="Content"
                                         Mode="OneWay"
                                         Converter="{x:Static c:ObjectConverters.IsTypeOf}"
                                         ConverterParameter="{x:Type m:RevisionTextFile}"/>
                              </ToggleButton.IsVisible>
                              <Path Width="13" Height="13" Data="{StaticResource Icons.SyntaxHighlight}" Margin="0,3,0,0"/>
                            </ToggleButton>
                            <Button Grid.Column="3"
                                    Classes="icon_button"
                                    Width="28"
                                    Background="Transparent"
                                    Click="OnOpenFileWithDefaultEditor"
                                    IsVisible="{Binding CanOpenWithDefaultEditor, Mode=OneWay}"
                                    ToolTip.Tip="{DynamicResource Text.Open}">
                              <Path Width="12" Height="12" Data="{StaticResource Icons.OpenWith}"/>
                            </Button>
                          </Grid>
                        </Border>

                        <v:RevisionFileContentViewer Grid.Row="1" Content="{Binding Content}"/>
                      </Grid>
                    </Border>
                  </DataTemplate>
                </ContentControl.DataTemplates>
              </ContentControl>

              <Button Grid.Row="2"
                      Classes="flat primary"
                      Margin="0,0,0,8"
                      HorizontalAlignment="Center"
                      Content="{DynamicResource Text.ChangeCM.CheckoutThisRevision}"
                      Click="OnResetToSelectedRevision"/>
            </Grid>
          </DataTemplate>

          <DataTemplate DataType="vm:FileHistoriesCompareRevisions">
            <Grid RowDefinitions="Auto,*">
              <Grid Grid.Row="0" Margin="4,6" ColumnDefinitions="*,32,*,Auto">
                <Grid.DataTemplates>
                  <DataTemplate DataType="m:FileVersion">
                    <Grid RowDefinitions="Auto,*">
                      <Grid Grid.Row="0" ColumnDefinitions="Auto,*,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 SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" Cursor="Hand" PointerPressed="OnPressCommitSHA" />
                        <v:DateTimePresenter Grid.Column="3"
                                             Margin="8,0,0,0"
                                             Timestamp="{Binding AuthorTime}"
                                             Foreground="{DynamicResource Brush.FG2}"/>
                      </Grid>

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

                <Border Grid.Column="0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
                  <ContentControl Content="{Binding StartPoint}"/>
                </Border>

                <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>

                <Border Grid.Column="2" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}" CornerRadius="4" Padding="4">
                  <ContentControl Content="{Binding EndPoint}"/>
                </Border>

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

              <ContentControl Grid.Row="1" Margin="4,4,8,8" Content="{Binding ViewContent}">
                <ContentControl.DataTemplates>
                  <DataTemplate DataType="vm:DiffContext">
                    <v:DiffView/>
                  </DataTemplate>
                </ContentControl.DataTemplates>
              </ContentControl>
            </Grid>
          </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>

      <v:LoadingIcon Grid.Column="2"
                     Width="48" Height="48"
                     HorizontalAlignment="Center" VerticalAlignment="Center"
                     IsVisible="{Binding IsLoading}"/>
    </Grid>
  </Grid>
</v:ChromelessWindow>
