<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:c="using:SourceGit.Converters"
                    xmlns:v="using:SourceGit.Views"
                    mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
                    x:Class="SourceGit.Views.InteractiveRebase"
                    x:DataType="vm:InteractiveRebase"
                    x:Name="ThisControl"
                    Icon="/App.ico"
                    Title="{DynamicResource Text.InteractiveRebase}"
                    Width="1080" Height="720"
                    WindowStartupLocation="CenterOwner">
  <Grid RowDefinitions="Auto,Auto,*,Auto,Auto">
    <!-- TitleBar -->
    <Grid Grid.Row="0" Height="28" IsVisible="{Binding !#ThisControl.UseSystemWindowFrame}">
      <Border Background="{DynamicResource Brush.TitleBar}"
              BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
              PointerPressed="BeginMoveWindow"/>

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

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

      <v:CaptionButtons HorizontalAlignment="Right"
                        IsCloseButtonOnly="True"
                        IsVisible="{OnPlatform True, macOS=False}"/>
    </Grid>

    <!-- Operation Information -->
    <Grid Grid.Row="1" Margin="8" ColumnDefinitions="*,Auto,0,0">
      <StackPanel Grid.Column="0" Orientation="Horizontal" ClipToBounds="True">
        <TextBlock Text="{DynamicResource Text.InteractiveRebase.Target}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
        <Path Width="14" Height="14" Margin="8,0,0,0" Data="{StaticResource Icons.Branch}"/>
        <TextBlock VerticalAlignment="Center" Text="{Binding Current.FriendlyName}" Margin="8,0,0,0"/>

        <TextBlock Margin="16,0,0,0" Text="{DynamicResource Text.InteractiveRebase.On}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
        <Path Width="14" Height="14" Margin="8,0,0,0" Data="{StaticResource Icons.Commit}"/>
        <TextBlock VerticalAlignment="Center" Text="{Binding On.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0"/>
        <TextBlock VerticalAlignment="Center" Text="{Binding On.Subject}" Margin="4,0,0,0"/>
      </StackPanel>

      <Border Grid.Column="1" Background="Transparent" Width="20" Height="20">
        <ToolTip.Tip>
          <TextBlock>
            <Run Text="{DynamicResource Text.InteractiveRebase.ReorderTip}"/>
            <Run Text=" "/>
            <Run Text="{OnPlatform 'Ctrl+Up/Ctrl+Down', macOS='⌘+Up/⌘+Down'}" FontSize="11" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
          </TextBlock>
        </ToolTip.Tip>

        <Path Width="14" Height="14" Data="{StaticResource Icons.Info}"/>
      </Border>
      
      <Button Grid.Column="2" Width="0" Height="0" Click="OnMoveSelectedUp" HotKey="{OnPlatform Ctrl+Up, macOS=⌘+Up}"/>
      <Button Grid.Column="3" Width="0" Height="0" Click="OnMoveSelectedDown" HotKey="{OnPlatform Ctrl+Down, macOS=⌘+Down}"/>
    </Grid>

    <!-- Body -->
    <Border Grid.Row="2" Margin="8,0,8,8" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
      <Grid RowDefinitions="*,3,*">
        <v:InteractiveRebaseListBox Grid.Row="0"
                                    x:Name="ItemListBox"
                                    Focusable="True"
                                    Background="{DynamicResource Brush.Contents}"
                                    ItemsSource="{Binding Items}"
                                    SelectionMode="Multiple"
                                    SelectedItem="{Binding PreSelected, Mode=OneWay}"
                                    SelectionChanged="OnRowsSelectionChanged"
                                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                    ScrollViewer.VerticalScrollBarVisibility="Auto"
                                    Grid.IsSharedSizeScope="True">
          <v:InteractiveRebaseListBox.Styles>
            <Style Selector="ListBoxItem">
              <Setter Property="Margin" Value="0"/>
              <Setter Property="Padding" Value="0"/>
              <Setter Property="Height" Value="28"/>
              <Setter Property="HorizontalAlignment" Value="Stretch"/>
              <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
          </v:InteractiveRebaseListBox.Styles>

          <v:InteractiveRebaseListBox.ItemsPanel>
            <ItemsPanelTemplate>
              <VirtualizingStackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
          </v:InteractiveRebaseListBox.ItemsPanel>

          <v:InteractiveRebaseListBox.ItemTemplate>
            <DataTemplate DataType="vm:InteractiveRebaseItem">
              <Grid Height="28"
                    Margin="8,0"
                    Background="Transparent"
                    ClipToBounds="True"
                    PointerPressed="OnRowPointerPressed"
                    DragDrop.AllowDrop="True"
                    DragDrop.DragOver="OnRowDragOver"
                    DragDrop.DragLeave="OnRowDragLeave"
                    DragDrop.Drop="OnRowDrop">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" SharedSizeGroup="CommitOrderColumn"/>
                  <ColumnDefinition Width="110"/>
                  <ColumnDefinition Width="24"/>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="32"/>
                  <ColumnDefinition Width="108"/>
                  <ColumnDefinition Width="Auto" SharedSizeGroup="CommitHashColumn"/>
                  <ColumnDefinition Width="Auto" SharedSizeGroup="CommitTimeColumn"/>
                </Grid.ColumnDefinitions>

                <!-- Drop Direction Indicator -->
                <Border Grid.Column="0" Grid.ColumnSpan="8"
                        Background="Transparent"
                        BorderThickness="{Binding DropDirectionIndicator, Mode=OneWay}"
                        BorderBrush="{DynamicResource Brush.Accent}"/>

                <!-- Original Order -->
                <TextBlock Grid.Column="0"
                           Margin="4,0,0,0"
                           FontFamily="{DynamicResource Fonts.Monospace}"
                           Text="{Binding OriginalOrder, StringFormat='#{0}'}"
                           HorizontalAlignment="Center"/>
                
                <!-- Action -->
                <Button Grid.Column="1" Opacity="1" Margin="4,0,0,0" Padding="8,2" Background="Transparent" Click="OnShowActionsDropdownMenu">
                  <StackPanel Orientation="Horizontal">
                    <Ellipse Width="14" Height="14" Fill="{Binding Action, Converter={x:Static c:InteractiveRebaseActionConverters.ToIconBrush}}"/>
                    <TextBlock Margin="8,0" Text="{Binding Action, Converter={x:Static c:InteractiveRebaseActionConverters.ToName}}"/>
                  </StackPanel>
                </Button>

                <!-- Indicator -->
                <v:InteractiveRebaseIndicator Grid.Column="2"
                                              Fill="{DynamicResource Brush.FG1}"
                                              PendingType="{Binding PendingType, Mode=OneWay}"/>

                <!-- Subject -->
                <Grid Grid.Column="3" ColumnDefinitions="Auto,*" ClipToBounds="True">
                  <Button Grid.Column="0"
                          Classes="icon_button"
                          Margin="0,0,6,0" Padding="0"
                          IsVisible="{Binding ShowEditMessageButton, Mode=OneWay}"
                          Click="OnOpenCommitMessageEditor">
                    <Path Width="14" Height="14" Margin="0,4,0,0" Data="{StaticResource Icons.Edit}"/>
                  </Button>

                  <v:CommitSubjectPresenter Grid.Column="1"
                                            Height="28"
                                            Margin="0,0,4,0"
                                            FontFamily="{DynamicResource Fonts.Default}"
                                            CodeFontFamily="{DynamicResource Fonts.Monospace}"
                                            InlineCodeBackground="{DynamicResource Brush.InlineCode}"
                                            Foreground="{DynamicResource Brush.FG1}"
                                            LinkForeground="{DynamicResource Brush.Link}"
                                            Subject="{Binding Subject}"
                                            IssueTrackers="{Binding $parent[v:InteractiveRebase].((vm:InteractiveRebase)DataContext).IssueTrackers}"
                                            FontWeight="Normal"
                                            Opacity="{Binding Action, Mode=OneWay, Converter={x:Static c:InteractiveRebaseActionConverters.ToOpacity}}"
                                            ShowStrikethrough="{Binding Action, Mode=OneWay, Converter={x:Static c:InteractiveRebaseActionConverters.IsDrop}}"/>
                </Grid>

                <!-- Author Avatar -->
                <v:Avatar Grid.Column="4"
                          Width="16" Height="16"
                          Margin="8,0,0,0"
                          VerticalAlignment="Center"
                          User="{Binding Commit.Author}"/>

                <!-- Author Name -->
                <Border Grid.Column="5" ClipToBounds="True">
                  <TextBlock Margin="6,0,12,0"
                             Text="{Binding Commit.Author.Name}"
                             Opacity="{Binding Action, Mode=OneWay, Converter={x:Static c:InteractiveRebaseActionConverters.ToOpacity}}"
                             Classes.dropped="{Binding Action, Mode=OneWay, Converter={x:Static c:InteractiveRebaseActionConverters.IsDrop}}"/>
                </Border>

                <!-- Commit SHA -->
                <Border Grid.Column="6" ClipToBounds="True">
                  <TextBlock Text="{Binding Commit.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
                             Opacity="{Binding Action, Mode=OneWay, Converter={x:Static c:InteractiveRebaseActionConverters.ToOpacity}}"
                             Classes.dropped="{Binding Action, Mode=OneWay, Converter={x:Static c:InteractiveRebaseActionConverters.IsDrop}}"/>
                </Border>

                <!-- Commit Time -->
                <Border Grid.Column="7">
                  <v:DateTimePresenter Margin="16,0,8,0"
                                       Timestamp="{Binding Commit.CommitterTime}"
                                       Opacity="{Binding Action, Mode=OneWay, Converter={x:Static c:InteractiveRebaseActionConverters.ToOpacity}}"
                                       Classes.dropped="{Binding Action, Mode=OneWay, Converter={x:Static c:InteractiveRebaseActionConverters.IsDrop}}"/>
                </Border>
              </Grid>
            </DataTemplate>
          </v:InteractiveRebaseListBox.ItemTemplate>
        </v:InteractiveRebaseListBox>

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

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

        <ContentControl Grid.Row="2">
          <ContentControl.Content>
            <Binding Path="Detail">
              <Binding.TargetNullValue>
                <Path Width="128" Height="128"
                      Data="{StaticResource Icons.Detail}"
                      HorizontalAlignment="Center" VerticalAlignment="Center"
                      Fill="{DynamicResource Brush.FG2}"/>
              </Binding.TargetNullValue>
            </Binding>
          </ContentControl.Content>

          <ContentControl.DataTemplates>
            <DataTemplate DataType="m:Count">
              <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 Value, Converter={x:Static c:StringConverters.FormatByResourceKey}, ConverterParameter='Histories.Selected'}"/>
              </StackPanel>
            </DataTemplate>
            <DataTemplate DataType="vm:CommitDetail">
              <v:CommitDetail/>
            </DataTemplate>
          </ContentControl.DataTemplates>
        </ContentControl>
      </Grid>
    </Border>

    <!-- Options -->
    <Grid Grid.Row="3" ColumnDefinitions="*,Auto,Auto" Margin="8,0,8,8">
      <CheckBox Grid.Column="0"
                Content="{DynamicResource Text.InteractiveRebase.AutoStash}"
                VerticalAlignment="Center"
                IsChecked="{Binding AutoStash, Mode=TwoWay}"/>
      <Button Grid.Column="1" Classes="flat primary" MinWidth="80" Content="{DynamicResource Text.Start}" Click="OnStartJobs"/>
      <Button Grid.Column="2" Classes="flat" Margin="8,0,0,0" MinWidth="80" Content="{DynamicResource Text.Cancel}" Click="CloseWindow"/>
    </Grid>

    <!-- Progress -->
    <ProgressBar x:Name="Running"
                 Grid.Row="4"
                 Margin="8,0,8,12"
                 Background="{DynamicResource Brush.FG2}"
                 Foreground="{DynamicResource Brush.Accent}"
                 Minimum="0"
                 Maximum="100"
                 IsVisible="False"/>
  </Grid>
</v:ChromelessWindow>
