<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:v="using:SourceGit.Views"
             xmlns:vm="using:SourceGit.ViewModels"
             xmlns:c="using:SourceGit.Converters"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="SourceGit.Views.Conflict"
             x:DataType="vm:Conflict">
  <Border Background="{DynamicResource Brush.Window}" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
    <Grid VerticalAlignment="Center">
      <StackPanel Orientation="Vertical" IsVisible="{Binding !IsResolved}">
        <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"/>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,8">
          <Border Height="16" VerticalAlignment="Center" Background="Red" CornerRadius="8">
            <TextBlock Text="{Binding Marker}" Foreground="White" FontWeight="Bold" Margin="8,0" FontSize="10"/>
          </Border>

          <TextBlock Margin="8,0,0,0" VerticalAlignment="Center" Text="{Binding Description}"/>
        </StackPanel>

        <Border Margin="16,0" Padding="8" CornerRadius="4" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
          <Border.DataTemplates>
            <DataTemplate DataType="m:Commit">
              <StackPanel Orientation="Horizontal">
                <v:CommitRefsPresenter Foreground="{DynamicResource Brush.FG1}"
                                       FontSize="12"
                                       VerticalAlignment="Center"
                                       UseGraphColor="False"/>
                <TextBlock Margin="4,0,0,0"
                           Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
                           Foreground="DarkOrange"
                           TextDecorations="Underline"
                           Cursor="Hand"
                           PointerPressed="OnPressedSHA"/>
                <TextBlock Margin="4,0,0,0" Text="{Binding Subject}"/>
              </StackPanel>
            </DataTemplate>

            <DataTemplate DataType="x:String">
              <StackPanel Orientation="Horizontal">
                <Path Width="12" Height="12" Data="{StaticResource Icons.Changes}"/>
                <TextBlock Margin="4,0,0,0" Text="{Binding}"/>
              </StackPanel>
            </DataTemplate>
          </Border.DataTemplates>

          <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="MINE"/>
            <ContentControl Grid.Row="0" Grid.Column="1" Margin="16,0,0,0" Content="{Binding Mine}"/>
            <TextBlock Grid.Row="1" Grid.Column="0" Classes="info_label" Text="THEIRS"/>
            <ContentControl Grid.Row="1" Grid.Column="1" Margin="16,0,0,0" Content="{Binding Theirs}"/>
          </Grid>
        </Border>

        <StackPanel Margin="0,8,0,0" Orientation="Horizontal" HorizontalAlignment="Center">
          <Button Classes="flat" Margin="0,0,0,0" Click="OnUseTheirs">
            <StackPanel Orientation="Horizontal">
              <Path Width="12" Height="12" Data="{StaticResource Icons.Incoming}"/>
              <TextBlock Margin="6,0,0,0" Text="{DynamicResource Text.WorkingCopy.Conflicts.UseTheirs}" VerticalAlignment="Center"/>
            </StackPanel>
          </Button>
          <Button Classes="flat" Margin="8,0,0,0" Click="OnUseMine">
            <StackPanel Orientation="Horizontal">
              <Path Width="12" Height="12" Data="{StaticResource Icons.Local}"/>
              <TextBlock Margin="6,0,0,0" Text="{DynamicResource Text.WorkingCopy.Conflicts.UseMine}" VerticalAlignment="Center"/>
            </StackPanel>
          </Button>
          <SplitButton Height="28"
                       Margin="8,0,0,0" Padding="8,0"
                       Click="OnMerge"
                       IsVisible="{Binding CanMerge, Mode=OneWay}">
            <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 AccessText">
                  <Setter Property="Foreground" Value="{DynamicResource Brush.FG2}"/>
                </Style>

                <Style Selector="^:disabled Path">
                  <Setter Property="Fill" Value="{DynamicResource Brush.FG2}"/>
                </Style>
              </Style>
            </SplitButton.Styles>
            
            <SplitButton.Flyout>
              <MenuFlyout Placement="BottomEdgeAlignedLeft">
                <MenuItem Header="{DynamicResource Text.WorkingCopy.Conflicts.MergeExternal}"
                          Click="OnMergeExternal">
                  <MenuItem.Icon>
                    <Path Width="12" Height="12" Data="{StaticResource Icons.OpenWith}"/>
                  </MenuItem.Icon>
                </MenuItem>
              </MenuFlyout>
            </SplitButton.Flyout>

            <StackPanel Orientation="Horizontal">
              <Path Width="12" Height="12" Data="{StaticResource Icons.Conflict}" Fill="White"/>
              <TextBlock Margin="6,0,0,0" Text="{DynamicResource Text.WorkingCopy.Conflicts.Merge}" Foreground="White"/>
            </StackPanel>
          </SplitButton>
        </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>
</UserControl>
