<UserControl x:Class="FasdDesktopUi.Basics.UserControls.HierarchicalSelectionControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:FasdDesktopUi.Basics.UserControls"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ico="clr-namespace:FasdDesktopUi.Basics.UserControls.AdaptableIcon;assembly=F4SD-AdaptableIcon"
             xmlns:vc="clr-namespace:FasdDesktopUi.Basics.Converter"
             xmlns:models="clr-namespace:FasdDesktopUi.Basics.Models"
             mc:Ignorable="d"
             x:Name="HierarchySelector"
             Focusable="True"
             d:DesignHeight="60"
             d:DesignWidth="350">

    <UserControl.Resources>
        <vc:LanguageDefinitionsConverter x:Key="LanguageConverter" />
        <vc:NullValueToVisibilityConverter x:Key="NullToVisibility" />

        <ControlTemplate x:Key="HierarchicalComboBoxTemplate"
                         TargetType="{x:Type ComboBox}">
            <Grid>
                <ToggleButton x:Name="toggleButton"
                              Grid.ColumnSpan="2"
                              Focusable="False"
                              ClickMode="Press"
                              IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                              Background="{TemplateBinding Background}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}"
                              Style="{StaticResource ComboBoxToggleButton}" />

                <TextBlock x:Name="SelectedPathText"
                           IsHitTestVisible="False"
                           Text="{Binding SelectedItem.FullPath, RelativeSource={RelativeSource AncestorType={x:Type local:HierarchicalSelectionControl}}}"
                           Margin="6 0"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Stretch"
                           Foreground="{DynamicResource FontColor.Menu.Categories}"
                           TextTrimming="CharacterEllipsis" />

                <Popup x:Name="PART_Popup"
                       Placement="Bottom"
                       PlacementTarget="{Binding ElementName=toggleButton}"
                       AllowsTransparency="True"
                       Focusable="False"
                       IsOpen="{TemplateBinding IsDropDownOpen}"
                       PopupAnimation="Fade">
                    <Border Width="{Binding ActualWidth, ElementName=HierarchySelector}"
                            Background="{DynamicResource BackgroundColor.Menu.SubCategory}"
                            BorderBrush="{DynamicResource BackgroundColor.Menu.SubCategory.Hover}"
                            BorderThickness="1"
                            CornerRadius="12"
                            Padding="14">
                        <Border.Effect>
                            <DropShadowEffect BlurRadius="16"
                                              Color="{DynamicResource DropShadowColor.Menu}"
                                              Direction="320"
                                              ShadowDepth="8"
                                              Opacity="0.35" />
                        </Border.Effect>

                        <StackPanel>
                            <Border Background="{DynamicResource BackgroundColor.DetailsPage.DataHistory.ValueColumn}"
                                    CornerRadius="8"
                                    Padding="6 4"
                                    Margin="0 0 0 12">
                                <Grid>
                                    <TextBox x:Name="PART_SearchTextBox"
                                             Style="{DynamicResource Customizable.Editable.TextBox.EditOnly}"
                                             Background="Transparent"
                                             Padding="27 5 5 5"
                                             BorderThickness="0"
                                             VerticalContentAlignment="Center" />
                                    <ico:AdaptableIcon SelectedInternIcon="menuBar_search"
                                                       Style="{DynamicResource Menu.MenuBar.PinnedIcon.Base}"
                                                       BorderPadding="5 5 5 7"
                                                       Margin="0"
                                                       IconBackgroundColor="Transparent"
                                                       HorizontalAlignment="Left"
                                                       VerticalAlignment="Center" />
                                    <TextBlock Margin="30 0 0 0"
                                               Foreground="{DynamicResource FontColor.DetailsPage.DataHistory.Date}"
                                               IsHitTestVisible="False"
                                               VerticalAlignment="Center">
                                        <TextBlock.Style>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="Visibility" Value="Collapsed" />
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding Text, ElementName=PART_SearchTextBox}"
                                                                 Value="">
                                                        <Setter Property="Text"
                                                                Value="{Binding Path=SearchPlaceholderText, RelativeSource={RelativeSource AncestorType={x:Type local:HierarchicalSelectionControl}}}" />
                                                        <Setter Property="Visibility" Value="Visible" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>
                                </Grid>
                            </Border>

                            <Border Background="{DynamicResource BackgroundColor.DetailsPage.DataHistory.ValueColumn}"
                                    CornerRadius="10"
                                    Padding="4"
                                    BorderBrush="{DynamicResource BackgroundColor.Menu.SubCategory.Hover}"
                                    BorderThickness="1">
                                <ScrollViewer x:Name="PART_ItemsScrollViewer"
                                              MaxHeight="320"
                                              VerticalScrollBarVisibility="Auto"
                                              Background="Transparent"
                                              BorderThickness="0"
                                              PreviewMouseWheel="ItemsScrollViewer_PreviewMouseWheel">
                                    <TreeView x:Name="PART_TreeView"
                                              Background="Transparent"
                                              BorderThickness="0"
                                              HorizontalContentAlignment="Stretch"
                                              ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}}">
                                        <TreeView.Resources>
                                            <Style x:Key="HierarchyTreeViewItemStyle"
                                                   TargetType="TreeViewItem"
                                                   BasedOn="{StaticResource {x:Type TreeViewItem}}">
                                                <Setter Property="IsExpanded"
                                                        Value="{Binding IsExpanded, Mode=TwoWay}" />
                                                <Setter Property="Margin"
                                                        Value="0,-1,0,1" />
                                                <Setter Property="Padding"
                                                        Value="0" />
                                            </Style>

                                            <HierarchicalDataTemplate DataType="{x:Type models:HierarchicalSelectionItem}"
                                                                      ItemsSource="{Binding Children}">
                                                <Border x:Name="NodeContainer"
                                                        Padding="10 4"
                                                        Margin="2 1"
                                                        Background="Transparent"
                                                        CornerRadius="6"
                                                        BorderBrush="Transparent"
                                                        BorderThickness="1">
                                                    <TextBlock x:Name="NodeText"
                                                               Text="{Binding DisplayName}"
                                                               Foreground="{DynamicResource FontColor.Menu.Categories}"
                                                               FontSize="14" />
                                                </Border>
                                                <HierarchicalDataTemplate.Triggers>
                                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}, Path=IsMouseOver}"
                                                                 Value="True">
                                                        <Setter TargetName="NodeContainer"
                                                                Property="Background"
                                                                Value="{DynamicResource BackgroundColor.Menu.SubCategory.Hover}" />
                                                        <Setter TargetName="NodeText"
                                                                Property="Foreground"
                                                                Value="{DynamicResource FontColor.Menu.Categories.Hover}" />
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}, Path=IsSelected}"
                                                                 Value="True">
                                                        <Setter TargetName="NodeContainer"
                                                                Property="Background"
                                                                Value="{DynamicResource BackgroundColor.Menu.SubCategory}" />
                                                        <Setter TargetName="NodeContainer"
                                                                Property="BorderBrush"
                                                                Value="{DynamicResource Color.FunctionMarker}" />
                                                        <Setter TargetName="NodeText"
                                                                Property="Foreground"
                                                                Value="{DynamicResource FontColor.Menu.Categories}" />
                                                    </DataTrigger>
                                                </HierarchicalDataTemplate.Triggers>
                                            </HierarchicalDataTemplate>
                                        </TreeView.Resources>
                                        <TreeView.ItemContainerStyle>
                                            <StaticResource ResourceKey="HierarchyTreeViewItemStyle" />
                                        </TreeView.ItemContainerStyle>
                                    </TreeView>
                                </ScrollViewer>
                            </Border>
                        </StackPanel>
                    </Border>
                </Popup>
            </Grid>
        </ControlTemplate>
    </UserControl.Resources>

    <Grid>
        <ComboBox x:Name="ComboBoxControl"
                  Background="{Binding ElementName=HierarchySelector, Path=ComboBoxBackground}"
                  BorderBrush="Transparent"
                  BorderThickness="0"
                  Foreground="{DynamicResource FontColor.Menu.Categories}"
                  ItemsSource="{Binding VisibleItems, ElementName=HierarchySelector}"
                  DisplayMemberPath="FullPath"
                  Template="{StaticResource HierarchicalComboBoxTemplate}"
                  SelectedIndex="-1"
                  IsSynchronizedWithCurrentItem="False"
                  DropDownOpened="ComboBoxControl_DropDownOpened"
                  DropDownClosed="ComboBoxControl_DropDownClosed"
                  IsEditable="False"
                  Padding="0" />
    </Grid>
</UserControl>
