<UserControl x:Class="RoslynPad.DocumentView"
             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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:controls="clr-namespace:RoslynPad.Controls"
             xmlns:ui="clr-namespace:RoslynPad.UI;assembly=RoslynPad.Common.UI"
             xmlns:editor="clr-namespace:RoslynPad.Editor;assembly=RoslynPad.Editor.Windows"
             mc:Ignorable="d"
             d:DataContext="{d:DesignInstance ui:OpenDocumentViewModel}">
    <FrameworkElement.InputBindings>
        <KeyBinding Key="F5"
                    Command="{Binding RunCommand, Mode=OneTime}" />
        <KeyBinding Gesture="Shift+F5"
                    Command="{Binding TerminateCommand, Mode=OneTime}" />
        <KeyBinding Gesture="Ctrl+S"
                    Command="{Binding SaveCommand, Mode=OneTime}" />
        <KeyBinding Gesture="Ctrl+D"
                    Command="{Binding FormatDocumentCommand, Mode=OneTime}" />
        <KeyBinding Gesture="Ctrl+K"
                    Command="{Binding CommentSelectionCommand, Mode=OneTime}" />
        <KeyBinding Gesture="Ctrl+U"
                    Command="{Binding UncommentSelectionCommand, Mode=OneTime}" />
        <KeyBinding Gesture="F2"
                    Command="{Binding RenameSymbolCommand, Mode=OneTime}" />
    </FrameworkElement.InputBindings>
    <DockPanel>
        <StatusBar DockPanel.Dock="Bottom">
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <DockPanel LastChildFill="False" />
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>
            <StatusBarItem>
                <TextBlock>
                    Ln <Run Name="Ln" />
                    Col <Run Name="Col" />
                </TextBlock>
            </StatusBarItem>
        </StatusBar>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition />
                <RowDefinition Height="5" />
                <RowDefinition Name="ResultPaneRow"
                               Height="0" />
            </Grid.RowDefinitions>
            <ToolBar Grid.ColumnSpan="3"
                     Background="Transparent"
                     BorderBrush="DarkGray"
                     BorderThickness="0 0 0 1">
                <Button Command="{Binding RunCommand, Mode=OneTime}"
                        ToolTip="Run (F5)">
                    <Grid>
                        <Image>
                            <Image.Style>
                                <Style TargetType="Image">
                                    <Setter Property="Source"
                                            Value="{StaticResource Run}" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsLiveMode}"
                                                     Value="True">
                                            <Setter Property="Source"
                                                    Value="{StaticResource Rocket}" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                        <ProgressBar VerticalAlignment="Bottom"
                                     Height="4"
                                     IsIndeterminate="{Binding IsRunning}"
                                     BorderThickness="0"
                                     Visibility="{Binding IsRunning, Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </Grid>
                </Button>
                <Menu Background="Transparent">
                    <MenuItem Header="▾"
                              Padding="0"
                              FontFamily="Segoe UI Symbol">
                        <MenuItem Command="{Binding ToggleLiveModeCommand}"
                                  Icon="{StaticResource Rocket}">
                            <MenuItem.Style>
                                <Style TargetType="MenuItem">
                                    <Setter Property="Header"
                                            Value="Live Mode" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsLiveMode}"
                                                     Value="True">
                                            <Setter Property="Header"
                                                    Value="Turn off Live Mode" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </MenuItem.Style>
                        </MenuItem>
                    </MenuItem>
                </Menu>
                <Grid Visibility="{Binding HasReportedProgress, Converter={StaticResource BooleanToVisibilityConverter}}"
                      Width="60"
                      Margin="5, 0">
                    <ProgressBar Value="{Binding ReportedProgress, Mode=OneWay}"
                                 Minimum="0.0"
                                 Maximum="1.0" />
                    <TextBlock Text="{Binding ReportedProgress, Mode=OneWay, Converter={StaticResource DoubleToPercentageTextConverter}}"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center" />
                </Grid>
                <Button Command="{Binding TerminateCommand, Mode=OneTime}"
                        ToolTip="Terminate process (Shift+F5)"
                        Content="{StaticResource Stop}"
                        IsEnabled="{Binding IsRunning}" />
                <ComboBox ItemsSource="{Binding AvailablePlatforms, Mode=OneTime}"
                          SelectedItem="{Binding Platform}"
                          HorizontalContentAlignment="Stretch" />
                <Button Content="{StaticResource SetPlatform}"
                        ToolTip="Set platform as default"
                        Command="{Binding SetDefaultPlatformCommand, Mode=OneTime}"  />
                <Separator />
                <Button Command="{Binding SaveCommand, Mode=OneTime}"
                        ToolTip="Save (Ctrl+S)"
                        Content="{StaticResource Save}" />
                <Button Command="{Binding OpenBuildPathCommand, Mode=OneTime}"
                        ToolTip="Open Build Folder"
                        Content="{StaticResource PackageFolder}" />
                <Separator />
                <Menu Background="Transparent"
                      Name="RootMenu">
                    <MenuItem Name="RootNuGetMenu"
                              IsSubmenuOpen="{Binding NuGet.IsPackagesMenuOpen}"
                              ItemsSource="{Binding NuGet.Packages}"
                              Header="{Binding Mode=OneTime}"
                              ToolTip="NuGet Packages"
                              IsTextSearchEnabled="True">
                        <MenuItem.HeaderTemplate>
                            <DataTemplate DataType="{x:Type ui:OpenDocumentViewModel}">
                                <Grid>
                                    <Image Source="{StaticResource NuGet}" />
                                    <ProgressBar Name="Spinner"
                                                 VerticalAlignment="Bottom"
                                                 Height="4"
                                                 BorderThickness="0"
                                                 IsIndeterminate="{Binding NuGet.IsSearching}"
                                                 Visibility="{Binding NuGet.IsSearching, Converter={StaticResource BooleanToVisibilityConverter}}" />
                                </Grid>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding IsRestoring}"
                                                 Value="True">
                                        <Setter TargetName="Spinner"
                                                Property="Foreground"
                                                Value="Orange" />
                                        <Setter TargetName="Spinner"
                                                Property="Visibility"
                                                Value="Visible" />
                                        <Setter TargetName="Spinner"
                                                Property="IsIndeterminate"
                                                Value="True" />
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </MenuItem.HeaderTemplate>
                        <ItemsControl.ItemContainerStyle>
                            <Style TargetType="MenuItem">
                                <Setter Property="Command"
                                        Value="{Binding ElementName=RootMenu, Path=DataContext.NuGet.InstallPackageCommand, Mode=OneTime}" />
                                <Setter Property="CommandParameter"
                                        Value="{Binding Mode=OneTime}" />
                                <Setter Property="Icon"
                                        Value="{Binding Mode=OneTime}" />
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="{x:Type ui:PackageData}">
                                <TextBlock>
                                    <Run Text="{Binding Id, Mode=OneTime}" />
                                    <Run Text="{Binding Version, Mode=OneTime}" />
                                </TextBlock>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.Resources>
                            <DataTemplate DataType="{x:Type ui:PackageData}">
                                <Menu Background="Transparent"
                                      ToolTip="Other versions">
                                    <MenuItem ItemsSource="{Binding OtherVersions, Mode=OneTime}"
                                              DisplayMemberPath="Version"
                                              Padding="2">
                                        <MenuItem.Header>
                                            <TextBlock FontWeight="Bold"
                                                       FontFamily="Segoe UI Symbol"
                                                       Text=""
                                                       FontSize="8" />
                                        </MenuItem.Header>
                                        <ItemsControl.ItemContainerStyle>
                                            <Style TargetType="MenuItem">
                                                <Setter Property="Command"
                                                        Value="{Binding ElementName=RootMenu, Path=DataContext.NuGet.InstallPackageCommand, Mode=OneTime}" />
                                                <Setter Property="CommandParameter"
                                                        Value="{Binding Mode=OneTime}" />
                                            </Style>
                                        </ItemsControl.ItemContainerStyle>
                                    </MenuItem>
                                </Menu>
                            </DataTemplate>
                        </ItemsControl.Resources>
                    </MenuItem>
                </Menu>
                <Grid>
                    <TextBox Name="NuGetSearch"
                             BorderBrush="#eee"
                             PreviewKeyDown="SearchTerm_OnPreviewKeyDown"
                             Text="{Binding NuGet.SearchTerm, Delay=500, UpdateSourceTrigger=PropertyChanged}"
                             Style="{StaticResource {x:Static ToolBar.TextBoxStyleKey}}"
                             Width="200" />
                    <TextBlock IsHitTestVisible="False"
                               Text="Search NuGet (Ctrl+T)"
                               VerticalAlignment="Center"
                               Margin="10,0,0,0"
                               Foreground="DarkGray">
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Visibility"
                                        Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Text, ElementName=NuGetSearch}"
                                                 Value="">
                                        <Setter Property="Visibility"
                                                Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </Grid>
                <Decorator VerticalAlignment="Center">
                    <CheckBox IsChecked="{Binding NuGet.Prerelease}"
                              ToolTip="Include prerelease packages"
                              Content="Prerelease" />
                </Decorator>
                <Separator />
                <Button Content="{StaticResource FormatDocument}"
                        ToolTip="Format Document (Ctrl+D)"
                        Command="{Binding FormatDocumentCommand, Mode=OneTime}" />
                <Button Content="{StaticResource CommentCode}"
                        ToolTip="Comment Selection (Ctrl+K)"
                        Command="{Binding CommentSelectionCommand, Mode=OneTime}" />
                <Button Content="{StaticResource UncommentCode}"
                        ToolTip="Uncomment Selection (Ctrl+U)"
                        Command="{Binding UncommentSelectionCommand, Mode=OneTime}" />
                <Button Content="{StaticResource Rename}"
                        ToolTip="Rename Symbol (F2)"
                        Command="{Binding RenameSymbolCommand, Mode=OneTime}" />
            </ToolBar>
            <editor:RoslynCodeEditor x:Name="Editor"
                                     x:FieldModifier="private"
                                     ContextActionsIcon="{StaticResource Bulb}"
                                     IsBraceCompletionEnabled="{Binding MainViewModel.Settings.EnableBraceCompletion}"
                                     FontFamily="Consolas"
                                     Grid.Row="1"
                                     Grid.Column="0"
                                     Loaded="Editor_OnLoaded" />
            <GridSplitter Grid.Row="2"
                          Grid.Column="0"
                          Background="#ccc"
                          ResizeBehavior="PreviousAndNext"
                          ResizeDirection="Rows"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch" />

            <TabControl Grid.Column="0"
                        Grid.Row="3"
                        TabStripPlacement="Bottom"
                        SelectionChanged="OnTabSelectionChanged">
                <TabItem Header="Results">
                    <DockPanel Grid.IsSharedSizeScope="True">
                        <ScrollViewer DockPanel.Dock="Top"
                                      Name="HeaderScroll"
                                      HorizontalScrollBarVisibility="Hidden"
                                      VerticalScrollBarVisibility="Disabled">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="150"
                                                      SharedSizeGroup="ResultNameGroup" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="450"
                                                      SharedSizeGroup="ResultValueGroup" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="Auto"
                                                      SharedSizeGroup="ResultTypeGroup" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Name"
                                           Margin="20 0 20 0"
                                           HorizontalAlignment="Center"
                                           Grid.Column="0" />
                                <GridSplitter Grid.Column="1"
                                              ResizeBehavior="PreviousAndNext"
                                              ResizeDirection="Columns"
                                              HorizontalAlignment="Stretch"
                                              VerticalAlignment="Stretch"
                                              Background="DarkGray" />
                                <TextBlock Text="Value"
                                           Margin="20 0 20 0"
                                           HorizontalAlignment="Center"
                                           Grid.Column="2" />
                                <GridSplitter Grid.Column="3"
                                              ResizeBehavior="PreviousAndNext"
                                              ResizeDirection="Columns"
                                              HorizontalAlignment="Stretch"
                                              VerticalAlignment="Stretch"
                                              Background="DarkGray" />
                                <TextBlock Text="Type"
                                           Margin="20 0 20 0"
                                           HorizontalAlignment="Center"
                                           Grid.Column="4" />
                                <GridSplitter Grid.Column="5"
                                              ResizeBehavior="PreviousAndCurrent"
                                              ResizeDirection="Columns"
                                              HorizontalAlignment="Stretch"
                                              VerticalAlignment="Stretch"
                                              Background="DarkGray" />
                            </Grid>
                        </ScrollViewer>
                        <controls:TreeListView x:Name="ResultTree"
                                               ItemsSource="{Binding Results}"
                                               KeyDown="OnTreeViewKeyDown"
                                               MouseDoubleClick="OnTreeViewDoubleClick"
                                               VirtualizingPanel.IsVirtualizing="True"
                                               VirtualizingPanel.VirtualizationMode="Recycling"
                                               BorderBrush="#cccccc"
                                               ScrollViewer.ScrollChanged="ScrollViewer_OnScrollChanged"
                                               ContextMenuOpening="ResultTree_OnContextMenuOpening">
                            <FrameworkElement.Style>
                                <Style TargetType="controls:TreeListView">
                                    <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
                                            Value="Hidden" />
                                    <Style.Triggers>
                                        <Trigger Property="IsMouseOver"
                                                 Value="True">
                                            <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
                                                    Value="Auto" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </FrameworkElement.Style>
                            <FrameworkElement.CommandBindings>
                                <CommandBinding Command="Copy"
                                                Executed="CopyCommand" />
                            </FrameworkElement.CommandBindings>
                            <FrameworkElement.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Name="CopyValue"
                                              Header="Copy Value"
                                              InputGestureText="Ctrl+C"
                                              Click="CopyClick">
                                        <MenuItem.Icon>
                                            <Image Source="{StaticResource Copy}"
                                                   Height="12" />
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <MenuItem Name="CopyValueWithChildren"
                                              Header="Copy Value with Children"
                                              Click="CopyClick" />
                                    <MenuItem Name="CopyAllValues"
                                              Header="Copy All Values"
                                              Click="CopyAllClick" />
                                    <MenuItem Name="CopyAllValuesWithChildren"
                                              Header="Copy All Values with Children"
                                              InputGestureText="Ctrl+Shift+C"
                                              Click="CopyAllClick" />
                                </ContextMenu>
                            </FrameworkElement.ContextMenu>
                            <ItemsControl.ItemContainerStyle>
                                <Style TargetType="controls:TreeListViewItem"
                                       BasedOn="{StaticResource {x:Type controls:TreeListViewItem}}">
                                    <Setter Property="IsExpanded"
                                            Value="{Binding IsExpanded, Mode=OneTime}" />
                                    <EventSetter Event="KeyDown"
                                                 Handler="OnTreeViewKeyDown" />
                                </Style>
                            </ItemsControl.ItemContainerStyle>
                        </controls:TreeListView>
                    </DockPanel>
                </TabItem>
                <TabItem Name="ILViewerTab"
                         Header="IL"
                         IsSelected="{Binding ShowIL}" />
            </TabControl>
        </Grid>
    </DockPanel>
</UserControl>
