<Window x:Class="InABox.DynamicGrid.ExpressionEditorWindow"
        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:local="clr-namespace:InABox.DynamicGrid"
        xmlns:system="clr-namespace:System;assembly=System.Runtime"
        mc:Ignorable="d"
        Title="Expression Editor" Height="650" Width="800"
        Loaded="ExpressionWindow_Loaded"
        x:Name="ExpressionWindow">
    <Window.Resources>
        <local:ExpressionEditorVariableTemplateSelector x:Key="variableTemplateSelector"/>
        <local:ExpressionEditorVariableTemplateItemStyleSelector x:Key="variableTemplateItemStyleSelector"/>

        <Style x:Key="variableTemplateItemStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <ContentPresenter/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="tooltipDescriptionStyle" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Description}" Value="">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="VariableListStyle" TargetType="ListBox">
            <Setter Property="ItemTemplate" Value="{StaticResource VariableExpanderTemplate}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasChildren}" Value="True"/>
            </Style.Triggers>
        </Style>
        <DataTemplate x:Key="VariableTemplate" DataType="local:ExpressionEditorVariable">
            <ContentControl MouseDoubleClick="Variable_Click"
                            Tag="{Binding VariableName}">
                <TextBlock Text="{Binding Display}"
                           FontFamily="Consolas" FontSize="12"
                           Padding="5"/>
            </ContentControl>
        </DataTemplate>
        <DataTemplate x:Key="VariableExpanderTemplate" DataType="{x:Type local:ExpressionEditorVariable}">
            <Border BorderThickness="1,0,0,1" BorderBrush="LightGray"
                    Padding="5">
                <Expander Header="{Binding Display}">
                    <ListBox ItemsSource="{Binding ChildVariables}"
                             ItemTemplateSelector="{StaticResource variableTemplateSelector}"
                             ItemContainerStyleSelector="{StaticResource variableTemplateItemStyleSelector}"
                             HorizontalContentAlignment="Stretch"
                             BorderThickness="0">
                        <ListBox.Template>
                            <ControlTemplate>
                                <ItemsPresenter/>
                            </ControlTemplate>
                        </ListBox.Template>
                    </ListBox>
                </Expander>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="250"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBox x:Name="Editor"
                 Grid.Column="0" Grid.Row="0" Grid.RowSpan="2"
                 Background="LightYellow"
                 BorderThickness="1,1,0,1"
                 Padding="5"
                 FontFamily="Consolas" FontSize="12"
                 TextWrapping="Wrap"
                 PreviewLostKeyboardFocus="Editor_PreviewLostKeyboardFocus"
                 PreviewTextInput="Editor_PreviewTextInput"
                 KeyDown="Editor_KeyDown"/>
        <Border Grid.Column="1" Grid.Row="0"
                BorderBrush="DarkGray" BorderThickness="1,0,0,0">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
                <StackPanel>
                    <Border BorderThickness="0,0,0,1" BorderBrush="LightGray"
                            Padding="5">
                        <Expander Header="Functions">
                            <ItemsControl ItemsSource="{x:Static local:ExpressionEditorWindow.FunctionTemplates}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border BorderThickness="1,0,0,1" BorderBrush="LightGray"
                                                Padding="5">
                                            <Expander Header="{Binding Item1}">
                                                <ListBox ItemsSource="{Binding Item2}"
                                                         HorizontalContentAlignment="Stretch"
                                                         BorderThickness="0">
                                                    <ListBox.Template>
                                                        <ControlTemplate>
                                                            <ItemsPresenter/>
                                                        </ControlTemplate>
                                                    </ListBox.Template>
                                                    <ListBox.ItemTemplate>
                                                        <DataTemplate DataType="local:FunctionTemplate">
                                                            <ContentControl MouseDoubleClick="FunctionTemplate_Click"
                                                                            Tag="{Binding}">
                                                                <TextBlock Text="{Binding Template}"
                                                                           FontFamily="Consolas" FontSize="12"
                                                                           Padding="5">
                                                                    <TextBlock.ToolTip>
                                                                        <ToolTip Background="LightGray">
                                                                            <StackPanel>
                                                                                <TextBlock Text="{Binding Tooltip}"
                                                                                           FontFamily="Consolas" FontSize="12"/>
                                                                                <TextBlock Text="{Binding Description}" FontSize="12"
                                                                                           Style="{StaticResource tooltipDescriptionStyle}"/>
                                                                            </StackPanel>
                                                                        </ToolTip>
                                                                    </TextBlock.ToolTip>
                                                                </TextBlock>
                                                            </ContentControl>
                                                        </DataTemplate>
                                                    </ListBox.ItemTemplate>
                                                </ListBox>
                                            </Expander>
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Expander>
                    </Border>
                    <Border BorderThickness="0,0,0,1" BorderBrush="LightGray"
                            Margin="0,5,0,0"
                            Padding="5">
                        <Expander Header="Variables" IsExpanded="True">
                            <ListBox ItemsSource="{Binding Path=Variables,ElementName=ExpressionWindow}"
                                     HorizontalContentAlignment="Stretch"
                                     BorderThickness="0"
                                     ItemTemplateSelector="{StaticResource variableTemplateSelector}"
                                     ItemContainerStyleSelector="{StaticResource variableTemplateItemStyleSelector}">
                                <ListBox.Template>
                                    <ControlTemplate>
                                        <ItemsPresenter/>
                                    </ControlTemplate>
                                </ListBox.Template>
                            </ListBox>
                        </Expander>
                    </Border>
                </StackPanel>
            </ScrollViewer>
        </Border>

        <Border Grid.Column="1" Grid.Row="1"
                BorderBrush="DarkGray" BorderThickness="1,0,0,0">
            <DockPanel LastChildFill="False">
                <Button Content="OK"
                        DockPanel.Dock="Right"
                        Click="OK_Click"
                        Padding="5" Width="50" Height="30"/>
            </DockPanel>
        </Border>
    </Grid>
</Window>
