<?xml version="1.0" encoding="utf-8" ?>
<UserControl
    x:Class="Ghost.Editor.Views.Controls.Hierarchy"
    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:local="using:Ghost.Editor.Views.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sg="using:Ghost.Editor.Core.SceneGraph"
    mc:Ignorable="d">

    <UserControl.Resources>
        <local:SceneGraphTemplateSelector
            x:Key="SceneGraphTemplateSelector"
            EntityNodeTemplate="{StaticResource EntityNodeTemplate}"
            SceneNodeTemplate="{StaticResource SceneNodeTemplate}" />

        <DataTemplate x:Key="SceneNodeTemplate" x:DataType="sg:SceneNode">
            <TreeViewItem
                AutomationProperties.Name="{x:Bind Name, Mode=OneWay}"
                IsExpanded="True"
                ItemsSource="{x:Bind Children, Mode=OneWay}">
                <TreeViewItem.ContextFlyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Click="OnCreateEntityClick" Text="Create Entity" />
                        <MenuFlyoutItem Click="OnSaveSceneClick" Text="Save Scene" />
                    </MenuFlyout>
                </TreeViewItem.ContextFlyout>
                <StackPanel Orientation="Horizontal">
                    <FontIcon FontSize="14" Glyph="&#xF156;" />
                    <TextBlock Margin="10,0,0,0" Text="{x:Bind Name, Mode=OneWay}" />
                </StackPanel>
            </TreeViewItem>
        </DataTemplate>

        <DataTemplate x:Key="EntityNodeTemplate" x:DataType="sg:EntityNode">
            <TreeViewItem AutomationProperties.Name="{x:Bind Name, Mode=OneWay}" ItemsSource="{x:Bind Children, Mode=OneWay}">
                <TreeViewItem.ContextFlyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Click="OnCreateChildClick" Text="Create Child" />
                        <MenuFlyoutItem Click="OnDeleteEntityClick" Text="Delete" />
                    </MenuFlyout>
                </TreeViewItem.ContextFlyout>
                <StackPanel Orientation="Horizontal">
                    <FontIcon FontSize="14" Glyph="&#xF158;" />
                    <TextBlock Margin="5,0,0,0" Text="{x:Bind Name, Mode=OneWay}" />
                </StackPanel>
            </TreeViewItem>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel
            Grid.Row="0"
            Padding="8,2,4,4"
            Background="{ThemeResource CardBackgroundFillColorDefaultBrush}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <TextBlock
                    Grid.Column="0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}"
                    Style="{StaticResource BodyLargeStrongTextBlockStyle}"
                    Text="Hierarchy" />
                <Button
                    Grid.Column="1"
                    HorizontalAlignment="Right"
                    Style="{ThemeResource ToolbarButton}">
                    <FontIcon Glyph="&#xE712;" />
                </Button>
            </Grid>

            <Grid Margin="0,2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <FontIcon
                    Grid.Column="0"
                    Margin="0,0,4,0"
                    FontSize="{StaticResource ToolbarFontIconFontSize}"
                    Glyph="&#xE721;" />
                <TextBox Grid.Column="1" PlaceholderText="Search item..." />
            </Grid>

            <Border Margin="-8,8,-4,-4" Style="{StaticResource HorizontalStrongDivider}" />
        </StackPanel>

        <TreeView
            x:Name="SceneTreeView"
            Grid.Row="1"
            Margin="4,2,0,2"
            AllowDrop="True"
            CanDrag="True"
            CanDragItems="True"
            CanReorderItems="True"
            DragItemsCompleted="OnTreeViewDragItemsCompleted"
            DragItemsStarting="OnTreeViewDragItemsStarting"
            ItemTemplateSelector="{StaticResource SceneGraphTemplateSelector}"
            KeyDown="OnTreeViewKeyDown"
            SelectionMode="Single" />
    </Grid>
</UserControl>
