<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"
             xmlns:c="using:SourceGit.Converters"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="600"
             x:Class="SourceGit.Views.WorkingCopy"
             x:DataType="vm:WorkingCopy">
  <Grid SizeChanged="OnMainLayoutSizeChanged">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="{Binding Source={x:Static vm:Preferences.Instance}, Path=Layout.WorkingCopyLeftWidth, Mode=TwoWay}" MinWidth="300"/>
      <ColumnDefinition Width="5"/>
      <ColumnDefinition Width="*" MinWidth="300"/>
    </Grid.ColumnDefinitions>

    <!-- Left -->
    <Grid Grid.Column="0">
      <Grid.RowDefinitions>
        <RowDefinition Height="36"/>
        <RowDefinition Height="*" MinHeight="100"/>
        <RowDefinition Height="1"/>
        <RowDefinition Height="*" MinHeight="100"/>
      </Grid.RowDefinitions>

      <!-- Search Filter -->
      <Border Grid.Row="0" BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}">
        <TextBox Height="24"
                 Margin="4,0"
                 BorderThickness="1"
                 CornerRadius="12"
                 Text="{Binding Filter, Mode=TwoWay}"
                 BorderBrush="{DynamicResource Brush.Border2}"
                 VerticalContentAlignment="Center">
          <TextBox.InnerLeftContent>
            <Path Width="14" Height="14"
                  Margin="6,0,0,0"
                  Fill="{DynamicResource Brush.FG2}"
                  Data="{StaticResource Icons.Search}"/>
          </TextBox.InnerLeftContent>

          <TextBox.InnerRightContent>
            <Button Classes="icon_button"
                    Width="16"
                    Margin="0,0,6,0"
                    Command="{Binding ClearFilter}"
                    IsVisible="{Binding Filter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
                    HorizontalAlignment="Right">
              <Path Width="14" Height="14"
                    Margin="0,1,0,0"
                    Fill="{DynamicResource Brush.FG1}"
                    Data="{StaticResource Icons.Clear}"/>
            </Button>
          </TextBox.InnerRightContent>
        </TextBox>
      </Border>

      <!-- Unstaged -->
      <Grid Grid.Row="1" RowDefinitions="28,*">
        <!-- Unstaged Toolbar -->
        <Border Grid.Row="0" BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}">
          <Grid ColumnDefinitions="Auto,Auto,Auto,*,Auto,Auto,Auto,Auto,Auto,Auto">
            <Path Grid.Column="0" Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Changes}"/>
            <TextBlock Grid.Column="1" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="4,0,0,0">
              <Run Text="{DynamicResource Text.WorkingCopy.Unstaged}"/>
              <Run Text="{Binding Unstaged, Converter={x:Static c:ListConverters.ToCount}, Mode=OneWay}"/>
            </TextBlock>
            <v:LoadingIcon Grid.Column="2" Width="14" Height="14" Margin="8,0,0,0" IsVisible="{Binding IsStaging}"/>

            <Button Grid.Column="4"
                    Classes="icon_button"
                    Width="26" Height="14"
                    Padding="0"
                    ToolTip.Tip="{DynamicResource Text.WorkingCopy.Unstaged.ViewAssumeUnchanged}"
                    Click="OnOpenAssumeUnchanged">
              <Path Width="14" Height="14" Data="{StaticResource Icons.File.Ignore}"/>
            </Button>
            <ToggleButton Grid.Column="5"
                          Classes="toggle_untracked"
                          Width="26" Height="14"
                          ToolTip.Tip="{DynamicResource Text.WorkingCopy.IncludeUntracked}"
                          IsChecked="{Binding IncludeUntracked, Mode=TwoWay}"/>
            <Button Grid.Column="6"
                    Classes="icon_button"
                    Width="26" Height="14"
                    Padding="0"
                    IsVisible="{Binding HasUnsolvedConflicts}"
                    ToolTip.Tip="{DynamicResource Text.WorkingCopy.Conflicts.OpenExternalMergeToolAllConflicts}"
                    Click="OnOpenExternalMergeToolAllConflicts">
              <Path Width="14" Height="14" Data="{StaticResource Icons.Conflict}"/>
            </Button>
            <Button Grid.Column="7"
                    Classes="icon_button"
                    Width="26" Height="14"
                    Padding="0"
                    Click="OnStageSelectedButtonClicked">
              <ToolTip.Tip>
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                  <TextBlock Text="{DynamicResource Text.WorkingCopy.Unstaged.Stage}" VerticalAlignment="Center"/>
                  <TextBlock Margin="16,0,0,0"
                             Text="{OnPlatform Space/Enter, macOS=␣/Enter}"
                             Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"
                             FontSize="11"
                             VerticalAlignment="Center"/>
                </StackPanel>
              </ToolTip.Tip>
              <Path Width="14" Height="14" Margin="0,6,0,0" Data="{StaticResource Icons.Down}"/>
            </Button>
            <Button Grid.Column="8"
                    Classes="icon_button"
                    Width="26" Height="14"
                    Padding="0"
                    ToolTip.Tip="{DynamicResource Text.WorkingCopy.Unstaged.StageAll}"
                    Click="OnStageAllButtonClicked">
              <Path Width="14" Height="14" Data="{StaticResource Icons.DoubleDown}"/>
            </Button>
            <v:ChangeViewModeSwitcher Grid.Column="9"
                                      Width="26" Height="14"
                                      Margin="0,1,0,0"
                                      ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=UnstagedChangeViewMode, Mode=TwoWay}"/>
          </Grid>
        </Border>

        <!-- Unstaged Changes -->
        <v:ChangeCollectionView Grid.Row="1"
                                x:Name="UnstagedChangesView"
                                IsUnstagedChange="True"
                                Background="{DynamicResource Brush.Contents}"
                                ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=UnstagedChangeViewMode}"
                                EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
                                Changes="{Binding VisibleUnstaged}"
                                SelectedChanges="{Binding SelectedUnstaged, Mode=TwoWay}"
                                ContextRequested="OnUnstagedContextRequested"
                                ChangeDoubleTapped="OnUnstagedChangeDoubleTapped"
                                KeyDown="OnUnstagedKeyDown"/>
      </Grid>

      <!-- Splitter -->
      <GridSplitter Grid.Row="2"
                    MinHeight="1"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                    Background="{DynamicResource Brush.Border0}"/>

      <!-- Staged -->
      <Grid Grid.Row="3" RowDefinitions="28,*">
        <!-- Staged Toolbar -->
        <Border Grid.Row="0" BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}">
          <Grid ColumnDefinitions="Auto,Auto,Auto,*,Auto,Auto,Auto">
            <Path Grid.Column="0" Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Changes}"/>
            <TextBlock Grid.Column="1" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="4,0,0,0">
              <Run Text="{DynamicResource Text.WorkingCopy.Staged}"/>
              <Run Text="{Binding Staged, Converter={x:Static c:ListConverters.ToCount}, Mode=OneWay}"/>
            </TextBlock>
            <v:LoadingIcon Grid.Column="2" Width="14" Height="14" Margin="8,0,0,0" IsVisible="{Binding IsUnstaging}"/>
            <Button Grid.Column="4" Classes="icon_button" Width="26" Height="14" Padding="0" Click="OnUnstageSelectedButtonClicked">
              <ToolTip.Tip>
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                  <TextBlock Text="{DynamicResource Text.WorkingCopy.Staged.Unstage}" VerticalAlignment="Center"/>
                  <TextBlock Margin="16,0,0,0"
                             Text="{OnPlatform Space/Enter, macOS=␣/Enter}"
                             FontSize="11"
                             VerticalAlignment="Center"
                             Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
                </StackPanel>
              </ToolTip.Tip>
              <Path Width="14" Height="14" Margin="0,6,0,0" Data="{StaticResource Icons.Up}"/>
            </Button>
            <Button Grid.Column="5" Classes="icon_button" Width="26" Height="14" Padding="0" ToolTip.Tip="{DynamicResource Text.WorkingCopy.Staged.UnstageAll}" Click="OnUnstageAllButtonClicked">
              <Path Width="14" Height="14" Data="{StaticResource Icons.DoubleUp}"/>
            </Button>
            <v:ChangeViewModeSwitcher Grid.Column="6"
                                      Width="26" Height="14"
                                      Margin="0,1,0,0"
                                      ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=StagedChangeViewMode, Mode=TwoWay}"/>
          </Grid>
        </Border>

        <!-- Staged Changes -->
        <v:ChangeCollectionView Grid.Row="1"
                                x:Name="StagedChangesView"
                                IsUnstagedChange="False"
                                Background="{DynamicResource Brush.Contents}"
                                ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=StagedChangeViewMode}"
                                EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
                                Changes="{Binding VisibleStaged}"
                                SelectedChanges="{Binding SelectedStaged, Mode=TwoWay}"
                                ContextRequested="OnStagedContextRequested"
                                ChangeDoubleTapped="OnStagedChangeDoubleTapped"
                                KeyDown="OnStagedKeyDown"/>
      </Grid>
    </Grid>

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

    <!-- Right -->
    <Grid Grid.Column="2" Margin="0,4,4,4">
      <Grid.RowDefinitions>
        <RowDefinition Height="*" MinHeight="400"/>
        <RowDefinition Height="4"/>
        <RowDefinition Height="128" MinHeight="100"/>
        <RowDefinition Height="36"/>
      </Grid.RowDefinitions>

      <!-- Select Change Detail -->
      <Grid Grid.Row="0">
        <Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
          <StackPanel Orientation="Vertical" VerticalAlignment="Center">
            <Path Width="64" Height="64" Data="{StaticResource Icons.Diff}" Fill="{DynamicResource Brush.FG2}"/>
            <TextBlock Margin="0,16,0,0"
                       Text="{DynamicResource Text.Diff.Welcome}"
                       FontSize="18" FontWeight="Bold"
                       Foreground="{DynamicResource Brush.FG2}"
                       HorizontalAlignment="Center"/>
          </StackPanel>
        </Border>

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

            <DataTemplate DataType="vm:DiffContext">
              <v:DiffView/>
            </DataTemplate>
          </ContentControl.DataTemplates>
        </ContentControl>
      </Grid>

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

      <!-- Commit Message -->
      <v:CommitMessageToolBox Grid.Row="2" ShowAdvancedOptions="True" CommitMessage="{Binding CommitMessage, Mode=TwoWay}"/>

      <!-- Commit Options -->
      <Grid Grid.Row="3" Margin="0,6,0,0" ColumnDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto,Auto">
        <CheckBox Grid.Column="0"
                  Height="24"
                  Margin="1,0,0,0"
                  HorizontalAlignment="Left"
                  IsChecked="{Binding EnableSignOff, Mode=TwoWay}"
                  Content="{DynamicResource Text.WorkingCopy.SignOff}"
                  ToolTip.Tip="--signoff"
                  ToolTip.Placement="Top"
                  ToolTip.VerticalOffset="0"/>

        <CheckBox Grid.Column="1"
                  Height="24"
                  Margin="12,0,0,0"
                  HorizontalAlignment="Left"
                  IsChecked="{Binding NoVerifyOnCommit, Mode=TwoWay}"
                  Content="{DynamicResource Text.WorkingCopy.NoVerify}"
                  ToolTip.Tip="--no-verify"
                  ToolTip.Placement="Top"
                  ToolTip.VerticalOffset="0"/>

        <CheckBox Grid.Column="2"
                  Height="24"
                  Margin="12,0,0,0"
                  HorizontalAlignment="Left"
                  IsChecked="{Binding UseAmend, Mode=TwoWay}"
                  Content="{DynamicResource Text.WorkingCopy.Amend}"
                  ToolTip.Tip="--amend"
                  ToolTip.Placement="Top"
                  ToolTip.VerticalOffset="0"/>

        <CheckBox Grid.Column="3"
                  Height="24"
                  Margin="12,0,0,0"
                  HorizontalAlignment="Left"
                  IsChecked="{Binding ResetAuthor, Mode=TwoWay}"
                  IsVisible="{Binding UseAmend}"
                  Content="{DynamicResource Text.WorkingCopy.ResetAuthor}"
                  ToolTip.Tip="--reset-author"
                  ToolTip.Placement="Top"
                  ToolTip.VerticalOffset="0"/>

        <v:LoadingIcon Grid.Column="4"
                       Width="18" Height="18"
                       HorizontalAlignment="Right"
                       IsVisible="{Binding IsCommitting}"/>

        <SplitButton Grid.Column="5"
                     Content="{DynamicResource Text.Repository.Continue}"
                     Height="28"
                     Margin="8,0,0,0"
                     Padding="8,0"
                     Click="OnContinue"
                     IsVisible="{Binding InProgressContext, Converter={x:Static ObjectConverters.IsNotNull}}">
          <SplitButton.Styles>
            <Style Selector="SplitButton">
              <Setter Property="MinHeight" Value="24"/>
              <Setter Property="Template">
                <ControlTemplate>
                  <Grid ColumnDefinitions="*,1,Auto">
                    <Button x:Name="PART_PrimaryButton"
                            Grid.Column="0"
                            Classes="flat primary"
                            MinWidth="32"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            Command="{TemplateBinding Command}"
                            CommandParameter="{TemplateBinding CommandParameter}"
                            CornerRadius="3,0,0,3"
                            Padding="{TemplateBinding Padding}"
                            Focusable="False"
                            KeyboardNavigation.IsTabStop="False" />

                    <Button x:Name="PART_SecondaryButton"
                            Grid.Column="2"
                            Classes="flat primary"
                            Width="32"
                            CornerRadius="0,3,3,0"
                            Padding="0"
                            Focusable="False"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            KeyboardNavigation.IsTabStop="False">
                      <Path Height="12" Width="12"
                            Margin="0,4,0,0"
                            Fill="{DynamicResource AccentButtonForeground}"
                            Data="{DynamicResource Icons.Down}"/>
                    </Button>
                  </Grid>
                </ControlTemplate>
              </Setter>

              <Style Selector="^:disabled /template/ Button">
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="BorderBrush" Value="{DynamicResource Brush.Border2}"/>
                <Setter Property="Background" Value="Transparent"/>
              </Style>

              <Style Selector="^:disabled TextBlock">
                <Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/>
              </Style>

              <Style Selector="^:disabled Path">
                <Setter Property="Fill" Value="{DynamicResource Brush.FG2}"/>
              </Style>
            </Style>
          </SplitButton.Styles>
          
          <SplitButton.IsEnabled>
            <MultiBinding Converter="{x:Static BoolConverters.And}">
              <Binding Path="IsCommitting" Converter="{x:Static BoolConverters.Not}"/>
              <Binding Path="HasUnsolvedConflicts" Converter="{x:Static BoolConverters.Not}"/>
            </MultiBinding>
          </SplitButton.IsEnabled>
          <SplitButton.Flyout>
            <MenuFlyout>
              <MenuItem Header="{DynamicResource Text.WorkingCopy.CommitToEdit}"
                        Click="OnCommit"
                        IsEnabled="{Binding CommitMessage, Converter={x:Static c:StringConverters.IsNotNullOrWhitespace}}"/>
            </MenuFlyout>
          </SplitButton.Flyout>
        </SplitButton>

        <Button Grid.Column="5"
                Classes="flat primary"
                Content="{DynamicResource Text.WorkingCopy.Commit}"
                Height="28"
                Margin="8,0,0,0"
                Padding="8,0"
                Click="OnCommit"
                HotKey="{OnPlatform Ctrl+Enter, macOS=⌘+Enter}"
                IsVisible="{Binding InProgressContext, Converter={x:Static ObjectConverters.IsNull}}"
                ToolTip.Placement="Top"
                ToolTip.VerticalOffset="0">
          <ToolTip.Tip>
            <StackPanel Orientation="Vertical">
              <TextBlock TextAlignment="Left" TextWrapping="Wrap">
                <Run Text="{OnPlatform Ctrl+Enter, macOS=⌘+Enter}"/>
                <Run Foreground="{DynamicResource Brush.FG2}" Text="{DynamicResource Text.WorkingCopy.CommitTip}"/>
              </TextBlock>
              <TextBlock TextAlignment="Left" TextWrapping="Wrap" Margin="0,4,0,0">
                <Run Text="{OnPlatform Ctrl+Shift+Enter, macOS=⌘+⇧+Enter}"/>
                <Run Foreground="{DynamicResource Brush.FG2}" Text="{DynamicResource Text.WorkingCopy.CommitWithAutoStage}"/>
              </TextBlock>
            </StackPanel>
          </ToolTip.Tip>

          <Button.IsEnabled>
            <MultiBinding Converter="{x:Static BoolConverters.And}">
              <Binding Path="IsCommitting" Converter="{x:Static BoolConverters.Not}"/>
              <Binding Path="CommitMessage" Converter="{x:Static c:StringConverters.IsNotNullOrWhitespace}"/>
            </MultiBinding>
          </Button.IsEnabled>
        </Button>

        <!-- Invisible button just to add another hotkey `Ctrl+Shift+Enter` to commit with auto-stage -->
        <Button Grid.Column="6"
                Width="0" Height="0"
                Background="Transparent"
                Click="OnCommitWithAutoStage"
                HotKey="{OnPlatform Ctrl+Shift+Enter, macOS=⌘+Shift+Enter}">
          <Button.IsEnabled>
            <MultiBinding Converter="{x:Static BoolConverters.And}">
              <Binding Path="IsCommitting" Converter="{x:Static BoolConverters.Not}"/>
              <Binding Path="CommitMessage" Converter="{x:Static c:StringConverters.IsNotNullOrWhitespace}"/>
            </MultiBinding>
          </Button.IsEnabled>
        </Button>

        <Button Grid.Column="7"
                Classes="flat"
                Content="{DynamicResource Text.WorkingCopy.CommitAndPush}"
                Height="28"
                Margin="8,0,0,0"
                Padding="8,0"
                Click="OnCommitWithPush"
                HotKey="{OnPlatform Ctrl+Alt+Enter, macOS=⌘+Alt+Enter}"
                ToolTip.Tip="{OnPlatform Ctrl+Alt+Enter, macOS=⌘+⌥+Enter}"
                ToolTip.Placement="Top"
                ToolTip.VerticalOffset="0">
          <Button.IsEnabled>
            <MultiBinding Converter="{x:Static BoolConverters.And}">
              <Binding Path="IsCommitting" Converter="{x:Static BoolConverters.Not}"/>
              <Binding Path="CommitMessage" Converter="{x:Static c:StringConverters.IsNotNullOrWhitespace}"/>
            </MultiBinding>
          </Button.IsEnabled>

          <Button.IsVisible>
            <MultiBinding Converter="{x:Static BoolConverters.And}">
              <Binding Path="HasRemotes"/>
              <Binding Path="UseAmend" Converter="{x:Static BoolConverters.Not}"/>
              <Binding Path="InProgressContext" Converter="{x:Static ObjectConverters.IsNull}"/>
            </MultiBinding>
          </Button.IsVisible>
        </Button>
      </Grid>
    </Grid>
  </Grid>
</UserControl>
