<UserControl x:Class="FasdDesktopUi.Basics.UserControls.ComboBoxPageable"
             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:FasdDesktopUi.Basics.UserControls"
             xmlns:ico="clr-namespace:FasdDesktopUi.Basics.UserControls.AdaptableIcon;assembly=F4SD-AdaptableIcon"
             xmlns:vc="clr-namespace:FasdDesktopUi.Basics.Converter"
             mc:Ignorable="d"
             x:Name="ComboBoxPagableUc"
             d:DesignHeight="450"
             d:DesignWidth="800">

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

        <ControlTemplate x:Key="ComboBoxPagableTemplate"
                         TargetType="{x:Type ComboBox}">

            <Grid x:Name="templateRoot"
                  SnapsToDevicePixels="true">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"
                                      Width="0" />
                </Grid.ColumnDefinitions>

                <Popup x:Name="PART_Popup"
                       AllowsTransparency="true"
                       Grid.ColumnSpan="2"
                       IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                       Margin="1"
                       Placement="Bottom"
                       PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
                    <StackPanel>
                        <Grid>
                            <TextBox x:Name="SearchTextBox"
                                     Style="{DynamicResource Customizable.Editable.TextBox.EditOnly}"
                                     Background="{TemplateBinding Background}"
                                     FontSize="14"
                                     VerticalAlignment="Center"
                                     Padding="27 5 5 5"
                                     Margin="0"
                                     TextChanged="SearchTextBox_TextChanged" />

                            <ico:AdaptableIcon SelectedInternIcon="menuBar_search"
                                               HorizontalAlignment="Left"
                                               BorderPadding="5 5 5 7"
                                               Margin="0"
                                               Style="{DynamicResource Menu.MenuBar.PinnedIcon.Base}" />

                            <TextBlock IsHitTestVisible="False"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Left"
                                       Margin="30 0 0 0"
                                       Foreground="{DynamicResource FontColor.DetailsPage.DataHistory.Date}">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Text, ElementName=SearchTextBox}"
                                                         Value="">
                                                <Setter Property="Text"
                                                        Value="{Binding Converter={StaticResource LanguageConverter}, ConverterParameter=Searchbar.Placeholder}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </Grid>

                        <Border CornerRadius="7.5"
                                Padding="10 0"
                                Margin="5"
                                HorizontalAlignment="Center"
                                Background="{TemplateBinding Background}">
                            <StackPanel Orientation="Horizontal"
                                        HorizontalAlignment="Center"
                                        Background="#01010101">

                                <ico:AdaptableIcon x:Name="PageBackButton"
                                                   SelectedInternIcon="misc_chevron_left"
                                                   Style="{DynamicResource SettingsPage.Close.Icon}"
                                                   MouseLeftButtonUp="PageBackButton_MouseLeftButtonUp"
                                                   TouchDown="PageBackButton_TouchDown"
                                                   BorderPadding="7.5"
                                                   IconHeight="30"
                                                   IconWidth="30" />

                                <TextBlock x:Name="PageTrackerTextBlock"
                                           VerticalAlignment="Center" />

                                <ico:AdaptableIcon x:Name="PageForButton"
                                                   SelectedInternIcon="misc_chevron_right"
                                                   Style="{DynamicResource SettingsPage.Close.Icon}"
                                                   MouseLeftButtonUp="PageForButton_MouseLeftButtonUp"
                                                   TouchDown="PageForButton_TouchDown"
                                                   BorderPadding="7.5"
                                                   IconHeight="30"
                                                   IconWidth="30" />
                                <CheckBox x:Name="ResetFilterCheckbox"
                                          Style="{DynamicResource ToggleSwitch}"
                                          Margin="20 0 0 0"
                                          HorizontalAlignment="Left"
                                          IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ComboBoxPageable}}, Path=ResetFilter, Mode=TwoWay}"
                                          VerticalAlignment="Center"
                                          Cursor="Hand"
                                          >
                                </CheckBox>
                                <TextBlock VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ComboBoxPageable}}, Path=ResetFilterLabel, Mode=TwoWay}"/>
                            </StackPanel>
                        </Border>

                        <Border MinWidth="{Binding ActualWidth, ElementName=templateRoot}"
                                MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border x:Name="dropDownBorder"
                                    CornerRadius="7.5"
                                    Background="{DynamicResource BackgroundColor.Menu.SubCategory}">
                                <ScrollViewer x:Name="DropDownScrollViewer">
                                    <Grid x:Name="grid"
                                          RenderOptions.ClearTypeHint="Enabled">
                                        <Canvas x:Name="canvas"
                                                HorizontalAlignment="Left"
                                                Height="0"
                                                VerticalAlignment="Top"
                                                Width="0">
                                            <Border x:Name="opaqueRect"
                                                    CornerRadius="7.5"
                                                    Background="{Binding Background, ElementName=dropDownBorder}"
                                                    Height="{Binding ActualHeight, ElementName=dropDownBorder}"
                                                    Width="{Binding ActualWidth, ElementName=dropDownBorder}" />
                                        </Canvas>
                                        <ItemsPresenter x:Name="ItemsPresenter"
                                                        KeyboardNavigation.DirectionalNavigation="Contained"
                                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                    </Grid>
                                </ScrollViewer>
                            </Border>
                        </Border>
                    </StackPanel>
                </Popup>
                <ToggleButton x:Name="toggleButton"
                              Background="{TemplateBinding Background}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}"
                              Grid.ColumnSpan="2"
                              IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                              Style="{StaticResource ComboBoxToggleButton}" />
                <ContentPresenter x:Name="contentPresenter"
                                  ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
                                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                  Content="{TemplateBinding SelectionBoxItem}"
                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  IsHitTestVisible="false"
                                  Margin="{TemplateBinding Padding}"
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
            </Grid>

            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled"
                         Value="false">
                    <Setter Property="Opacity"
                            Value="0.7" />
                </Trigger>
                <Trigger Property="HasDropShadow"
                         SourceName="PART_Popup"
                         Value="true">
                </Trigger>
                <Trigger Property="HasItems"
                         Value="false">
                    <Setter Property="Height"
                            TargetName="dropDownBorder"
                            Value="95" />
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsGrouping"
                                   Value="true" />
                        <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping"
                                   Value="false" />
                    </MultiTrigger.Conditions>
                    <Setter Property="ScrollViewer.CanContentScroll"
                            Value="false" />
                </MultiTrigger>
                <Trigger Property="ScrollViewer.CanContentScroll"
                         SourceName="DropDownScrollViewer"
                         Value="false">
                    <Setter Property="Canvas.Top"
                            TargetName="opaqueRect"
                            Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}" />
                    <Setter Property="Canvas.Left"
                            TargetName="opaqueRect"
                            Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </UserControl.Resources>

    <Grid>
        <ComboBox x:Name="ComboBoxControl"
                  DropDownOpened="ComboBoxControl_DropDownOpened"
                  DropDownClosed="ComboBoxControl_DropDownClosed"
                  Background="{Binding ElementName=ComboBoxPagableUc, Path=ComboBoxBackground}"
                  BorderBrush="{Binding ElementName=ComboBoxPagableUc, Path=BorderBrush}"
                  BorderThickness="{Binding ElementName=ComboBoxPagableUc, Path=BorderThickness}"
                  ItemsSource="{Binding ElementName=ComboBoxPagableUc, Path=ItemData}"
                  SelectedItem="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ComboBoxPageable}}, Path=SelectedItem, Mode=TwoWay}"
                  DisplayMemberPath="Key"
                  Template="{StaticResource ComboBoxPagableTemplate}" />
    </Grid>
</UserControl>
