<Window
    x:ClassModifier="internal"
    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:PixiEditor.Views.UserControls.Palettes" 
    xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters" 
    xmlns:gif="http://wpfanimatedgif.codeplex.com" 
    xmlns:usercontrols="clr-namespace:PixiEditor.Views.UserControls" 
    xmlns:views="clr-namespace:PixiEditor.Views"
    xmlns:behaviours="clr-namespace:PixiEditor.Helpers.Behaviours"
    xmlns:PixiEditor="clr-namespace:PixiEditor"
    xmlns:dialogs="clr-namespace:PixiEditor.Views.Dialogs"
    xmlns:helpers="clr-namespace:PixiEditor.Helpers"
    xmlns:ui="clr-namespace:PixiEditor.Extensions.UI;assembly=PixiEditor.Extensions"
    xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:commands="clr-namespace:PixiEditor.Models.Commands.Attributes.Commands"
    xmlns:xaml="clr-namespace:PixiEditor.Models.Commands.XAML"
    x:Class="PixiEditor.Views.Dialogs.PalettesBrowser"
    mc:Ignorable="d"
    WindowStartupLocation="CenterScreen"
    Height="600" Width="850"
    WindowStyle="None"
    x:Name="palettesBrowser"
    FlowDirection="{helpers:Localization FlowDirection}"
    ui:Translator.Key="PALETTE_BROWSER">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
    </Window.Resources>
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="32"  GlassFrameThickness="0.1"
                      ResizeBorderThickness="{x:Static SystemParameters.WindowResizeBorderThickness}" />
    </WindowChrome.WindowChrome>

    <Window.CommandBindings>
        <CommandBinding Command="{x:Static SystemCommands.CloseWindowCommand}" CanExecute="CommandBinding_CanExecute"
                        Executed="CommandBinding_Executed_Close" />
    </Window.CommandBindings>

    <Grid Background="{StaticResource AccentColor}" FocusVisualStyle="{x:Null}" Focusable="True" MouseDown="Grid_MouseDown">
        <Grid.RowDefinitions>
            <RowDefinition Height="35" />
            <RowDefinition Height="45"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <dialogs:DialogTitleBar TitleKey="PALETTE_BROWSER" CloseCommand="{x:Static SystemCommands.CloseWindowCommand}"/>

        <DockPanel Background="{StaticResource MainColor}" Grid.Row="1">
            <StackPanel HorizontalAlignment="Left" Margin="10" Orientation="Horizontal" VerticalAlignment="Center">
                <Label ui:Translator.Key="SORT_BY" Style="{StaticResource BaseLabel}" VerticalAlignment="Center"/>
                <ComboBox x:Name="sortingComboBox" VerticalAlignment="Center" SelectionChanged="SortingComboBox_SelectionChanged">
                    <ComboBoxItem IsSelected="True" ui:Translator.Key="DEFAULT"/>
                    <ComboBoxItem ui:Translator.Key="ALPHABETICAL"/>
                    <ComboBoxItem ui:Translator.Key="COLOR_COUNT"/>
                </ComboBox>
                <ToggleButton Margin="10 0 0 0" x:Name="toggleBtn"
                              IsChecked="{Binding SortAscending, ElementName=palettesBrowser}"
                              Focusable="False"
                              Style="{StaticResource PixiEditorDockThemeToolButtonStyle}">
                    <Image Width="24" Height="24" Source="/Images/ChevronsDown.png">
                        <Image.Style>
                            <Style TargetType="{x:Type Image}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsChecked, ElementName=toggleBtn}" Value="true">
                                        <Setter Property="RenderTransform">
                                            <Setter.Value>
                                                <RotateTransform Angle="180" CenterX="11.5" CenterY="11.5"/>
                                            </Setter.Value>
                                        </Setter>
                                        <Setter Property="ui:Translator.TooltipKey" Value="ASCENDING"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding IsChecked, ElementName=toggleBtn}" Value="false">
                                        <Setter Property="ui:Translator.TooltipKey" Value="DESCENDING"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </ToggleButton>
                <Label Margin="10 0 0 0" ui:Translator.Key="NAME" Style="{StaticResource BaseLabel}" VerticalAlignment="Center"/>
                <usercontrols:InputBox
                                       Text="{Binding NameFilter, Delay=100, ElementName=palettesBrowser, UpdateSourceTrigger=PropertyChanged}"
                                       VerticalAlignment="Center"
                                       Style="{StaticResource DarkTextBoxStyle}" Width="150">
                    <b:Interaction.Behaviors>
                        <behaviours:TextBoxFocusBehavior SelectOnMouseClick="True" ConfirmOnEnter="True"
                                                         FocusNext="{Binding ElementName=numberInput, Path=FocusNext}"/>
                        <behaviours:GlobalShortcutFocusBehavior/>
                    </b:Interaction.Behaviors>
                </usercontrols:InputBox>

                <Label Margin="10 0 0 0" ui:Translator.Key="COLORS" Style="{StaticResource BaseLabel}" VerticalAlignment="Center"/>
                <ComboBox x:Name="colorsComboBox" VerticalAlignment="Center" SelectionChanged="ColorsComboBox_SelectionChanged">
                    <ComboBoxItem IsSelected="True" ui:Translator.Key="ANY"/>
                    <ComboBoxItem ui:Translator.Key="MAX"/>
                    <ComboBoxItem ui:Translator.Key="MIN"/>
                    <ComboBoxItem ui:Translator.Key="EXACT"/>
                </ComboBox>
                <usercontrols:NumberInput Width="50" VerticalAlignment="Center" Margin="10 0 0 0"
                                   FocusNext="True"
                                   Value="{Binding ColorsNumber, ElementName=palettesBrowser, Mode=TwoWay}"/>
                <CheckBox Margin="10 0 0 0" VerticalAlignment="Center"
                          IsChecked="{Binding ShowOnlyFavourites, ElementName=palettesBrowser}" ui:Translator.Key="FAVORITES"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 10 0">
                <Button ui:Translator.TooltipKey="ADD_FROM_CURRENT_PALETTE" Command="{Binding ElementName=palettesBrowser, Path=AddFromPaletteCommand}" Cursor="Hand" Margin="10 0"
                        Style="{StaticResource ToolButtonStyle}" Width="24" Height="24">
                    <Image Source="/Images/Plus-square.png"/>
                </Button>
                <Button Cursor="Hand" Click="OpenFolder_OnClick" Style="{StaticResource ImageButtonStyle}" Width="24" Height="24"
                        ui:Translator.TooltipKey="OPEN_PALETTES_DIR_TOOLTIP">
                    <Image Source="/Images/Folder.png"/>
                </Button>
                <Button HorizontalAlignment="Right" Margin="10 0 0 0" ui:Translator.TooltipKey="BROWSE_ON_LOSPEC_TOOLTIP"
                        Style="{StaticResource ImageButtonStyle}" Width="24" Height="24"
                        Click="BrowseOnLospec_OnClick"
                        CommandParameter="https://lospec.com/palette-list">
                    <Image Source="/Images/Globe.png"/>
                </Button>
                <Button HorizontalAlignment="Right" Margin="10 0 0 0" ui:Translator.TooltipKey="IMPORT_FROM_FILE_TOOLTIP"
                        Style="{StaticResource ImageButtonStyle}" Width="24" Height="24"
                        Click="ImportFromFile_OnClick">
                    <Image Source="/Images/hard-drive.png"/>
                </Button>
            </StackPanel>
        </DockPanel>
        <Grid Grid.Row="2" Margin="10">
            <TextBlock ui:Translator.Key="COULD_NOT_LOAD_PALETTE" Foreground="White" FontSize="20" HorizontalAlignment="Center"
                       VerticalAlignment="Center" Visibility="{Binding Visibility, Converter={converters:OppositeVisibilityConverter}, ElementName=itemsControl}"/>
            <StackPanel Panel.ZIndex="10" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="{Binding ElementName=palettesBrowser, Path=SortedResults.Count, Converter={converters:CountToVisibilityConverter}}">
                <TextBlock ui:Translator.Key="NO_PALETTES_FOUND" Foreground="White" FontSize="20" TextAlignment="Center"/>
                <TextBlock Margin="0 10 0 0">
                    <Hyperlink Foreground="Gray" Cursor="Hand" FontSize="18" NavigateUri="https://lospec.com/palette-list"
                               RequestNavigate="Hyperlink_OnRequestNavigate">
                        <TextBlock ui:Translator.Key="LOSPEC_LINK_TEXT"/>
                    </Hyperlink>
                </TextBlock>
                <Image Width="128" Height="128" Source="/Images/Search.png"/>
            </StackPanel>
            <ScrollViewer x:Name="scrollViewer" Margin="5" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" ScrollChanged="ScrollViewer_ScrollChanged">
                <ItemsControl x:Name="itemsControl" ItemsSource="{Binding SortedResults, ElementName=palettesBrowser}"
                              Visibility="{Binding PaletteList.FetchedCorrectly, Converter={StaticResource BoolToVisibilityConverter}, ElementName=palettesBrowser}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <local:PaletteItem Palette="{Binding}"
                                               OnRename="PaletteItem_OnRename"
                                               ToggleFavouriteCommand="{Binding ToggleFavouriteCommand, ElementName=palettesBrowser}"
                                               DeletePaletteCommand="{Binding DeletePaletteCommand, ElementName=palettesBrowser}"
                                               ImportPaletteCommand="{Binding ImportPaletteCommand, ElementName=palettesBrowser}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
            <Image gif:ImageBehavior.AnimatedSource="/Images/Processing.gif" HorizontalAlignment="Center" VerticalAlignment="Center"
                   Visibility="{Binding IsFetching, Converter={StaticResource BoolToVisibilityConverter}, ElementName=palettesBrowser}"
                   Height="50" gif:ImageBehavior.AnimationSpeedRatio="1.5"/>
        </Grid>
    </Grid>
</Window>
