<UserControl x:Class="Artemis.UI.Screens.ProfileEditor.ProfileTree.ProfileTreeView"
             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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:s="https://github.com/canton7/Stylet"
             xmlns:dd="urn:gong-wpf-dragdrop"
             xmlns:behaviors="clr-namespace:Artemis.UI.Behaviors"
             xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:profileTree1="clr-namespace:Artemis.UI.Screens.ProfileEditor.ProfileTree"
             xmlns:treeItem="clr-namespace:Artemis.UI.Screens.ProfileEditor.ProfileTree.TreeItem"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800"
             d:DataContext="{d:DesignInstance {x:Type profileTree1:ProfileTreeViewModel}}">
    <materialDesign:DialogHost IsTabStop="False" Focusable="False" Identifier="ProfileTreeDialog" DialogTheme="Inherit">
        <Grid>
            <Grid.Resources>
                <DataTemplate x:Key="ElementDragTemplate" DataType="{x:Type treeItem:TreeItemViewModel}">
                    <Border Background="{DynamicResource MaterialDesignTextFieldBoxHoverBackground}"
                            Padding="10"
                            CornerRadius="4">
                        <TextBlock Text="{Binding ProfileElement.Name}" VerticalAlignment="Center" Foreground="{DynamicResource MaterialDesignBody}" />
                    </Border>
                </DataTemplate>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <StackPanel>
                <TextBlock Style="{StaticResource MaterialDesignSubtitle1TextBlock}" Margin="10 5 0 -4">
                    Profile elements
                </TextBlock>
                <Separator Style="{StaticResource MaterialDesignDarkSeparator}" Margin="8 0" />
            </StackPanel>

            <TreeView Grid.Row="1"
                      ItemsSource="{Binding ActiveItem.Items}"
                      HorizontalContentAlignment="Stretch"
                      dd:DragDrop.IsDragSource="True"
                      dd:DragDrop.IsDropTarget="True"
                      dd:DragDrop.DropHandler="{Binding}"
                      dd:DragDrop.DragAdornerTemplate="{StaticResource ElementDragTemplate}"
                      ContextMenuOpening="{s:Action ContextMenuOpening}"
                      PreviewMouseDown="{s:Action TreeViewPreviewMouseDown}"
                      PreviewMouseUp="{s:Action TreeViewPreviewMouseUp}">
                <TreeView.InputBindings>
                    <KeyBinding Key="F2" Command="{s:Action RenameElement}" s:View.ActionTarget="{Binding SelectedTreeItem}" />
                    <KeyBinding Key="Delete" Command="{s:Action DeleteElement}" s:View.ActionTarget="{Binding SelectedTreeItem}" />
                    <KeyBinding Key="D" Modifiers="Control" Command="{s:Action DuplicateElement}" s:View.ActionTarget="{Binding SelectedTreeItem}" />
                    <KeyBinding Key="C" Modifiers="Control" Command="{s:Action CopyElement}" s:View.ActionTarget="{Binding SelectedTreeItem}" />
                    <KeyBinding Key="V" Modifiers="Control" Command="{s:Action PasteElement}" s:View.ActionTarget="{Binding SelectedTreeItem}" />
                </TreeView.InputBindings>
                <TreeView.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Add new folder" Command="{s:Action AddFolder}">
                            <MenuItem.Icon>
                                <materialDesign:PackIcon Kind="CreateNewFolder" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Add new layer" Command="{s:Action AddLayer}">
                            <MenuItem.Icon>
                                <materialDesign:PackIcon Kind="LayersPlus" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <Separator />
                        <MenuItem Header="Duplicate" Command="{s:Action DuplicateElement}" InputGestureText="Ctrl+D" IsEnabled="False">
                            <MenuItem.Icon>
                                <materialDesign:PackIcon Kind="ContentDuplicate" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Copy" Command="{s:Action CopyElement}" InputGestureText="Ctrl+C" IsEnabled="False">
                            <MenuItem.Icon>
                                <materialDesign:PackIcon Kind="ContentCopy" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Paste" Command="{s:Action PasteElement}" InputGestureText="Ctrl+V">
                            <MenuItem.Icon>
                                <materialDesign:PackIcon Kind="ContentPaste" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <Separator />
                        <MenuItem Header="Rename" Command="{s:Action RenameElement}" InputGestureText="F2" IsEnabled="False">
                            <MenuItem.Icon>
                                <materialDesign:PackIcon Kind="RenameBox" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Delete" Command="{s:Action DeleteElement}" InputGestureText="Del" IsEnabled="False">
                            <MenuItem.Icon>
                                <materialDesign:PackIcon Kind="TrashCan" />
                            </MenuItem.Icon>
                        </MenuItem>
                    </ContextMenu>
                </TreeView.ContextMenu>
                <b:Interaction.Behaviors>
                    <behaviors:TreeViewSelectionBehavior SelectedItem="{Binding SelectedTreeItem}" />
                </b:Interaction.Behaviors>
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource MaterialDesignTreeViewItem}">
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay, FallbackValue=False}"/>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type treeItem:FolderViewModel}" ItemsSource="{Binding Items}">
                        <ContentControl s:View.Model="{Binding}" />
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type treeItem:LayerViewModel}">
                        <ContentControl s:View.Model="{Binding}" />
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>

            <StackPanel HorizontalAlignment="Right" Grid.Row="2" Orientation="Horizontal" Margin="8">
                <Button Style="{StaticResource MaterialDesignIconForegroundButton}"
                        Padding="2 0 2 0"
                        Width="30"
                        Height="30"
                        materialDesign:RippleAssist.IsCentered="True"
                        ToolTip="Add new folder to root"
                        Command="{s:Action AddFolder}">
                    <materialDesign:PackIcon Kind="CreateNewFolder" Width="15" />
                </Button>
                <Button Style="{StaticResource MaterialDesignIconForegroundButton}"
                        Width="30"
                        Height="30"
                        Padding="2 0 2 0"
                        materialDesign:RippleAssist.IsCentered="True"
                        ToolTip="Add new layer to root"
                        Command="{s:Action AddLayer}">
                    <materialDesign:PackIcon Kind="LayersPlus" Width="15" />
                </Button>
            </StackPanel>
        </Grid>
    </materialDesign:DialogHost>
</UserControl>