<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="me"
                    Icon="/App.ico"
                    Title="{DynamicResource Text.FileHistory}"
                    MinWidth="1280" MinHeight="720">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="30"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!-- TitleBar -->
    <Grid Grid.Row="0" ColumnDefinitions="Auto,Auto,*,Auto">
      <!-- Bottom border -->
      <Border Grid.Column="0" Grid.ColumnSpan="5"
              Background="{DynamicResource Brush.TitleBar}"
              BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
              DoubleTapped="MaximizeOrRestoreWindow"
              PointerPressed="BeginMoveWindow"
              PointerMoved="MoveWindow"
              PointerReleased="EndMoveWindow"/>

      <!-- Caption Buttons (macOS) -->
      <Border Grid.Column="0" IsVisible="{OnPlatform False, macOS=True}">
        <v:CaptionButtonsMacOS/>
      </Border>

      <!-- Icon -->
      <Path Grid.Column="1" Margin="8,0,0,0" Width="12" Height="12" Stretch="Uniform" Data="{StaticResource Icons.Histories}"/>

      <!-- Title -->
      <TextBlock Grid.Column="2" Margin="4,0,0,0" Text="{DynamicResource Text.FileHistory}" FontWeight="Bold" IsHitTestVisible="False" VerticalAlignment="Center"/>

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

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

      <DataGrid Grid.Column="0"
                Background="{DynamicResource Brush.Contents}"
                BorderThickness="1"
                Margin="8,4,4,8"
                BorderBrush="{DynamicResource Brush.Border2}"
                ItemsSource="{Binding Commits}"
                SelectedItem="{Binding SelectedCommit, Mode=TwoWay}"
                SelectionMode="Single"
                CanUserReorderColumns="False"
                CanUserResizeColumns="False"
                CanUserSortColumns="False"
                IsReadOnly="True"
                HeadersVisibility="None"
                Focusable="False"
                RowHeight="50"
                HorizontalScrollBarVisibility="Disabled"
                VerticalScrollBarVisibility="Auto">
        <DataGrid.Columns>
          <DataGridTemplateColumn Width="*">
            <DataGridTemplateColumn.CellTemplate>
              <DataTemplate DataType="m:Commit">
                <Border BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="0,0,0,1" Padding="4">
                  <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" Classes="monospace" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
                      <TextBlock Grid.Column="2" Classes="monospace" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0"/>
                      <TextBlock Grid.Column="3" Classes="monospace" Text="{Binding AuthorTimeShortStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
                    </Grid>

                    <TextBlock Grid.Row="1" Classes="monospace" Text="{Binding Subject}" VerticalAlignment="Bottom"/>
                  </Grid>
                </Border>
              </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
        </DataGrid.Columns>
      </DataGrid>

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

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

      <Grid Grid.Column="2" RowDefinitions="*,3,*" IsVisible="{Binding !IsLoading}">
        <ContentControl Grid.Row="0" Margin="4,4,8,0" Content="{Binding DiffContext}">
          <ContentControl.DataTemplates>
            <DataTemplate DataType="vm:DiffContext">
              <v:DiffView/>
            </DataTemplate>
          </ContentControl.DataTemplates>
        </ContentControl>

        <GridSplitter Grid.Row="1"
                      MinHeight="1"
                      HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                      Background="Transparent"/>

        <Grid Grid.Row="2" Margin="0,0,4,4">
          <Border Background="{DynamicResource Brush.Window}">
            <Path Width="128" Height="128"
                  Data="{StaticResource Icons.Detail}"
                  HorizontalAlignment="Center"
                  Fill="{DynamicResource Brush.FG2}"/>
          </Border>

          <ContentControl Content="{Binding DetailContext}">
            <ContentControl.DataTemplates>
              <DataTemplate DataType="vm:CommitDetail">
                <v:CommitDetail/>
              </DataTemplate>
            </ContentControl.DataTemplates>
          </ContentControl>
        </Grid>
      </Grid>
    </Grid>
  </Grid>
</v:ChromelessWindow>
