<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: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="600"
             x:Class="SourceGit.Views.WorkingCopy"
             x:DataType="vm:WorkingCopy">
  <Grid SizeChanged="OnMainLayoutSizeChanged">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="{Binding Source={x:Static vm:Preference.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="*" MinHeight="100"/>
        <RowDefinition Height="1"/>
        <RowDefinition Height="*" MinHeight="100"/>
      </Grid.RowDefinitions>
      
      <!-- Unstaged -->
      <Grid Grid.Row="0" 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" Text="{DynamicResource Text.WorkingCopy.Unstaged}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="4,0,0,0"/>
            <TextBlock Grid.Column="2" FontWeight="Bold" Foreground="{DynamicResource Brush.FG2}" Text="{Binding Unstaged, Converter={x:Static c:ListConverters.ToCount}}"/>
            <v:LoadingIcon Grid.Column="3" Width="14" Height="14" Margin="8,0,0,0" IsVisible="{Binding IsStaging}"/>

            <Button Grid.Column="5"
                    Classes="icon_button"
                    Width="26" Height="14"
                    Padding="0"
                    ToolTip.Tip="{DynamicResource Text.WorkingCopy.Unstaged.ViewAssumeUnchaged}"
                    Command="{Binding OpenAssumeUnchanged}">
              <Path Width="14" Height="14" Data="{StaticResource Icons.File.Ignore}"/>
            </Button>
            <ToggleButton Grid.Column="6"
                          Classes="toggle_untracked"
                          Width="26" Height="14"
                          ToolTip.Tip="{DynamicResource Text.WorkingCopy.IncludeUntracked}"
                          IsChecked="{Binding IncludeUntracked, Mode=TwoWay}"/>
            <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}" Opacity=".6" 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}"
                    Command="{Binding StageAll}">
              <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:Preference.Instance}, Path=UnstagedChangeViewMode, Mode=TwoWay}"/>
          </Grid>
        </Border>

        <!-- Unstaged Changes -->
        <v:ChangeCollectionView Grid.Row="1"
                                x:Name="UnstagedChangesView"
                                Focusable="True"
                                IsUnstagedChange="True"
                                SelectionMode="Multiple"
                                Background="{DynamicResource Brush.Contents}"
                                ViewMode="{Binding Source={x:Static vm:Preference.Instance}, Path=UnstagedChangeViewMode}"
                                Changes="{Binding Unstaged}"
                                SelectedChanges="{Binding SelectedUnstaged, Mode=TwoWay}"
                                ContextRequested="OnUnstagedContextRequested"
                                ChangeDoubleTapped="OnUnstagedChangeDoubleTapped"
                                KeyDown="OnUnstagedKeyDown"/>
      </Grid>
      
      <!-- Splitter -->
      <GridSplitter Grid.Row="1"
                    MinHeight="1"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                    Background="{DynamicResource Brush.Border0}"/>

      <!-- Staged -->
      <Grid Grid.Row="2" 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,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" Text="{DynamicResource Text.WorkingCopy.Staged}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="4,0,0,0"/>
            <TextBlock Grid.Column="2" FontWeight="Bold" Foreground="{DynamicResource Brush.FG2}" Text="{Binding Staged, Converter={x:Static c:ListConverters.ToCount}}"/>
            <v:LoadingIcon Grid.Column="3" Width="14" Height="14" Margin="8,0,0,0" IsVisible="{Binding IsUnstaging}"/>
            <Button Grid.Column="5" 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}" Opacity=".6" FontSize="11" VerticalAlignment="Center"/>
                </StackPanel>
              </ToolTip.Tip>
              <Path Width="14" Height="14" Margin="0,6,0,0" Data="{StaticResource Icons.Up}"/>
            </Button>
            <Button Grid.Column="6" Classes="icon_button" Width="26" Height="14" Padding="0" ToolTip.Tip="{DynamicResource Text.WorkingCopy.Staged.UnstageAll}" Command="{Binding UnstageAll}">
              <Path Width="14" Height="14" Data="{StaticResource Icons.DoubleUp}"/>
            </Button>
            <v:ChangeViewModeSwitcher Grid.Column="7"
                                      Width="26" Height="14"
                                      Margin="0,1,0,0"
                                      ViewMode="{Binding Source={x:Static vm:Preference.Instance}, Path=StagedChangeViewMode, Mode=TwoWay}"/>
          </Grid>
        </Border>

        <!-- Staged Changes -->
        <v:ChangeCollectionView Grid.Row="1"
                                x:Name="StagedChangesView"
                                Focusable="True"
                                SelectionMode="Multiple"
                                Background="{DynamicResource Brush.Contents}"
                                ViewMode="{Binding Source={x:Static vm:Preference.Instance}, Path=StagedChangeViewMode}"
                                Changes="{Binding Staged}"
                                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">
              <Border Background="{DynamicResource Brush.Window}" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
                <Grid VerticalAlignment="Center">
                  <StackPanel Orientation="Vertical" IsVisible="{Binding !IsResolved}">
                    <StackPanel.DataTemplates>
                      <DataTemplate DataType="m:Branch">
                        <StackPanel Orientation="Horizontal">
                          <Path Width="12" Height="12" Data="{StaticResource Icons.Branch}"/>
                          <TextBlock Margin="4,0,0,0" Text="{Binding FriendlyName}"/>
                          <TextBlock Margin="4,0,0,0" Text="{Binding Head, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
                        </StackPanel>
                      </DataTemplate>

                      <DataTemplate DataType="m:Commit">
                        <StackPanel Orientation="Horizontal">
                          <Path Width="12" Height="12" Data="{StaticResource Icons.Commit}"/>
                          <v:CommitRefsPresenter Margin="8,0,0,0"
                                                 TagBackground="{DynamicResource Brush.DecoratorTag}"
                                                 Foreground="{DynamicResource Brush.FG1}"
                                                 FontFamily="{DynamicResource Fonts.Primary}"
                                                 FontSize="11"
                                                 VerticalAlignment="Center"
                                                 UseGraphColor="False"/>
                          <TextBlock Margin="4,0,0,0" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
                          <TextBlock Margin="4,0,0,0" Text="{Binding Subject}"/>
                        </StackPanel>
                      </DataTemplate>

                      <DataTemplate DataType="m:Tag">
                        <StackPanel Orientation="Horizontal">
                          <Path Width="12" Height="12" Data="{StaticResource Icons.Tag}"/>
                          <TextBlock Margin="4,0,0,0" Text="{Binding Name}"/>
                          <TextBlock Margin="4,0,0,0" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
                        </StackPanel>
                      </DataTemplate>
                    </StackPanel.DataTemplates>
                    
                    <Path Width="64" Height="64" Data="{StaticResource Icons.Conflict}" Fill="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
                    <TextBlock Margin="0,16" FontSize="20" FontWeight="Bold" Text="{DynamicResource Text.WorkingCopy.Conflicts}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>

                    <Border Margin="16,0" Padding="8" CornerRadius="4" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
                      <Border.IsVisible>
                        <MultiBinding Converter="{x:Static BoolConverters.And}">
                          <Binding Path="Theirs" Converter="{x:Static ObjectConverters.IsNotNull}"/>
                          <Binding Path="Mine" Converter="{x:Static ObjectConverters.IsNotNull}"/>
                        </MultiBinding>
                      </Border.IsVisible>

                      <Grid Margin="8,0,0,0" RowDefinitions="32,32" ColumnDefinitions="Auto,*">
                        <TextBlock Grid.Row="0" Grid.Column="0" Classes="info_label" Text="THEIRS"/>
                        <ContentControl Grid.Row="0" Grid.Column="1" Margin="16,0,0,0" Content="{Binding Theirs}"/>
                        <TextBlock Grid.Row="1" Grid.Column="0" Classes="info_label" Text="MINE"/>
                        <ContentControl Grid.Row="1" Grid.Column="1" Margin="16,0,0,0" Content="{Binding Mine}"/>
                      </Grid>
                    </Border>

                    <StackPanel Margin="0,8,0,0"  Orientation="Horizontal" HorizontalAlignment="Center">
                      <Button Classes="flat" Content="USE THEIRS" Command="{Binding UseTheirs}"/>
                      <Button Classes="flat" Margin="8,0,0,0" Content="USE MINE" Command="{Binding UseMine}"/>
                      <Button Classes="flat" Margin="8,0,0,0" Content="OPEN EXTERNAL MERGETOOL" Command="{Binding OpenExternalMergeTool}"/>
                    </StackPanel>
                  </StackPanel>

                  <StackPanel Orientation="Vertical" IsVisible="{Binding IsResolved}">
                    <Path Width="64" Height="64" Data="{StaticResource Icons.Check}" Fill="Green"/>
                    <TextBlock Margin="0,16,0,8" FontSize="20" FontWeight="Bold" Text="{DynamicResource Text.WorkingCopy.Conflicts.Resolved}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
                    <TextBlock Text="{DynamicResource Text.WorkingCopy.CanStageTip}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
                  </StackPanel>
                </Grid>                
              </Border>
            </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:CommitMessageTextBox Grid.Row="2" Text="{Binding CommitMessage, Mode=TwoWay}"/>

      <!-- Commit Options -->
      <Grid Grid.Row="3" Margin="0,6,0,0" ColumnDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto,Auto,Auto">
        <Button Grid.Column="0"
                Classes="icon_button"
                Margin="4,0,0,0" Padding="0"
                Click="OnOpenCommitMessagePicker"
                ToolTip.Tip="{DynamicResource Text.WorkingCopy.CommitMessageHelper}"
                ToolTip.Placement="Top"
                ToolTip.VerticalOffset="0">
          <Path Width="12" Height="12" Data="{StaticResource Icons.Menu}"/>
        </Button>

        <Button Grid.Column="1"
                Classes="icon_button"
                Margin="4,2,0,0"
                Click="OnOpenOpenAIHelper"
                ToolTip.Tip="{DynamicResource Text.AIAssistant.Tip}"
                ToolTip.Placement="Top"
                ToolTip.VerticalOffset="0">
          <Path Width="15" Height="15" Data="{StaticResource Icons.AIAssist}"/>
        </Button>

        <Button Grid.Column="2"
                Classes="icon_button"
                Margin="0,2,0,0"
                Click="OnOpenConventionalCommitHelper"
                ToolTip.Tip="{DynamicResource Text.ConventionalCommit}"
                ToolTip.Placement="Top"
                ToolTip.VerticalOffset="0">
          <Path Width="15" Height="15" Data="{StaticResource Icons.CommitMessageGenerator}"/>
        </Button>

        <CheckBox Grid.Column="3"
                  Height="24"
                  Margin="8,0,0,0"
                  HorizontalAlignment="Left"
                  IsChecked="{Binding UseAmend, Mode=TwoWay}"
                  Content="{DynamicResource Text.WorkingCopy.Amend}"/>

        <v:LoadingIcon Grid.Column="5" Width="18" Height="18" IsVisible="{Binding IsCommitting}"/>
        
        <SplitButton Grid.Column="6"
                     Content="{DynamicResource Text.Repository.Continue}"
                     Height="28"
                     Margin="8,0,0,0"
                     Padding="8,0"
                     Command="{Binding ContinueMerge}"
                     IsVisible="{Binding InProgressContext, Converter={x:Static ObjectConverters.IsNotNull}}"
                     IsEnabled="{Binding !HasUnsolvedConflicts}">
          <SplitButton.Flyout>
            <MenuFlyout>
              <MenuItem Header="{DynamicResource Text.WorkingCopy.CommitToEdit}" Command="{Binding Commit}"/>
            </MenuFlyout>
          </SplitButton.Flyout>
        </SplitButton>

        <Button Grid.Column="6"
                Classes="flat primary"
                Content="{DynamicResource Text.WorkingCopy.Commit}"
                Height="28"
                Margin="8,0,0,0"
                Padding="8,0"
                Command="{Binding Commit}"
                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>
        
        <!-- Invisible button just to add another hotkey `Ctrl+Shift+Enter` to commit with auto-stage -->
        <Button Grid.Column="7"
                Width="0" Height="0"
                Background="Transparent"
                Command="{Binding CommitWithAutoStage}"
                HotKey="{OnPlatform Ctrl+Shift+Enter, macOS=⌘+Shift+Enter}"/>

        <Button Grid.Column="8"
                Classes="flat"
                Content="{DynamicResource Text.WorkingCopy.CommitAndPush}"
                Height="28"
                Margin="8,0,0,0"
                Padding="8,0"
                Command="{Binding CommitWithPush}"
                HotKey="Alt+Enter"
                ToolTip.Tip="{OnPlatform Alt+Enter, macOS=⌥+Enter}"
                ToolTip.Placement="Top"
                ToolTip.VerticalOffset="0">
          <Button.IsVisible>
            <MultiBinding Converter="{x:Static BoolConverters.And}">
              <Binding Path="UseAmend" Converter="{x:Static BoolConverters.Not}"/>
              <Binding Path="CanCommitWithPush"/>
              <Binding Path="InProgressContext" Converter="{x:Static ObjectConverters.IsNull}"/>
            </MultiBinding>
          </Button.IsVisible>  
        </Button>
      </Grid>
    </Grid>
  </Grid>
</UserControl>
