<UserControl x:Class="Artemis.UI.Screens.ProfileEditor.LayerProperties.Tree.TreeGroupView"
             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:local="clr-namespace:Artemis.UI.Screens.ProfileEditor.LayerProperties.Tree"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:s="https://github.com/canton7/Stylet"
             xmlns:converters="clr-namespace:Artemis.UI.Converters"
             xmlns:dd="urn:gong-wpf-dragdrop"
             xmlns:layerProperties="clr-namespace:Artemis.UI.Screens.ProfileEditor.LayerProperties"
             xmlns:shared="clr-namespace:Artemis.UI.Shared;assembly=Artemis.UI.Shared"
             mc:Ignorable="d"
             d:DataContext="{d:DesignInstance local:TreeGroupViewModel}"
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <shared:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
        <converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
        <converters:PropertyTreeMarginConverter Length="20" x:Key="PropertyTreeMarginConverter" />
    </UserControl.Resources>

    <StackPanel>
        <Border Name="Bd"
                BorderBrush="{DynamicResource MaterialDesignDivider}"
                BorderThickness="0,0,0,1"
                Height="25">
            <Grid Margin="{Binding Converter={StaticResource PropertyTreeMarginConverter}}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="19" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <ToggleButton x:Name="Expander"
                              Foreground="{DynamicResource MaterialDesignBody}"
                              Style="{StaticResource MaterialDesignExpandCollapseToggleStyle}"
                              IsChecked="{Binding Path=LayerPropertyGroupViewModel.IsExpanded}"
                              Visibility="{Binding Path=LayerPropertyGroupViewModel.HasChildren, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"
                              ClickMode="Press" />

                <StackPanel Grid.Column="1">
                    <!-- Type: None -->
                    <TextBlock
                        dd:DragDrop.DragSourceIgnore="True"
                        Text="{Binding LayerPropertyGroup.GroupDescription.Name}"
                        ToolTip="{Binding LayerPropertyGroup.GroupDescription.Description}"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left"
                        Margin="3 5 0 5">
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding GroupType}" Value="None">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>

                    <!-- Type: General -->
                    <StackPanel Orientation="Horizontal" Margin="0 5" dd:DragDrop.DragSourceIgnore="True">
                        <StackPanel.Style>
                            <Style TargetType="{x:Type StackPanel}">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding GroupType}" Value="General">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </StackPanel.Style>
                        <materialDesign:PackIcon Kind="HammerWrench" Margin="0 -1 5 0" />
                        <TextBlock ToolTip="{Binding LayerPropertyGroup.GroupDescription.Description}">General</TextBlock>
                    </StackPanel>

                    <!-- Type: Transform -->
                    <StackPanel Orientation="Horizontal" Margin="0 5" dd:DragDrop.DragSourceIgnore="True">
                        <StackPanel.Style>
                            <Style TargetType="{x:Type StackPanel}">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding GroupType}" Value="Transform">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </StackPanel.Style>
                        <materialDesign:PackIcon Kind="TransitConnectionVariant" Margin="0 -1 5 0" />
                        <TextBlock ToolTip="{Binding LayerPropertyGroup.GroupDescription.Description}">Transform</TextBlock>
                    </StackPanel>

                    <!-- Type: LayerBrushRoot -->
                    <Grid dd:DragDrop.DragSourceIgnore="True">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.Style>
                            <Style TargetType="{x:Type Grid}">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding GroupType}" Value="LayerBrushRoot">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Style>
                        <shared:ArtemisIcon Grid.Column="0"
                                            Icon="{Binding LayerPropertyGroup.LayerBrush.Descriptor.Icon}"
                                            Width="16"
                                            Height="16"
                                            Margin="0 5 5 0" />
                        <TextBlock Grid.Column="1"
                                   ToolTip="{Binding LayerPropertyGroup.LayerBrush.Descriptor.Description}"
                                   Margin="0 5 0 0">
                            Brush -&#160;
                        </TextBlock>
                        <TextBlock Grid.Column="2"
                                   Text="{Binding LayerPropertyGroup.LayerBrush.Descriptor.DisplayName}"
                                   ToolTip="{Binding LayerPropertyGroup.LayerBrush.Descriptor.Description}"
                                   Margin="0 5 0 0" />

                        <StackPanel Grid.Column="3"
                                    Orientation="Horizontal" 
                                    HorizontalAlignment="Right" 
                                    Visibility="{Binding LayerPropertyGroup.LayerBrush.ConfigurationDialog, Converter={StaticResource NullToVisibilityConverter}}">
                            <TextBlock VerticalAlignment="Center">Extra options available!</TextBlock>
                            <materialDesign:PackIcon Kind="ChevronRight" VerticalAlignment="Center">
                                <materialDesign:PackIcon.RenderTransform>
                                    <TranslateTransform X="0" />
                                </materialDesign:PackIcon.RenderTransform>
                                <materialDesign:PackIcon.Triggers>
                                    <EventTrigger RoutedEvent="materialDesign:PackIcon.Loaded">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetProperty="(materialDesign:PackIcon.RenderTransform).(TranslateTransform.X)"
                                                                 From="0"
                                                                 To="2"
                                                                 Duration="0:0:0.5"
                                                                 AutoReverse="True"
                                                                 RepeatBehavior="Forever">
                                                    <DoubleAnimation.EasingFunction>
                                                        <CubicEase EasingMode="EaseInOut" />
                                                    </DoubleAnimation.EasingFunction>
                                                </DoubleAnimation>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </materialDesign:PackIcon.Triggers>
                            </materialDesign:PackIcon>
                            <Button Style="{StaticResource MaterialDesignIconButton}" ToolTip="Open brush settings" Width="24" Height="24" HorizontalAlignment="Right" Command="{s:Action OpenBrushSettings}">
                                <materialDesign:PackIcon Kind="Settings" Height="16" Width="16" />
                            </Button>
                        </StackPanel>

                    </Grid>

                    <!-- Type: LayerEffectRoot -->
                    <Grid Height="24">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.Style>
                            <Style TargetType="{x:Type Grid}">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding GroupType}" Value="LayerEffectRoot">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Style>
                        <shared:ArtemisIcon
                            Grid.Column="0"
                            Cursor="SizeNS"
                            Icon="{Binding LayerPropertyGroup.LayerEffect.Descriptor.Icon}"
                            Width="16"
                            Height="16"
                            Margin="0 5 5 0"
                            Background="Transparent" />
                        <TextBlock Grid.Column="1" ToolTip="{Binding LayerPropertyGroup.LayerEffect.Descriptor.Description}" Margin="0 5 0 0">
                            Effect
                        </TextBlock>
                        <TextBlock Grid.Column="2"
                                   ToolTip="{Binding LayerPropertyGroup.LayerEffect.Descriptor.Description}"
                                   Margin="3 5">
                            -
                        </TextBlock>

                        <!-- Show either the descriptors display name or, if set, the effect name -->
                        <TextBlock Grid.Column="3"
                                   Text="{Binding LayerPropertyGroup.LayerEffect.Descriptor.DisplayName}"
                                   ToolTip="{Binding LayerPropertyGroup.LayerEffect.Descriptor.Description}"
                                   Margin="0 5"
                                   Visibility="{Binding LayerPropertyGroup.LayerEffect.Name, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}" />
                        <TextBlock Grid.Column="4"
                                   Text="{Binding LayerPropertyGroup.LayerEffect.Name}"
                                   ToolTip="{Binding LayerPropertyGroup.LayerEffect.Descriptor.Description}"
                                   Margin="0 5"
                                   Visibility="{Binding LayerPropertyGroup.LayerEffect.Name, Converter={StaticResource NullToVisibilityConverter}}" />

                        <StackPanel Grid.Column="5" Orientation="Horizontal">
                            <ToggleButton
                                Style="{StaticResource MaterialDesignFlatToggleButton}"
                                ToolTip="Toggle suspended state"
                                Width="18"
                                Height="18"
                                IsChecked="{Binding LayerPropertyGroup.LayerEffect.Suspended, Converter={StaticResource InverseBooleanConverter}}"
                                VerticalAlignment="Center" Padding="-25"
                                Margin="5 0"
                                Command="{s:Action SuspendedToggled}">
                                <materialDesign:PackIcon Kind="Eye" Height="13" Width="13" />
                            </ToggleButton>
                            <Button Style="{StaticResource MaterialDesignIconButton}"
                                    ToolTip="Rename"
                                    Width="24"
                                    Height="24"
                                    VerticalAlignment="Center"
                                    Command="{s:Action RenameEffect}">
                                <materialDesign:PackIcon Kind="RenameBox" Height="16" Width="16" />
                            </Button>
                            <Button Style="{StaticResource MaterialDesignIconButton}"
                                    ToolTip="Open effect settings"
                                    Width="24"
                                    Height="24"
                                    VerticalAlignment="Center"
                                    Command="{s:Action OpenEffectSettings}"
                                    Visibility="{Binding LayerPropertyGroup.LayerEffect.ConfigurationDialog, Converter={StaticResource NullToVisibilityConverter}}">
                                <materialDesign:PackIcon Kind="Settings" Height="16" Width="16" />
                            </Button>
                            <Button Style="{StaticResource MaterialDesignIconButton}"
                                    ToolTip="Remove"
                                    Width="24"
                                    Height="24"
                                    VerticalAlignment="Center"
                                    Command="{s:Action DeleteEffect}">
                                <materialDesign:PackIcon Kind="TrashCan" Height="16" Width="16" />
                            </Button>
                        </StackPanel>
                    </Grid>
                </StackPanel>
            </Grid>
        </Border>

        <!-- 
        Do not bind directly to the LayerPropertyGroupViewModel.Children collection
        Instead use a reference provided by the VM that is null when collapsed, virtualization for noobs 
        -->
        <ItemsControl ItemsSource="{Binding Items}"
                      Visibility="{Binding LayerPropertyGroupViewModel.IsExpanded, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"
                      HorizontalAlignment="Stretch"
                      HorizontalContentAlignment="Stretch"
                      dd:DragDrop.DragSourceIgnore="True">
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="VerticalAlignment" Value="Stretch" />
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.Resources>
                <DataTemplate DataType="{x:Type layerProperties:LayerPropertyGroupViewModel}">
                    <ContentControl s:View.Model="{Binding TreeGroupViewModel}"
                                    IsTabStop="False"
                                    HorizontalAlignment="Stretch"
                                    Visibility="{Binding IsVisible, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}" />
                </DataTemplate>
                <DataTemplate DataType="{x:Type layerProperties:LayerPropertyViewModel}">
                    <ContentControl s:View.Model="{Binding TreePropertyViewModel}"
                                    IsTabStop="False"
                                    HorizontalAlignment="Stretch"
                                    Visibility="{Binding IsVisible, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}" />
                </DataTemplate>
            </ItemsControl.Resources>
        </ItemsControl>
    </StackPanel>
</UserControl>