<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="RoslynPad.DocumentView"
             xmlns:local="clr-namespace:RoslynPad;assembly=RoslynPad"
             xmlns:Editor="clr-namespace:RoslynPad.Editor;assembly=RoslynPad.Editor.Avalonia"
             xmlns:ui="clr-namespace:RoslynPad.UI;assembly=RoslynPad.Common.UI"
             xmlns:converters="clr-namespace:RoslynPad.Converters">
    <UserControl.Resources>
        <converters:DoubleToPercentageTextConverter x:Key="DoubleToPercentageTextConverter" />
    </UserControl.Resources>
    <UserControl.KeyBindings>
        <KeyBinding Gesture="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}" />
    </UserControl.KeyBindings>
    <Grid RowDefinitions="Auto,*,5,*">
        <StackPanel Grid.Row="0"
                    Name="Toolbar"
                    Orientation="Horizontal">
            <StackPanel.Styles>
                <Style Selector="#Toolbar > :is(Control)">
                    <Setter Property="Margin"
                            Value="2" />
                </Style>
                <Style Selector="Button">
                    <Setter Property="Background"
                            Value="Transparent" />
                    <Setter Property="BorderThickness"
                            Value="0" />
                    <Setter Property="Padding"
                            Value="0" />
                    <Setter Property="Margin"
                            Value="0 0 3 0" />
                    <Setter Property="Width"
                            Value="18" />
                    <Setter Property="Height"
                            Value="18" />
                </Style>
                <Style Selector="Button:pointerover /template/ ContentPresenter">
                    <Setter Property="Background"
                            Value="#eeeeee" />
                </Style>
            </StackPanel.Styles>
            <Button Command="{Binding RunCommand, Mode=OneTime}"
                    ToolTip.Tip="Run (F5)">
                <Grid>
                    <Image Source="{StaticResource Run}"
                           Width="16"
                           Height="16"/>
                    <ProgressBar VerticalAlignment="Bottom"
                                 Height="4"
                                 IsIndeterminate="{Binding IsRunning}"
                                 BorderThickness="0"
                                 IsVisible="{Binding IsRunning}" />
                </Grid>
            </Button>
            <Grid Margin="5, 0"
                  IsVisible="{Binding HasReportedProgress}" >
                <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.Tip="Terminate process (Shift+F5)"
                    IsEnabled="{Binding IsRunning}" >
                <Image Source="{StaticResource Stop}"
                       Width="16"
                       Height="16" />
            </Button>
            <DropDown Items="{Binding AvailablePlatforms, Mode=OneTime}"
                      SelectedItem="{Binding Platform}" />
            <Button ToolTip.Tip="Set platform as default"
                    Command="{Binding SetDefaultPlatformCommand, Mode=OneTime}">
                <Image Source="{StaticResource SetPlatform}"
                       Width="16"
                       Height="16"/>
            </Button>
            <Separator />
            <Button Command="{Binding SaveCommand, Mode=OneTime}"
                    ToolTip.Tip="Save (Ctrl+S)" >
                <Image Source="{StaticResource Save}"
                       Width="16"
                       Height="16"/>
            </Button>
            <Button Command="{Binding OpenBuildPathCommand, Mode=OneTime}"
                    ToolTip.Tip="Open Build Folder">
                <Image Source="{StaticResource PackageFolder}"
                       Width="16"
                       Height="16"/>
            </Button>
            <Separator />
            <Menu Background="Transparent"
                  Name="RootMenu"
                  IsEnabled="{Binding NuGet.IsEnabled}">
                <MenuItem Name="RootNuGetMenu"
                          IsSubMenuOpen="{Binding NuGet.IsPackagesMenuOpen}"
                          Items="{Binding NuGet.Packages}"
                          Header="{Binding NuGet, Mode=OneTime}"
                          ToolTip.Tip="NuGet Packages">
                    <MenuItem.Styles>
                        <Style Selector="MenuItem Popup">
                            <Setter Property="MaxHeight" Value="400" />
                        </Style>
                        <Style Selector="MenuItem > MenuItem">
                            <Setter Property="Command"
                                    Value="{Binding ElementName=RootMenu, Path=DataContext.NuGet.InstallPackageCommand, Mode=OneTime}" />
                            <Setter Property="CommandParameter"
                                    Value="{Binding}" />
                        </Style>
                    </MenuItem.Styles>
                    <MenuItem.DataTemplates>
                        <DataTemplate DataType="{x:Type ui:PackageData}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Id, Mode=OneTime}" />
                                <TextBlock Text="{Binding Version, Mode=OneTime}" />
                            </StackPanel>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type ui:NuGetDocumentViewModel}">
                            <Grid>
                                <Image Source="{StaticResource NuGet}"
                                       Width="16"
                                       Height="16" />
                                <ProgressBar VerticalAlignment="Bottom"
                                             Height="4"
                                             MinWidth="0"
                                             BorderThickness="0"
                                             IsIndeterminate="{Binding IsSearching}"
                                             IsVisible="{Binding IsSearching}" />
                            </Grid>
                        </DataTemplate>
                    </MenuItem.DataTemplates>
                </MenuItem>
            </Menu>
            <TextBox Name="NuGetSearch"
                     IsEnabled="{Binding NuGet.IsEnabled}"
                     BorderBrush="#eeeeee"
                     Text="{Binding NuGet.SearchTerm}"
                     Watermark="Search NuGet (Ctrl+T)"
                     Width="200" />
            <Decorator VerticalAlignment="Center">
                <CheckBox IsChecked="{Binding NuGet.Prerelease}"
                          ToolTip.Tip="Include prerelease packages"
                          Content="Prerelease" />
            </Decorator>
            <ContentPresenter Content="{Binding NuGet}">
                <ContentPresenter.ContentTemplate>
                    <DataTemplate DataType="{x:Type ui:NuGetDocumentViewModel}">
                        <Grid>
                            <Image Source="{StaticResource NuGet}"
                                   Width="16"
                                   Height="16"/>
                            <ProgressBar VerticalAlignment="Bottom"
                                             Height="4"
                                             MinWidth="0"
                                             BorderThickness="0"
                                             IsIndeterminate="{Binding IsRestoring}"
                                             IsVisible="{Binding IsRestoring}" />
                        </Grid>
                    </DataTemplate>
                </ContentPresenter.ContentTemplate>
            </ContentPresenter>
            <Separator />
            <Button ToolTip.Tip="Format Document (Ctrl+D)"
                    Command="{Binding FormatDocumentCommand, Mode=OneTime}">
                <Image Source="{StaticResource FormatDocument}"
                       Width="16"
                       Height="16" />
            </Button>
            <Button ToolTip.Tip="Comment Selection (Ctrl+K)"
                    Command="{Binding CommentSelectionCommand, Mode=OneTime}">
                <Image Source="{StaticResource CommentCode}"
                       Width="16"
                       Height="16" />
            </Button>
            <Button ToolTip.Tip="Uncomment Selection (Ctrl+U)"
                    Command="{Binding UncommentSelectionCommand, Mode=OneTime}" >
                <Image Source="{StaticResource UncommentCode}"
                       Width="16"
                       Height="16" />
            </Button>
            <Button ToolTip.Tip="Rename Symbol (F2)"
                    Command="{Binding RenameSymbolCommand, Mode=OneTime}" >
                <Image Source="{StaticResource Rename}"
                       Width="16"
                       Height="16"/>
            </Button>
        </StackPanel>

        <Editor:RoslynCodeEditor Name="Editor"
                                 ContextActionsIcon="{DynamicResource Bulb}"
                                 Grid.Row="1" />

        <TreeView Grid.Row="3"
                  Items="{Binding Results}">
            <TreeView.ItemTemplate>
                <TreeDataTemplate ItemsSource="{Binding Children}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Header}"
                                   Width="150" />
                        <TextBlock Text="{Binding Value}" />
                        <TextBlock Text="{Binding Message}" />
                    </StackPanel>
                </TreeDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</UserControl>
