<Window x:Class="CefFlashBrowser.Views.SolEditorWindow"
        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:CefFlashBrowser.Views"
        xmlns:m="clr-namespace:CefFlashBrowser.Models"
        xmlns:vm="clr-namespace:CefFlashBrowser.ViewModels"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:behaviors="clr-namespace:CefFlashBrowser.Views.Behaviors"
        xmlns:templateSelectors="clr-namespace:CefFlashBrowser.Views.TemplateSelectors"
        xmlns:svgc="http://sharpvectors.codeplex.com/svgc/"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        mc:Ignorable="d"
        Width="450"
        Height="500"
        WindowStartupLocation="CenterScreen"
        Title="{DynamicResource solEditor_title}">

    <d:Window.DataContext>
        <vm:SolEditorWindowViewModel/>
    </d:Window.DataContext>

    <Window.InputBindings>
        <KeyBinding Gesture="Ctrl+W"
                    Command="{Binding ElementName=windowBehavior, Path=CloseWindowCommand}"/>
        <KeyBinding Gesture="Ctrl+S"
                    Command="{Binding SaveFileCommand}"/>
        <KeyBinding Gesture="Ctrl+Shift+S"
                    Command="{Binding SaveAsFileCommand}"/>
    </Window.InputBindings>

    <i:Interaction.Behaviors>
        <behaviors:WindowBehavior x:Name="windowBehavior"/>
    </i:Interaction.Behaviors>

    <Window.Resources>
        <!--The default template for displaying nothing-->
        <DataTemplate x:Key="DisplayNothingTemplate">
        </DataTemplate>

        <!--Bool Editor-->
        <DataTemplate x:Key="BoolNodeTemplate"
                      DataType="{x:Type vm:SolNodeViewModel}">
            <CheckBox HorizontalAlignment="Right"
                      Style="{StaticResource ToggleButtonSwitch}"
                      IsChecked="{Binding Value}">
                <CheckBox.Effect>
                    <DropShadowEffect Color="White"
                                      Opacity="0.5"
                                      Direction="0"
                                      ShadowDepth="0"/>
                </CheckBox.Effect>
            </CheckBox>
        </DataTemplate>

        <!--Int Editor-->
        <DataTemplate x:Key="IntNodeTemplate"
                      DataType="{x:Type vm:SolNodeViewModel}">
            <hc:NumericUpDown Minimum="-268435456"
                              Maximum="268435455"
                              DecimalPlaces="0"
                              Value="{Binding Value, Converter={StaticResource IntToDouble}}"/>
        </DataTemplate>

        <!--Double Editor-->
        <DataTemplate x:Key="DoubleNodeTemplate"
                      DataType="{x:Type vm:SolNodeViewModel}">
            <hc:NumericUpDown Value="{Binding Value}"/>
        </DataTemplate>

        <!--DateTime Editor-->
        <DataTemplate x:Key="DateTimeNodeTemplate"
                      DataType="{x:Type vm:SolNodeViewModel}">
            <hc:DateTimePicker SelectedDateTime="{Binding Value}">
                <hc:DateTimePicker.ToolTip>
                    <TextBlock Text="{Binding Value, StringFormat='{}{0:yyyy-MM-dd HH:mm:ss}'}"/>
                </hc:DateTimePicker.ToolTip>
                <hc:DateTimePicker.Style>
                    <Style TargetType="{x:Type hc:DateTimePicker}"
                           BasedOn="{StaticResource {x:Type hc:DateTimePicker}}">
                        <Style.Triggers>
                            <Trigger Property="IsFocused" Value="False">
                                <Setter Property="DateTimeFormat" Value="yyyy-MM-dd"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </hc:DateTimePicker.Style>
            </hc:DateTimePicker>
        </DataTemplate>

        <!--Add Item Button-->
        <DataTemplate x:Key="AddItemTemplate"
                      DataType="{x:Type vm:SolNodeViewModel}">
            <Grid HorizontalAlignment="Right">
                <Grid.Resources>
                    <Style TargetType="Button"
                           BasedOn="{StaticResource {x:Type Button}}">
                        <Setter Property="Width" Value="28"/>
                        <Setter Property="Height" Value="28"/>
                        <Setter Property="Padding" Value="0"/>
                    </Style>
                </Grid.Resources>
                <Grid.Style>
                    <Style TargetType="Grid">
                        <Setter Property="Visibility" Value="Hidden"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DockPanel}, Path=IsMouseOver}">
                                <DataTrigger.Value>
                                    <sys:Boolean>True</sys:Boolean>
                                </DataTrigger.Value>
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>
                <Button Command="{Binding DataContext.AddChildCommand, RelativeSource={RelativeSource AncestorType=Window}}"
                        CommandParameter="{Binding}"
                        ToolTip="{DynamicResource solEditor_addItem}">
                    <svgc:SvgIcon Width="12"
                                  Height="12"
                                  UriSource="/Assets/Svgs/add.svg"/>
                </Button>
            </Grid>
        </DataTemplate>

        <!--Open editor button-->
        <DataTemplate x:Key="OpenTextEditorTemplate"
                      DataType="{x:Type vm:SolNodeViewModel}">
            <Button Command="{Binding DataContext.EditTextCommand, RelativeSource={RelativeSource AncestorType=Window}}"
                    CommandParameter="{Binding}">
                <Button.Style>
                    <Style TargetType="Button"
                           BasedOn="{StaticResource {x:Type Button}}">
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="ToolTip" Value="{Binding Value, Mode=OneWay}"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Value, Converter={StaticResource ObjectToString}}" Value="">
                                <Setter Property="ToolTip" Value="{x:Null}"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
                <TextBlock Width="100"
                           TextWrapping="NoWrap"
                           TextTrimming="CharacterEllipsis">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Setter Property="Text" Value="{Binding Value, Mode=OneWay}"/>
                            <Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Value, Converter={StaticResource ObjectToString}}" Value="">
                                    <Setter Property="Text" Value="{DynamicResource solEditor_emptyString}"/>
                                    <Setter Property="Foreground" Value="{DynamicResource SecondaryTextBrush}"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </Button>
        </DataTemplate>

        <!--Import/Export button-->
        <DataTemplate x:Key="ImportExportTemplate"
                      DataType="{x:Type vm:SolNodeViewModel}">
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Right">
                <StackPanel.Resources>
                    <Style TargetType="Button"
                           BasedOn="{StaticResource {x:Type Button}}">
                        <Setter Property="Width" Value="28"/>
                        <Setter Property="Height" Value="28"/>
                        <Setter Property="Padding" Value="0"/>
                    </Style>
                </StackPanel.Resources>
                <TextBlock>
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Setter Property="Margin" Value="0,0,10,0"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                            <Setter Property="Background" Value="Transparent"/>
                            <Setter Property="Foreground" Value="{DynamicResource SecondaryTextBrush}"/>
                            <Setter Property="Text" Value="{Binding Value, Converter={StaticResource BinaryToReadableSizeString}}"/>
                            <Setter Property="ToolTip" Value="{Binding Value, Converter={StaticResource BinaryToSizeString}}"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsSelected}"  Value="True">
                                    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Foreground}"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
                <Button ToolTip="{DynamicResource common_export}"
                        Command="{Binding DataContext.ExportBinaryCommand, RelativeSource={RelativeSource AncestorType=Window}}"
                        CommandParameter="{Binding}">
                    <svgc:SvgIcon Width="12.5"
                                  Height="12.5"
                                  UriSource="/Assets/Svgs/export.svg"/>
                </Button>
                <Button ToolTip="{DynamicResource common_import}"
                        Command="{Binding DataContext.ImportBinaryCommand, RelativeSource={RelativeSource AncestorType=Window}}"
                        CommandParameter="{Binding}">
                    <svgc:SvgIcon Width="13"
                                  Height="13"
                                  UriSource="/Assets/Svgs/import.svg"/>
                </Button>
            </StackPanel>
        </DataTemplate>

        <!--Selector for the node editor templates-->
        <templateSelectors:SolNodeTemplateSelector
            x:Key="NodeEditorSelector"
            RootTemplate="{StaticResource AddItemTemplate}"
            ArrayTemplate="{StaticResource AddItemTemplate}"
            ObjectTemplate="{StaticResource AddItemTemplate}"
            BoolTemplate="{StaticResource BoolNodeTemplate}"
            IntTemplate="{StaticResource IntNodeTemplate}"
            DoubleTemplate="{StaticResource DoubleNodeTemplate}"
            DateTemplate="{StaticResource DateTimeNodeTemplate}"
            StringTemplate="{StaticResource OpenTextEditorTemplate}"
            XmlDocTemplate="{StaticResource OpenTextEditorTemplate}"
            XmlTemplate="{StaticResource OpenTextEditorTemplate}"
            BinaryTemplate="{StaticResource ImportExportTemplate}"
            DefaultTemplate="{StaticResource DisplayNothingTemplate}"/>
    </Window.Resources>

    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="{DynamicResource solEditor_file}">
                <MenuItem Header="{DynamicResource solEditor_save}"
                          Command="{Binding SaveFileCommand}"
                          InputGestureText="Ctrl+S"/>
                <MenuItem Header="{DynamicResource solEditor_saveAs}"
                          Command="{Binding SaveAsFileCommand}"
                          InputGestureText="Ctrl+Shift+S"/>
                <Separator/>
                <MenuItem Header="{DynamicResource solEditor_close}"
                          Command="{Binding ElementName=windowBehavior, Path=CloseWindowCommand}"
                          InputGestureText="Ctrl+W"/>
            </MenuItem>
            <MenuItem Header="{DynamicResource solEditor_edit}">
                <MenuItem.Style>
                    <Style TargetType="MenuItem"
                           BasedOn="{StaticResource {x:Type MenuItem}}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedNode}"
                                         Value="{x:Null}">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.Style>
                <MenuItem Header="{DynamicResource solEditor_addItem}"
                          Command="{Binding AddChildCommand}"
                          CommandParameter="{Binding SelectedNode}"
                          IsEnabled="{Binding SelectedNode.CanAddChild}"/>
                <MenuItem Header="{DynamicResource solEditor_removeItem}"
                          Command="{Binding RemoveItemCommand}"
                          CommandParameter="{Binding SelectedNode}"
                          IsEnabled="{Binding SelectedNode.CanRemove}"/>
                <Separator/>
                <MenuItem Header="{DynamicResource common_rename}"
                          Command="{Binding RenameItemCommand}"
                          CommandParameter="{Binding SelectedNode}"
                          Visibility="{Binding SelectedNode.CanRename, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                <MenuItem Header="{DynamicResource common_moveUp}"
                          Command="{Binding MoveUpCommand}"
                          CommandParameter="{Binding SelectedNode}"
                          Visibility="{Binding SelectedNode.CanMove, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                <MenuItem Header="{DynamicResource common_moveDown}"
                          Command="{Binding MoveDownCommand}"
                          CommandParameter="{Binding SelectedNode}"
                          Visibility="{Binding SelectedNode.CanMove, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                <MenuItem Header="{DynamicResource common_moveToTop}"
                          Command="{Binding MoveToTopCommand}"
                          CommandParameter="{Binding SelectedNode}"
                          Visibility="{Binding SelectedNode.CanMove, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                <MenuItem Header="{DynamicResource common_moveToBottom}"
                          Command="{Binding MoveToBottomCommand}"
                          CommandParameter="{Binding SelectedNode}"
                          Visibility="{Binding SelectedNode.CanMove, Converter={StaticResource Boolean2VisibilityConverter}}"/>
            </MenuItem>
        </Menu>

        <StatusBar DockPanel.Dock="Bottom">
            <TextBlock>
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Text" Value="{DynamicResource solEditor_statusUnknown}"/>
                        <Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Status}" Value="{x:Static m:SolEditorStatus.Ready}">
                                <Setter Property="Text" Value="{DynamicResource solEditor_statusReady}"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Status}" Value="{x:Static m:SolEditorStatus.Error}">
                                <Setter Property="Text" Value="{DynamicResource solEditor_statusError}"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Status}" Value="{x:Static m:SolEditorStatus.Modified}">
                                <Setter Property="Text" Value="{DynamicResource solEditor_statusModified}"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Status}" Value="{x:Static m:SolEditorStatus.Saved}">
                                <Setter Property="Text" Value="{DynamicResource solEditor_statusSaved}"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
            <Separator/>
            <TextBlock Foreground="{DynamicResource PrimaryTextBrush}"
                       Text="{Binding FileFormat, Mode=OneWay}"/>
            <Separator/>
            <TextBlock TextTrimming="CharacterEllipsis"
                       Foreground="{DynamicResource SecondaryTextBrush}"
                       Text="{Binding FilePath, Mode=OneWay}"
                       ToolTip="{Binding FilePath, Mode=OneWay}"/>
        </StatusBar>

        <TreeView BorderThickness="0"
                  ItemsSource="{Binding RootNodes}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectedItemChanged">
                    <i:InvokeCommandAction Command="{Binding SelectNodeCommand}"
                                           CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TreeView}, Path=SelectedItem}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <DockPanel MinHeight="35"
                               Background="Transparent">
                        <Grid DockPanel.Dock="Right"
                              Margin="10,5"
                              MinWidth="120">
                            <ContentPresenter ContentTemplateSelector="{StaticResource NodeEditorSelector}"/>
                        </Grid>

                        <StackPanel Orientation="Horizontal"
                                    VerticalAlignment="Center">
                            <TextBlock Text="{Binding DisplayName}"/>
                            <TextBlock>
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock"
                                           BasedOn="{StaticResource {x:Type TextBlock}}">
                                        <Setter Property="Text" Value="{Binding TypeString, Mode=OneWay, StringFormat=': {0}'}"/>
                                        <Setter Property="Foreground" Value="{DynamicResource SecondaryTextBrush}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding TypeString}" Value="">
                                                <Setter Property="Visibility" Value="Collapsed"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsSelected}" Value="True">
                                                <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Foreground}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </StackPanel>
                    </DockPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>

            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}"
                       BasedOn="{StaticResource {x:Type TreeViewItem}}">
                    <Setter Property="IsExpanded" Value="True"/>
                    <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                    <Setter Property="Tag"
                            Value="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Window}}"/>
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <ContextMenu.Resources>
                                    <Style TargetType="MenuItem"
                                           BasedOn="{StaticResource {x:Type MenuItem}}">
                                        <Setter Property="Tag"
                                                Value="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                                    </Style>
                                </ContextMenu.Resources>
                                <MenuItem Header="{DynamicResource solEditor_addItem}"
                                          IsEnabled="{Binding CanAddChild}"
                                          Command="{Binding Tag.AddChildCommand, RelativeSource={RelativeSource Mode=Self}}"
                                          CommandParameter="{Binding}"/>
                                <MenuItem Header="{DynamicResource solEditor_removeItem}"
                                          IsEnabled="{Binding CanRemove}"
                                          Command="{Binding Tag.RemoveItemCommand, RelativeSource={RelativeSource Mode=Self}}"
                                          CommandParameter="{Binding}"/>
                                <Separator/>
                                <MenuItem Header="{DynamicResource common_rename}"
                                          Command="{Binding Tag.RenameItemCommand, RelativeSource={RelativeSource Mode=Self}}"
                                          CommandParameter="{Binding}"
                                          Visibility="{Binding CanRename, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                                <MenuItem Header="{DynamicResource common_moveUp}"
                                          Command="{Binding Tag.MoveUpCommand, RelativeSource={RelativeSource Mode=Self}}"
                                          CommandParameter="{Binding}"
                                          Visibility="{Binding CanMove, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                                <MenuItem Header="{DynamicResource common_moveDown}"
                                          Command="{Binding Tag.MoveDownCommand, RelativeSource={RelativeSource Mode=Self}}"
                                          CommandParameter="{Binding}"
                                          Visibility="{Binding CanMove, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                                <MenuItem Header="{DynamicResource common_moveToTop}"
                                          Command="{Binding Tag.MoveToTopCommand, RelativeSource={RelativeSource Mode=Self}}"
                                          CommandParameter="{Binding}"
                                          Visibility="{Binding CanMove, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                                <MenuItem Header="{DynamicResource common_moveToBottom}"
                                          Command="{Binding Tag.MoveToBottomCommand, RelativeSource={RelativeSource Mode=Self}}"
                                          CommandParameter="{Binding}"
                                          Visibility="{Binding CanMove, Converter={StaticResource Boolean2VisibilityConverter}}"/>
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                    <EventSetter Event="PreviewMouseRightButtonUp"
                                 Handler="TreeViewItemPreviewMouseRightButtonUp"/>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
    </DockPanel>
</Window>
