<Application x:Class="RoslynPad.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:formatting="clr-namespace:RoslynPad.Formatting"
             xmlns:codeAnalysis="clr-namespace:Microsoft.CodeAnalysis;assembly=Microsoft.CodeAnalysis"
             xmlns:codeActions="clr-namespace:Microsoft.CodeAnalysis.CodeActions;assembly=Microsoft.CodeAnalysis.Workspaces"
             xmlns:codeFixes="clr-namespace:RoslynPad.Roslyn.CodeFixes;assembly=RoslynPad.Roslyn"
             xmlns:cc="clr-namespace:ICSharpCode.AvalonEdit.CodeCompletion;assembly=ICSharpCode.AvalonEdit"
             xmlns:controls="clr-namespace:RoslynPad.Controls"
             xmlns:rp="clr-namespace:RoslynPad"
             xmlns:converters="clr-namespace:RoslynPad.Converters"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Icons.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
            <converters:DoubleToPercentageTextConverter x:Key="DoubleToPercentageTextConverter" />
            <formatting:BooleanToVisibilityConverter x:Key="BooleanToVisibilityHiddenConverter"
                                                     FalseValue="Hidden" />

            <ControlTemplate x:Key="ErrorTemplate">
                <Border BorderBrush="Red"
                        ToolTip="{Binding AdornedElement.(Validation.Errors)[0].ErrorContent, ElementName=controlWithError}"
                        BorderThickness="1">
                    <AdornedElementPlaceholder x:Name="controlWithError" />
                </Border>
            </ControlTemplate>

            <HierarchicalDataTemplate DataType="{x:Type codeFixes:CodeFixCollection}"
                                      ItemsSource="{Binding Fixes, Mode=OneTime}">
                <TextBlock Text="{Binding Provider, Mode=OneTime}" />
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type codeFixes:CodeFix}"
                                      ItemsSource="{Binding Action, Mode=OneTime, Converter={formatting:CodeActionsConverter}}">
                <ContentPresenter Content="{Binding Action, Mode=OneTime}" />
            </HierarchicalDataTemplate>

            <DataTemplate DataType="{x:Type codeActions:CodeAction}">
                <DockPanel>
                    <Image Source="{Binding Mode=OneTime, Converter={formatting:CodeActionToGlyphConverter}}" />
                    <TextBlock Text="{Binding Title, Mode=OneTime}" />
                </DockPanel>
            </DataTemplate>

            <DataTemplate DataType="{x:Type codeAnalysis:Diagnostic}">
                <TextBlock Text="{Binding Descriptor.Title, Mode=OneTime}" />
            </DataTemplate>

            <DataTemplate DataType="{x:Type ImageSource}">
                <Image Source="{Binding Mode=OneTime}" />
            </DataTemplate>

            <Style TargetType="cc:InsightWindow">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="cc:InsightWindow">
                            <Border BorderThickness="{TemplateBinding BorderThickness}"
                                    Padding="{TemplateBinding Padding}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="Title"
                   TargetType="TextBlock">
                <Setter Property="FontSize"
                        Value="16" />
            </Style>

            <Style x:Key="DialogButton"
                   TargetType="Button">
                <Setter Property="Padding"
                        Value="10,2,10,2" />
                <Setter Property="Margin"
                        Value="10,0,0,0" />
            </Style>

            <Style x:Key="LinkButton"
                   TargetType="Button">
                <Setter Property="Background"
                        Value="Transparent" />
                <Setter Property="BorderThickness"
                        Value="0" />
                <Setter Property="Padding"
                        Value="0" />
                <Setter Property="Foreground"
                        Value="DodgerBlue" />
                <Setter Property="Cursor"
                        Value="Hand" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <TextBlock Name="Text"
                                       Text="{TemplateBinding Content}"
                                       Background="{TemplateBinding Background}"
                                       Foreground="{TemplateBinding Foreground}" />
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver"
                                         Value="True">
                                    <Setter TargetName="Text"
                                            Property="TextBlock.TextDecorations"
                                            Value="Underline" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Stroke"
                             Color="#FF818181" />
            <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Fill"
                             Color="#FFFFFFFF" />
            <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Stroke"
                             Color="#FF27C7F7" />
            <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Fill"
                             Color="#FFCCEEFB" />
            <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Stroke"
                             Color="#FF262626" />
            <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Fill"
                             Color="#FF595959" />
            <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Checked.Stroke"
                             Color="#FF1CC4F7" />
            <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Checked.Fill"
                             Color="#FF82DFFB" />

            <Style TargetType="{x:Type controls:TreeListViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type controls:TreeListViewItem}">
                            <Border Name="OuterBd"
                                    BorderBrush="#eeeeee">
                                <DockPanel>
                                    <Border Name="Bd"
                                            DockPanel.Dock="Top"
                                            Background="{TemplateBinding Background}"
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            BorderThickness="{TemplateBinding BorderThickness}"
                                            Padding="{TemplateBinding Padding}">
                                        <ContentPresenter x:Name="PART_Header"
                                                          ContentSource="Header" />
                                    </Border>
                                    <ItemsPresenter x:Name="ItemsHost" />
                                </DockPanel>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="ShowSeparator"
                                         Value="True">
                                    <Setter TargetName="OuterBd"
                                            Property="Margin"
                                            Value="0,2,0,2" />
                                    <Setter TargetName="OuterBd"
                                            Property="BorderThickness"
                                            Value="0,0,0,1" />
                                </Trigger>
                                <Trigger Property="IsExpanded"
                                         Value="false">
                                    <Setter TargetName="ItemsHost"
                                            Property="Visibility"
                                            Value="Collapsed" />
                                </Trigger>
                                <Trigger Property="IsSelected"
                                         Value="true">
                                    <Setter TargetName="Bd"
                                            Property="Background"
                                            Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Fill}" />
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected"
                                                   Value="true" />
                                        <Condition Property="IsSelectionActive"
                                                   Value="false" />
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="Bd"
                                            Property="Background"
                                            Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                                </MultiTrigger>
                                <Trigger Property="IsEnabled"
                                         Value="false">
                                    <Setter Property="Foreground"
                                            Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <controls:LevelToIndentConverter x:Key="LevelToIndentConverter" />

            <PathGeometry x:Key="TreeArrow">
                <PathGeometry.Figures>
                    <PathFigureCollection>
                        <PathFigure IsFilled="True"
                                    StartPoint="0 0"
                                    IsClosed="True">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <LineSegment Point="0 6" />
                                    <LineSegment Point="6 0" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>

            <Style x:Key="TreeListViewToggleStyle"
                   TargetType="{x:Type ToggleButton}">
                <Setter Property="Margin"
                        Value="{Binding Path=Level, Mode=OneTime, Converter={StaticResource LevelToIndentConverter}, RelativeSource={RelativeSource AncestorType=controls:TreeListViewItem}}" />
                <Setter Property="IsChecked"
                        Value="{Binding Path=IsExpanded, RelativeSource={RelativeSource AncestorType=controls:TreeListViewItem}}" />
                <Setter Property="Visibility"
                        Value="{Binding Path=HasItems, Converter={StaticResource BooleanToVisibilityHiddenConverter}, RelativeSource={RelativeSource AncestorType=controls:TreeListViewItem}}" />
                <Setter Property="ClickMode"
                        Value="Press" />
                <Setter Property="Focusable"
                        Value="False" />
                <Setter Property="Width"
                        Value="16" />
                <Setter Property="Height"
                        Value="16" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                            <Border Width="16"
                                    Height="16"
                                    Background="Transparent"
                                    Padding="5,5,5,5">
                                <Path x:Name="ExpandPath"
                                      Fill="{StaticResource TreeViewItem.TreeArrow.Static.Fill}"
                                      Stroke="{StaticResource TreeViewItem.TreeArrow.Static.Stroke}"
                                      Data="{StaticResource TreeArrow}">
                                    <Path.RenderTransform>
                                        <RotateTransform Angle="135"
                                                         CenterX="3"
                                                         CenterY="3" />
                                    </Path.RenderTransform>
                                </Path>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked"
                                         Value="True">
                                    <Setter TargetName="ExpandPath"
                                            Property="RenderTransform">
                                        <Setter.Value>
                                            <RotateTransform Angle="180"
                                                             CenterX="3"
                                                             CenterY="3" />
                                        </Setter.Value>
                                    </Setter>
                                    <Setter TargetName="ExpandPath"
                                            Property="Fill"
                                            Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Fill}" />
                                    <Setter TargetName="ExpandPath"
                                            Property="Stroke"
                                            Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Stroke}" />
                                </Trigger>
                                <Trigger Property="IsMouseOver"
                                         Value="True">
                                    <Setter TargetName="ExpandPath"
                                            Property="Stroke"
                                            Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Stroke}" />
                                    <Setter TargetName="ExpandPath"
                                            Property="Fill"
                                            Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Fill}" />
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsMouseOver"
                                                   Value="True" />
                                        <Condition Property="IsChecked"
                                                   Value="True" />
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="ExpandPath"
                                            Property="Stroke"
                                            Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Stroke}" />
                                    <Setter TargetName="ExpandPath"
                                            Property="Fill"
                                            Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Fill}" />
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <DataTemplate x:Key="{DataTemplateKey {x:Static rp:XamlTypes.RestoreResultObject}}"
                          DataType="{x:Static rp:XamlTypes.RestoreResultObject}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="ResultNameGroup" />
                        <ColumnDefinition Width="1" />
                        <ColumnDefinition SharedSizeGroup="ResultValueGroup" />
                        <ColumnDefinition Width="1" />
                        <ColumnDefinition SharedSizeGroup="ResultTypeGroup" />
                    </Grid.ColumnDefinitions>
                    <DockPanel Grid.Column="0">
                        <Button Command="Copy"
                                Padding="0"
                                Margin="2 0 5 0"
                                VerticalAlignment="Top"
                                KeyboardNavigation.IsTabStop="False"
                                Background="Transparent"
                                BorderBrush="Transparent">
                            <Image Source="{StaticResource Copy}"
                                   ToolTip="Copy"
                                   Height="12" />
                        </Button>
                        <Image Name="Icon"
                               Source="{StaticResource ExceptionMarker}"
                               VerticalAlignment="Top"
                               Width="12"
                               Height="12"
                               Margin="0 0 5 0" />
                        <TextBlock Text="Restore Error"
                                   Margin="0 0 5 0" />
                    </DockPanel>
                    <TextBlock Grid.Column="2"
                               Text="{Binding Message, Mode=OneTime}"
                               TextWrapping="Wrap" />
                </Grid>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Severity}"
                                 Value="Warning">
                        <Setter TargetName="Icon"
                                Property="Source"
                                Value="{StaticResource WarningMarker}" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>

            <DataTemplate x:Key="{DataTemplateKey {x:Static rp:XamlTypes.CompilationErrorResultObject}}"
                          DataType="{x:Static rp:XamlTypes.CompilationErrorResultObject}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="ResultNameGroup" />
                        <ColumnDefinition Width="1" />
                        <ColumnDefinition SharedSizeGroup="ResultValueGroup" />
                        <ColumnDefinition Width="1" />
                        <ColumnDefinition SharedSizeGroup="ResultTypeGroup" />
                    </Grid.ColumnDefinitions>
                    <DockPanel Grid.Column="0">
                        <Button Command="Copy"
                                Padding="0"
                                Margin="2 0 5 0"
                                VerticalAlignment="Top"
                                KeyboardNavigation.IsTabStop="False"
                                Background="Transparent"
                                BorderBrush="Transparent">
                            <Image Source="{StaticResource Copy}"
                                   ToolTip="Copy"
                                   Height="12" />
                        </Button>
                        <Image Name="Icon"
                               Source="{StaticResource ExceptionMarker}"
                               VerticalAlignment="Top"
                               Width="12"
                               Height="12"
                               Margin="0 0 5 0" />
                        <TextBlock Text="{Binding Severity, Mode=OneTime}"
                                   Margin="0 0 5 0" />
                        <TextBlock Text="{Binding ErrorCode, Mode=OneTime}" />
                    </DockPanel>
                    <TextBlock Grid.Column="2"
                               Text="{Binding Message, Mode=OneTime}"
                               TextWrapping="Wrap" />
                    <TextBlock Grid.Column="4"
                               VerticalAlignment="Top"
                               TextWrapping="Wrap">
                        <Run Text="{Binding Line, Mode=OneTime}" />,
                        <Run Text="{Binding Column, Mode=OneTime}" />
                    </TextBlock>
                </Grid>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Severity}"
                                 Value="Warning">
                        <Setter TargetName="Icon"
                                Property="Source"
                                Value="{StaticResource WarningMarker}" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>

            <HierarchicalDataTemplate x:Key="{DataTemplateKey {x:Static rp:XamlTypes.ResultObject}}"
                                      DataType="{x:Static rp:XamlTypes.ResultObject}"
                                      ItemsSource="{Binding Children, Mode=OneTime}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="ResultNameGroup" />
                        <ColumnDefinition Width="1" />
                        <ColumnDefinition SharedSizeGroup="ResultValueGroup" />
                        <ColumnDefinition Width="1" />
                        <ColumnDefinition SharedSizeGroup="ResultTypeGroup" />
                    </Grid.ColumnDefinitions>
                    <DockPanel Grid.Column="0">
                        <ToggleButton Style="{StaticResource TreeListViewToggleStyle}" />
                        <Button Command="Copy"
                                Padding="0"
                                Margin="2 0 5 0"
                                VerticalAlignment="Top"
                                KeyboardNavigation.IsTabStop="False"
                                Background="Transparent"
                                BorderBrush="Transparent">
                            <Image Source="{StaticResource Copy}"
                                   ToolTip="Copy Value"
                                   Height="12" />
                        </Button>
                        <TextBlock Text="{Binding Header, Mode=OneTime}" />
                    </DockPanel>
                    <TextBlock Grid.Column="2"
                               Text="{Binding Value, Mode=OneTime}"
                               TextWrapping="Wrap"
                               Margin="{Binding Path=Level, Mode=OneTime, Converter={StaticResource LevelToIndentConverter}, RelativeSource={RelativeSource AncestorType=controls:TreeListViewItem}}" />
                    <TextBlock Grid.Column="4"
                               TextWrapping="Wrap"
                               Text="{Binding Type, Mode=OneTime}"
                               Margin="{Binding Path=Level, Mode=OneTime, Converter={StaticResource LevelToIndentConverter}, RelativeSource={RelativeSource AncestorType=controls:TreeListViewItem}}" />
                </Grid>
            </HierarchicalDataTemplate>

            <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" Value="0.5" />
                    </Trigger>
                </Style.Triggers>
            </Style>

        </ResourceDictionary>
    </Application.Resources>
</Application>
