<local:GenericPullRequestDetailView x:Class="GitHub.VisualStudio.Views.GitHubPane.PullRequestDetailView"
                                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                                    xmlns:ghfvs="https://github.com/github/VisualStudio"
                                    xmlns:local="clr-namespace:GitHub.VisualStudio.Views.GitHubPane"
                                    xmlns:v="clr-namespace:GitHub.VisualStudio.Views"
                                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                                    xmlns:markdig="clr-namespace:Markdig.Wpf;assembly=Markdig.Wpf"
                                    xmlns:vsui="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.14.0"
                                    xmlns:theming="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Imaging"
                                    Background="{DynamicResource GitHubVsToolWindowBackground}"
                                    Foreground="{DynamicResource GitHubVsWindowText}"
                                    theming:ImageThemingUtilities.ImageBackgroundColor="{Binding RelativeSource={RelativeSource Self}, Path=Background.Color}"
                                    DataContext="{Binding ViewModel}"
                                    d:DesignWidth="356"
                                    d:DesignHeight="800"
                                    mc:Ignorable="d">
    <d:DesignProperties.DataContext>
        <Binding>
            <Binding.Source>
                <ghfvs:PullRequestDetailViewModelDesigner SourceBranchDisplayName="shana/error-handling-a-ridiculously-long-branch-name-because-why-not"
                                                          TargetBranchDisplayName="master-is-always-stable"
                                                          CommentCount="10">
                    <ghfvs:PullRequestDetailViewModelDesigner.UpdateState>
                        <ghfvs:PullRequestUpdateStateDesigner CommitsAhead="0" CommitsBehind="0" UpToDate="True"/>
                    </ghfvs:PullRequestDetailViewModelDesigner.UpdateState>
                    <ghfvs:PullRequestDetailViewModelDesigner.OperationError>
                        Unable to connect to the internets over here!
                    </ghfvs:PullRequestDetailViewModelDesigner.OperationError>
                </ghfvs:PullRequestDetailViewModelDesigner>
            </Binding.Source>
        </Binding>
    </d:DesignProperties.DataContext>

    <Control.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ghfvs:SharedDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/SharedDictionary.xaml" />
                <ghfvs:SharedDictionaryManager Source="pack://application:,,,/GitHub.UI;component/SharedDictionary.xaml" />
                <ghfvs:SharedDictionaryManager Source="pack://application:,,,/GitHub.UI.Reactive;component/SharedDictionary.xaml" />
                <ghfvs:SharedDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/Styles/Markdown.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="Separator" TargetType="Rectangle">
                <Setter Property="Fill" Value="{DynamicResource GitHubHeaderSeparatorBrush}"/>
            </Style>

            <Style x:Key="CheckoutMessage" TargetType="TextBlock">
                <Setter Property="Margin" Value="0 4"/>
            </Style>

            <Style x:Key="CheckoutErrorMessage" TargetType="TextBlock">
                <Setter Property="Foreground" Value="Red" />
            </Style>

            <ghfvs:AllCapsConverter x:Key="AllCaps"/>
        </ResourceDictionary>
    </Control.Resources>

    <FrameworkElement.CommandBindings>
        <CommandBinding Command="{x:Static markdig:Commands.Hyperlink}" Executed="OpenHyperlink" />
    </FrameworkElement.CommandBindings>

    <DockPanel Grid.IsSharedSizeScope="True">
        <StackPanel DockPanel.Dock="Top"
                    Orientation="Vertical"
                    Margin="8 0 0 0">

            <!-- Title -->
            <TextBlock Style="{DynamicResource {x:Static vsui:VsResourceKeys.TextBlockEnvironment122PercentFontSizeStyleKey}}"
                       TextWrapping="Wrap"
                       Margin="0 0 5 3"
                       Text="{Binding Model.Title}"/>

            <!-- State and branches -->
            <StackPanel Orientation="Horizontal">
                <v:IssueishStateBadge DataContext="{Binding Model.State}" VerticalAlignment="Center"/>

                <Rectangle Margin="9 0" Width="1" Height="12" VerticalAlignment="Center" Style="{DynamicResource Separator}" />

                <Border Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" CornerRadius="2" Padding="5 1" Background="{DynamicResource GitHubBranchNameBackgroundBrush}">
                    <TextBlock FontFamily="Consolas" TextTrimming="CharacterEllipsis" ToolTip="{Binding TargetBranchDisplayName, Mode=OneWay}" Text="{Binding TargetBranchDisplayName, Mode=OneWay}" />
                </Border>

                <ghfvs:OcticonImage Grid.Column="2" VerticalAlignment="Center" Margin="5 3" Icon="arrow_left" />

                <Border Grid.Column="3" HorizontalAlignment="Left" VerticalAlignment="Center" CornerRadius="2" Padding="5 1" Background="{DynamicResource GitHubBranchNameBackgroundBrush}">
                    <TextBlock FontFamily="Consolas" TextTrimming="CharacterEllipsis" ToolTip="{Binding SourceBranchDisplayName, Mode=OneWay}" Text="{Binding SourceBranchDisplayName, Mode=OneWay}" />
                </Border>

            </StackPanel>

            <!-- Updated at -->
            <TextBlock Opacity="0.5"
                       Text="{Binding Model.UpdatedAt, StringFormat={x:Static ghfvs:Resources.UpdatedFormat}, Converter={ghfvs:DurationToStringConverter}, Mode=OneWay}"/>

            <!-- User actions -->
            <StackPanel Orientation="Horizontal" 
                        Margin="0 0 0 -4"
                        ghfvs:ScrollingVerticalStackPanel.IsFixed="true">
                <ghfvs:GitHubActionLink Margin="0 6" Command="{Binding OpenOnGitHub}">
                    View on GitHub
                </ghfvs:GitHubActionLink>

                <Rectangle Margin="5 0" Width="1" Height="12" VerticalAlignment="Center" Style="{DynamicResource Separator}" />

                <StackPanel Orientation="Horizontal">
                    <ghfvs:OcticonImage Margin="0 2 4 0" Foreground="{DynamicResource VsBrush.GrayText}" Width="12" Height="12" Icon="comment"/>
                    <ghfvs:GitHubActionLink Margin="0 6" Command="{Binding OpenConversation}" Content="{Binding Model.CommentCount}" />
                </StackPanel>

                <Rectangle Margin="5 0" Width="1" Height="12" VerticalAlignment="Center" Style="{DynamicResource Separator}" />

                <!-- Checkout pull request button -->
                <ghfvs:GitHubActionLink Command="{Binding Checkout}"
                                        Content="{Binding CheckoutState.Caption}"
                                        VerticalAlignment="Center"
                                        TextTrimming="CharacterEllipsis"
                                        Visibility="{Binding CheckoutState, Converter={ghfvs:NullToVisibilityConverter}}"
                                        ToolTip="{Binding CheckoutState.ToolTip}"
                                        ToolTipService.ShowOnDisabled="True"/>

                <!-- Pull/push buttons -->
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center"
                                Visibility="{Binding UpdateState.UpToDate, FallbackValue=Collapsed, Converter={ghfvs:BooleanToInverseVisibilityConverter}}">
                    <ghfvs:OcticonImage Icon="arrow_down"/>
                    <TextBlock Text="{Binding UpdateState.CommitsBehind}" VerticalAlignment="Center"/>
                    <ghfvs:GitHubActionLink Content="{x:Static ghfvs:Resources.Pull}"
                                            Command="{Binding Pull}"
                                            Margin="4,0"
                                            ToolTip="{Binding UpdateState.PullToolTip}"
                                            ToolTipService.ShowOnDisabled="True"
                                            VerticalAlignment="Center"/>
                    <ghfvs:OcticonImage Icon="arrow_up"/>
                    <TextBlock Text="{Binding UpdateState.CommitsAhead}" VerticalAlignment="Center"/>
                    <ghfvs:GitHubActionLink Content="{x:Static ghfvs:Resources.Push}"
                                            Command="{Binding Push}"
                                            Margin="4,0"
                                            ToolTip="{Binding UpdateState.PushToolTip}"
                                            ToolTipService.ShowOnDisabled="True"
                                            VerticalAlignment="Center"/>
                    <!-- Sync submodules -->
                    <ghfvs:OcticonImage Icon="package"
                                        Visibility="{Binding UpdateState.SyncSubmodulesEnabled, FallbackValue=Collapsed, Converter={ghfvs:BooleanToVisibilityConverter}}" />
                    <TextBlock  Margin="4 0 0 0" Text="{Binding UpdateState.SubmodulesToSync}" VerticalAlignment="Center"
                                Visibility="{Binding UpdateState.SyncSubmodulesEnabled, FallbackValue=Collapsed, Converter={ghfvs:BooleanToVisibilityConverter}}" />
                    <ghfvs:GitHubActionLink Content="{x:Static ghfvs:Resources.Sync}"
                                            Command="{Binding SyncSubmodules}"
                                            Margin="4 0"
                                            ToolTip="{Binding UpdateState.SyncSubmodulesToolTip}"
                                            Visibility="{Binding UpdateState.SyncSubmodulesEnabled, FallbackValue=Collapsed, Converter={ghfvs:BooleanToVisibilityConverter}}" />
                </StackPanel>

                <!-- Branch checked out and up-to-date -->
                <TextBlock VerticalAlignment="Center" TextWrapping="Wrap"
                           Visibility="{Binding UpdateState.UpToDate, FallbackValue=Collapsed, Converter={ghfvs:BooleanToVisibilityConverter}}">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource CheckoutMessage}">
                            <Setter Property="Visibility" Value="Collapsed"/>
                            <Style.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding UpdateState.CommitsAhead}" Value="0"/>
                                        <Condition Binding="{Binding UpdateState.CommitsBehind}" Value="0"/>
                                    </MultiDataTrigger.Conditions>
                                    <MultiDataTrigger.Setters>
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </MultiDataTrigger.Setters>
                                </MultiDataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>

                    <ghfvs:OcticonImage Icon="check" Foreground="#6CC644" Margin="0 0 0 -4"/>
                    <Run Text="{x:Static ghfvs:Resources.LocalBranchUpToDate}"/>
                </TextBlock>
            </StackPanel>
            <!-- /User actions -->

            <!-- Git operation error message -->
            <TextBox Foreground="Red" 
                     Margin="-2 4"
                     Text="{Binding OperationError, Mode=OneWay}"
                     Style="{StaticResource FlatReadOnlyTextBox}"
                     Visibility="{Binding OperationError, Converter={ghfvs:NullToVisibilityConverter}}"/>
        </StackPanel>

        <Rectangle DockPanel.Dock="Top" Style="{StaticResource Separator}" Height="2" Margin="0,5,0,0"/>

        <ScrollViewer CanContentScroll="True"
                      Padding="8 0 0 0"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
            <ghfvs:ScrollingVerticalStackPanel>
                <!-- Author and open time -->
                <ghfvs:SectionControl Name="descriptionSection"
                                      HeaderText="{x:Static ghfvs:Resources.Description}"
                                      Margin="0 4 0 0"
                                      IsExpanded="False"
                                      ghfvs:ScrollingVerticalStackPanel.IsFixed="true">
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal" Margin="0 4 0 0">
                            <v:ActorAvatarView ViewModel="{Binding Author}"
                                               VerticalAlignment="Bottom"
                                               Width="16"
                                               Height="16"
                                               Margin="0,0,0,1"/>

                            <TextBlock VerticalAlignment="Center" Margin="4 0" TextWrapping="Wrap">
                                <Run FontWeight="SemiBold" Text="{Binding Model.Author.Login, Mode=OneWay}" />
                                <Run Text="{x:Static ghfvs:Resources.Wrote}" />
                            </TextBlock>
                        </StackPanel>
                        <!-- PR Body -->
                        <markdig:MarkdownViewer Name="bodyMarkdown"
                                                Margin="2 4 10 6"
                                                Markdown="{Binding Body}"/>
                    </StackPanel>
                </ghfvs:SectionControl>

                <ghfvs:SectionControl Name="reviewsSection"
                                      HeaderText="{x:Static ghfvs:Resources.Reviewers}"
                                      IsExpanded="True"
                                      Margin="0 8 0 0"
                                      ghfvs:ScrollingVerticalStackPanel.IsFixed="true">
                    <ItemsControl ItemsSource="{Binding Reviews}" Margin="0 4 12 4">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <local:PullRequestReviewSummaryView/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ghfvs:SectionControl>

                <ghfvs:SectionControl Name="checksSection"
                                      HeaderText="Checks"
                                      IsExpanded="True"
                                      Margin="0 8 0 0"
                                      ghfvs:ScrollingVerticalStackPanel.IsFixed="true"
                                      Visibility="{Binding Checks.Count, Converter={ghfvs:CountToVisibilityConverter}}">
                    <ItemsControl ItemsSource="{Binding Checks}" Margin="0 4 12 4">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </ghfvs:SectionControl>

                <!-- Files changed -->
                <ghfvs:SectionControl Name="changesSection" 
                                      Grid.Row="4"
                                      IsExpanded="True"
                                      HeaderText="{Binding Files.ChangedFilesCount, StringFormat={x:Static ghfvs:Resources.ChangesCountFormat}}"
                                      Margin="0 8 10 0"
                                      ghfvs:ScrollingVerticalStackPanel.IsFixed="true"/>

                <!-- Put the changes tree outside its expander, so it can scroll horizontally 
                     while the header remains fixed -->
                <local:PullRequestFilesView DataContext="{Binding Files}"
                                            Margin="8,0,0,0"
                                            Visibility="{Binding ElementName=changesSection, Path=IsExpanded, Converter={ghfvs:BooleanToVisibilityConverter}}"/>
            </ghfvs:ScrollingVerticalStackPanel>
        </ScrollViewer>
    </DockPanel>
</local:GenericPullRequestDetailView>