<UserControl x:Class="PerformanceMonitor.Ui.DeadlockGraphControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Background="{DynamicResource BackgroundBrush}"
             Focusable="True"
             PreviewMouseDown="DeadlockGraphControl_PreviewMouseDown">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Toolbar -->
        <Border Grid.Row="0" Background="{DynamicResource BackgroundDarkBrush}" Padding="8,6"
                BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,0,0,1">
            <DockPanel>
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Left">
                    <Button Content="+" Click="ZoomIn_Click" Width="28" Height="28" Padding="0" FontSize="16"
                            FontWeight="Bold" ToolTip="Zoom In"/>
                    <Button Content="&#x2212;" Click="ZoomOut_Click" Width="28" Height="28" Padding="0" FontSize="16"
                            FontWeight="Bold" Margin="4,0,0,0" ToolTip="Zoom Out"/>
                    <Button Content="Fit" Click="ZoomFit_Click" Height="28" Padding="8,0" Margin="4,0,0,0"
                            ToolTip="Zoom to Fit"/>
                    <TextBlock x:Name="ZoomLevelText" Text="100%" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundMutedBrush}" Margin="8,0,0,0" FontSize="11"/>
                    <Separator Margin="12,4"/>
                    <TextBlock x:Name="SummaryText" VerticalAlignment="Center"
                               Foreground="{DynamicResource ForegroundMutedBrush}" FontSize="11"/>
                </StackPanel>
                <TextBlock x:Name="ParallelBanner" DockPanel.Dock="Right" VerticalAlignment="Center"
                           HorizontalAlignment="Right" TextAlignment="Right"
                           Foreground="{DynamicResource WarningTextBrush}" FontSize="11" FontWeight="SemiBold"
                           Visibility="Collapsed"/>
            </DockPanel>
        </Border>

        <!-- Legend -->
        <Border Grid.Row="1" Background="{DynamicResource BackgroundDarkBrush}" Padding="8,4"
                BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,0,0,1">
            <StackPanel Orientation="Horizontal">
                <!-- Swatches track the same theme brushes the graph renders with, so the legend can't drift. -->
                <Border Width="12" Height="12" CornerRadius="2" Background="Transparent"
                        BorderThickness="2" VerticalAlignment="Center" BorderBrush="{DynamicResource DeadlockVictimBrush}"/>
                <TextBlock Text="Victim (rolled back)" VerticalAlignment="Center" FontSize="11"
                           Foreground="{DynamicResource ForegroundMutedBrush}" Margin="4,0,16,0"/>
                <TextBlock Text="&#x2192;" VerticalAlignment="Center" FontSize="15" FontWeight="Bold"
                           Foreground="{DynamicResource DeadlockEdgeBrush}"/>
                <TextBlock Text="waits for (arrow points at the lock holder)" VerticalAlignment="Center" FontSize="11"
                           Foreground="{DynamicResource ForegroundMutedBrush}" Margin="4,0,16,0"/>
            </StackPanel>
        </Border>

        <!-- Canvas + Properties Panel -->
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition x:Name="PropertiesColumn" Width="0"/>
            </Grid.ColumnDefinitions>

            <ScrollViewer Grid.Column="0" x:Name="GraphScrollViewer"
                          HorizontalScrollBarVisibility="Auto"
                          VerticalScrollBarVisibility="Auto"
                          Background="{DynamicResource BackgroundBrush}"
                          PreviewMouseWheel="GraphScrollViewer_PreviewMouseWheel"
                          PreviewMouseLeftButtonDown="GraphScrollViewer_PreviewMouseLeftButtonDown"
                          PreviewMouseMove="GraphScrollViewer_PreviewMouseMove"
                          PreviewMouseLeftButtonUp="GraphScrollViewer_PreviewMouseLeftButtonUp">
                <Canvas x:Name="GraphCanvas" ClipToBounds="False"
                        HorizontalAlignment="Left" VerticalAlignment="Top">
                    <Canvas.LayoutTransform>
                        <ScaleTransform x:Name="ZoomTransform" ScaleX="1" ScaleY="1"/>
                    </Canvas.LayoutTransform>
                </Canvas>
            </ScrollViewer>

            <!-- Empty State -->
            <StackPanel x:Name="EmptyState" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center">
                <TextBlock Text="No Deadlock Graph" FontSize="20" FontWeight="Light"
                           Foreground="{DynamicResource ForegroundMutedBrush}" HorizontalAlignment="Center"/>
                <TextBlock x:Name="EmptyStateDetail"
                           Text="This deadlock has no graph XML, or the XML could not be parsed."
                           FontSize="13" Foreground="{DynamicResource ForegroundMutedBrush}"
                           HorizontalAlignment="Center" Margin="0,8,0,0" TextWrapping="Wrap" MaxWidth="520"
                           TextAlignment="Center"/>
            </StackPanel>

            <!-- Properties Splitter -->
            <GridSplitter x:Name="PropertiesSplitter" Grid.Column="1" Width="5"
                          HorizontalAlignment="Center" VerticalAlignment="Stretch"
                          Background="{DynamicResource BorderBrush}"
                          Visibility="Collapsed"/>

            <!-- Properties Panel -->
            <Border x:Name="PropertiesPanel" Grid.Column="2"
                    Background="{DynamicResource BackgroundDarkBrush}"
                    BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1,0,0,0"
                    Visibility="Collapsed">
                <DockPanel>
                    <Border DockPanel.Dock="Top" Padding="10,8"
                            Background="{DynamicResource BackgroundBrush}"
                            BorderBrush="{DynamicResource BorderBrush}" BorderThickness="0,0,0,1">
                        <DockPanel>
                            <Button DockPanel.Dock="Right" Content="&#x2715;" Click="CloseProperties_Click"
                                    Width="24" Height="24" Padding="0" FontSize="12"
                                    VerticalAlignment="Center" ToolTip="Close Properties"
                                    Background="Transparent" BorderThickness="0"
                                    Foreground="{DynamicResource ForegroundMutedBrush}"/>
                            <TextBlock x:Name="PropertiesHeader" Text="Process Properties"
                                       FontWeight="SemiBold" FontSize="13"
                                       Foreground="{DynamicResource ForegroundBrush}"
                                       VerticalAlignment="Center"/>
                        </DockPanel>
                    </Border>
                    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled"
                                  Padding="0">
                        <StackPanel x:Name="PropertiesContent" Margin="10,8"/>
                    </ScrollViewer>
                </DockPanel>
            </Border>
        </Grid>
    </Grid>
</UserControl>
