<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>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="{Binding Source={x:Static vm:Preference.Instance}, Path=Layout.WorkingCopyLeftWidth, Mode=TwoWay}" MinWidth="300"/>
      <ColumnDefinition Width="5"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <!-- Left -->
    <Grid Grid.Column="0" RowDefinitions="28,*,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">
          <v:ChangeViewModeSwitcher Grid.Column="0" Width="12" Height="12" Margin="8,0,0,0" ViewMode="{Binding Source={x:Static vm:Preference.Instance}, Path=UnstagedChangeViewMode, Mode=TwoWay}"/>
          <TextBlock Grid.Column="1" Text="{DynamicResource Text.WorkingCopy.Unstaged}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="8,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>
        </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"/>

      <!-- Staged Toolbar -->
      <Border Grid.Row="2" BorderThickness="0,1" BorderBrush="{DynamicResource Brush.Border0}">
        <Grid ColumnDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto">
          <v:ChangeViewModeSwitcher Grid.Column="0" Width="12" Height="12" Margin="8,0,0,0" ViewMode="{Binding Source={x:Static vm:Preference.Instance}, Path=StagedChangeViewMode, Mode=TwoWay}"/>
          <TextBlock Grid.Column="1" Text="{DynamicResource Text.WorkingCopy.Staged}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="8,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>
        </Grid>
      </Border>

      <!-- Staged Changes -->
      <v:ChangeCollectionView Grid.Row="3"
                              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>

    <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:ConflictContext">
              <Border Background="{DynamicResource Brush.Window}" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
                <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                  <StackPanel Orientation="Vertical" IsVisible="{Binding !IsResolved}">
                    <Path Width="64" Height="64" Data="{StaticResource Icons.Conflict}" Fill="{DynamicResource Brush.FG2}"/>
                    <TextBlock Margin="0,16,0,8" FontSize="20" FontWeight="Bold" Text="{DynamicResource Text.WorkingCopy.Conflicts}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
                    <TextBlock Text="{DynamicResource Text.WorkingCopy.ResolveTip}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
                  </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"
                Width="32"
                Margin="4,2,0,0"
                Click="OnOpenAIAssist"
                ToolTip.Tip="{DynamicResource Text.AIAssistant.Tip}"
                ToolTip.Placement="Top"
                ToolTip.VerticalOffset="0">
          <Path Width="15" Height="15" Data="{StaticResource Icons.AIAssist}"/>
        </Button>

        <CheckBox Grid.Column="2"
                  Height="24"
                  Margin="4,0,0,0"
                  HorizontalAlignment="Left"
                  IsChecked="{Binding AutoStageBeforeCommit, Mode=TwoWay}"
                  Content="{DynamicResource Text.WorkingCopy.AutoStage}"/>

        <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}"/>

        <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}"
                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"
                IsVisible="{Binding IsCommitWithPushVisible}"/>
      </Grid>
    </Grid>
  </Grid>
</UserControl>
