<UserControl x:Class="RoslynPad.DocumentTreeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:controls="clr-namespace:RoslynPad.Controls"
             xmlns:ui="clr-namespace:RoslynPad.UI;assembly=RoslynPad.Common.UI"
             xmlns:roslynPad="clr-namespace:RoslynPad"
             xmlns:roslyn="clr-namespace:Microsoft.CodeAnalysis;assembly=Microsoft.CodeAnalysis"
             mc:Ignorable="d"
             d:DataContext="{d:DesignInstance ui:MainViewModel}"
             d:DesignHeight="300"
             d:DesignWidth="300">
    <FrameworkElement.Resources>

        <roslynPad:FilterCollectionViewConverter x:Key="FilterCollectionViewConverter"
                                                 FilterProperty="IsSearchMatch"
                                                 Filter="FilterCollectionViewSourceConverter_OnFilter" />

        <HierarchicalDataTemplate DataType="{x:Type ui:DocumentViewModel}"
                                  ItemsSource="{Binding Children, Converter={StaticResource FilterCollectionViewConverter}}">
            <DockPanel Margin="0 2 0 2"
                       VerticalAlignment="Center">
                <ToggleButton Style="{StaticResource TreeListViewToggleStyle}" />

                <Image Name="Icon"
                       Source="{StaticResource CsFile}"
                       Height="12"
                       Margin="0 0 3 0" />
                <TextBlock Text="{Binding Name}" />
            </DockPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsFolder, Mode=OneTime}"
                             Value="True">
                    <Setter TargetName="Icon"
                            Property="Source"
                            Value="{StaticResource Folder}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </HierarchicalDataTemplate>

        <ContextMenu x:Key="DocumentItemContextMenu"
                     x:Shared="False">
            <MenuItem Header="_Open in File Explorer"
                      Click="DocumentsContextMenu_OpenFolder_Click" />
        </ContextMenu>

        <Style x:Key="DocumentItemContainerStyle"
               TargetType="controls:TreeListViewItem"
               BasedOn="{StaticResource {x:Type controls:TreeListViewItem}}">
            <EventSetter Event="MouseDoubleClick"
                         Handler="OnDocumentClick" />
            <EventSetter Event="KeyDown"
                         Handler="OnDocumentKeyDown" />
            <Setter Property="ContextMenu"
                    Value="{StaticResource DocumentItemContextMenu}" />
        </Style>

        <Style x:Key="SearchButtonStyle"
               TargetType="ButtonBase">
            <Setter Property="BorderBrush"
                    Value="Transparent" />
            <Setter Property="Background"
                    Value="Transparent" />
            <Setter Property="Width"
                    Value="22" />
            <Setter Property="Padding"
                    Value="2" />
        </Style>

    </FrameworkElement.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Border BorderBrush="#ABADB3"
                BorderThickness="1,1,1,0"
                Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
            <DockPanel>
                <ToggleButton DockPanel.Dock="Right"
                              Style="{StaticResource SearchButtonStyle}"
                              IsChecked="{Binding SearchUsingRegex}"
                              ToolTip="Use Regular Expressions"
                              Content="{StaticResource RegularExpression}" />
                <ToggleButton DockPanel.Dock="Right"
                              Style="{StaticResource SearchButtonStyle}"
                              IsChecked="{Binding SearchFileContents}"
                              ToolTip="Search File Contents"
                              Content="{StaticResource FileCollection}" />
                <Button DockPanel.Dock="Right"
                        Style="{StaticResource SearchButtonStyle}"
                        Command="{Binding ClearSearchCommand}"
                        Visibility="{Binding CanClearSearch, Converter={StaticResource BooleanToVisibilityConverter}}"
                        ToolTip="Clear search (Esc)"
                        Content="{StaticResource Close}" />
                <Grid>
                    <TextBox Name="SearchText"
                             BorderThickness="0"
                             Validation.ErrorTemplate="{StaticResource ErrorTemplate}"
                             Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"
                             KeyDown="Search_OnKeyDown" />
                    <TextBlock Text="Search documents (press Enter)"
                               IsHitTestVisible="False"
                               Foreground="DarkGray">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Setter Property="Visibility"
                                        Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=SearchText, Path=Text}"
                                                 Value="">
                                        <Setter Property="Visibility"
                                                Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </Grid>
            </DockPanel>
        </Border>

        <controls:TreeListView Grid.Row="1"
                               ShowSeparator="False"
                               ItemContainerStyle="{StaticResource DocumentItemContainerStyle}"
                               ItemsSource="{Binding DocumentRoot.Children, Converter={StaticResource FilterCollectionViewConverter}}" />

        <ToolBar Grid.Row="2"
                 Background="Transparent"
                 BorderBrush="DarkGray"
                 BorderThickness="0 0 0 1"
                 DockPanel.Dock="Bottom"
                 Margin="0,5,0,0">
            <Button ToolTip="New Document (Ctrl+N)"
                    ToolTipService.Placement="Top"
                    Command="{Binding NewDocumentCommand, Mode=OneTime}"
                    CommandParameter="{x:Static roslyn:SourceCodeKind.Regular}">
                <Image Source="{StaticResource CsFile}"
                       Width="16"
                       Height="16" />
            </Button>
            <Button ToolTip="New Script Document (Ctrl+Shift+N)"
                    ToolTipService.Placement="Top"
                    Command="{Binding NewDocumentCommand, Mode=OneTime}"
                    CommandParameter="{x:Static roslyn:SourceCodeKind.Script}">
                <Image Source="{StaticResource CsFile}"
                       Width="16"
                       Height="16" />
            </Button>
            <Button ToolTip="Open File (Ctrl+O)"
                    ToolTipService.Placement="Top"
                    Command="{Binding OpenFileCommand, Mode=OneTime}">
                <Image Source="{StaticResource OpenFolder}"
                       Width="16"
                       Height="16" />
            </Button>
            <Button ToolTip="Change Documents Location"
                    ToolTipService.Placement="Top"
                    Command="{Binding EditUserDocumentPathCommand, Mode=OneTime}">
                <Image Source="{StaticResource MoveFolder}"
                       Width="16"
                       Height="16" />
            </Button>
        </ToolBar>

    </Grid>
</UserControl>
