<dialogs:PixiEditorPopup
    x:Class="PixiEditor.AvaloniaUI.Views.Windows.PalettesBrowser"
    x:ClassModifier="internal"
    xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ui="clr-namespace:PixiEditor.Extensions.UI;assembly=PixiEditor.Extensions"
    xmlns:dialogs="clr-namespace:PixiEditor.AvaloniaUI.Views.Dialogs"
    xmlns:input="clr-namespace:PixiEditor.AvaloniaUI.Views.Input"
    xmlns:behaviours="clr-namespace:PixiEditor.AvaloniaUI.Helpers.Behaviours"
    xmlns:converters="clr-namespace:PixiEditor.AvaloniaUI.Helpers.Converters"
    xmlns:ui1="clr-namespace:PixiEditor.AvaloniaUI.Helpers.UI"
    xmlns:palettes1="clr-namespace:PixiEditor.AvaloniaUI.Views.Palettes"
    xmlns:indicators1="clr-namespace:PixiEditor.AvaloniaUI.Views.Indicators"
    mc:Ignorable="d"
    Height="600" Width="850"
    x:Name="palettesBrowser"
    Title="PALETTE_BROWSER">
    <Grid Background="{DynamicResource ThemeBackgroundBrush1}" Focusable="True" PointerPressed="Grid_MouseDown">
        <Grid.RowDefinitions>
            <RowDefinition Height="45"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <DockPanel Background="{DynamicResource ThemeBackgroundBrush}" Grid.Row="0">
            <StackPanel HorizontalAlignment="Left" Margin="10" Orientation="Horizontal" VerticalAlignment="Center">
                <Label ui:Translator.Key="SORT_BY" VerticalAlignment="Center"/>
                <ComboBox x:Name="sortingComboBox" VerticalAlignment="Center" SelectedIndex="0" SelectionChanged="SortingComboBox_SelectionChanged">
                    <ComboBoxItem 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">
                    <Image Width="24" Height="24" Source="/Images/ChevronsDown.png">
                        <Image.Styles>
                            <Style Selector="Image">
                                <Setter Property="ui:Translator.TooltipKey" Value="DESCENDING"/>
                            </Style>
                            <Style Selector="Image.IsChecked">
                                <Setter Property="RenderTransform">
                                    <Setter.Value>
                                        <RotateTransform Angle="180" CenterX="11.5" CenterY="11.5"/>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="ui:Translator.TooltipKey" Value="ASCENDING"/>
                                </Style>
                        </Image.Styles>
                    </Image>
                </ToggleButton>
                <Label Margin="10 0 0 0" ui:Translator.Key="NAME" VerticalAlignment="Center"/>
                <input:InputBox
                                       Text="{Binding NameFilter, ElementName=palettesBrowser}"
                                       VerticalAlignment="Center" Width="150">
                    <Interaction.Behaviors>
                        <behaviours:TextBoxFocusBehavior SelectOnMouseClick="True" ConfirmOnEnter="True"
                                                         FocusNext="{Binding ElementName=numberInput, Path=FocusNext}"/>
                        <behaviours:GlobalShortcutFocusBehavior/>
                    </Interaction.Behaviors>
                </input:InputBox>

                <Label Margin="10 0 0 0" ui:Translator.Key="COLORS" VerticalAlignment="Center"/>
                <ComboBox x:Name="colorsComboBox" VerticalAlignment="Center" SelectedIndex="0" SelectionChanged="ColorsComboBox_SelectionChanged">
                    <ComboBoxItem ui:Translator.Key="ANY"/>
                    <ComboBoxItem ui:Translator.Key="MAX"/>
                    <ComboBoxItem ui:Translator.Key="MIN"/>
                    <ComboBoxItem ui:Translator.Key="EXACT"/>
                </ComboBox>
                <input: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" Width="24" Height="24">
                    <Image Source="/Images/Plus-square.png"/>
                </Button>
                <Button Cursor="Hand" Click="OpenFolder_OnClick" 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"
                        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"
                        Width="24" Height="24"
                        Click="ImportFromFile_OnClick">
                    <Image Source="/Images/hard-drive.png"/>
                </Button>
            </StackPanel>
        </DockPanel>
        <Grid Grid.Row="1" Margin="10">
            <TextBlock ui:Translator.Key="COULD_NOT_LOAD_PALETTE" Foreground="White" FontSize="20" HorizontalAlignment="Center"
                       VerticalAlignment="Center" IsVisible="{Binding !IsVisible, ElementName=itemsControl}"/>
            <StackPanel Panel.ZIndex="10" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center"
                        IsVisible="{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">
                    <TextBlock ui:Translator.Key="LOSPEC_LINK_TEXT"
                               ui1:Hyperlink.Url="https://lospec.com/palette-list"/>
                </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}"
                              IsVisible="{Binding PaletteList.FetchedCorrectly, ElementName=palettesBrowser}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <palettes1: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>
            <indicators1:LoadingIndicator
                IsVisible="{Binding IsFetching, ElementName=palettesBrowser}"
                HorizontalAlignment="Center" VerticalAlignment="Center"
                Height="50"/>
        </Grid>
    </Grid>
</dialogs:PixiEditorPopup>
