<UserControl 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:xaml="clr-namespace:PixiEditor.AvaloniaUI.Models.Commands.XAML"
             xmlns:viewModels1="clr-namespace:PixiEditor.AvaloniaUI.ViewModels"
             xmlns:converters="clr-namespace:PixiEditor.AvaloniaUI.Helpers.Converters"
             xmlns:palettes1="clr-namespace:PixiEditor.AvaloniaUI.Views.Palettes"
             xmlns:viewportControls="clr-namespace:PixiEditor.AvaloniaUI.Views.Main.ViewportControls"
             xmlns:subViewModels="clr-namespace:PixiEditor.AvaloniaUI.ViewModels.SubViewModels"
             xmlns:document="clr-namespace:PixiEditor.AvaloniaUI.ViewModels.Document"
             xmlns:palettes="clr-namespace:PixiEditor.Extensions.CommonApi.Palettes;assembly=PixiEditor.Extensions.CommonApi"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:DataType="subViewModels:ViewportWindowViewModel"
             x:Class="PixiEditor.AvaloniaUI.Views.Dock.DocumentTemplate">
    <Design.DataContext>
        <subViewModels:ViewportWindowViewModel />
    </Design.DataContext>

    <viewportControls:Viewport
        x:Name="Viewport"
        CenterViewportTrigger="{Binding DockCenterViewportTrigger}"
        ZoomViewportTrigger="{Binding ZoomViewportTrigger}"
        MouseDownCommand="{Binding Path=IoSubViewModel.MouseDownCommand, Source={viewModels1:MainVM}}"
        MouseMoveCommand="{Binding Path=IoSubViewModel.MouseMoveCommand, Source={viewModels1:MainVM}}"
        MouseUpCommand="{Binding Path=IoSubViewModel.MouseUpCommand, Source={viewModels1:MainVM}}"
        MiddleMouseClickedCommand="{Binding IoSubViewModel.PreviewMouseMiddleButtonCommand, Source={viewModels1:MainVM}}"
        Cursor="{Binding ToolsSubViewModel.ToolCursor, Source={viewModels1:MainVM}}"
        GridLinesVisible="{Binding ViewportSubViewModel.GridLinesEnabled, Source={viewModels1:MainVM}}"
        ZoomMode="{Binding ToolsSubViewModel.ActiveTool, Source={viewModels1:MainVM}, Converter={converters:ActiveToolToZoomModeConverter}}"
        ZoomOutOnClick="{Binding ToolsSubViewModel.ZoomTool.ZoomOutOnClick, Source={viewModels1:MainVM}}"
        UseTouchGestures="{Binding StylusSubViewModel.UseTouchGestures, Source={viewModels1:MainVM}}"
        FlipX="{Binding FlipX, Mode=TwoWay}"
        FlipY="{Binding FlipY, Mode=TwoWay}"
        Channels="{Binding Channels, Mode=TwoWay}"
        ContextRequested="Viewport_OnContextMenuOpening"
        Document="{Binding Document}">
        <viewportControls:Viewport.ContextFlyout>
            <Flyout x:CompileBindings="True" x:DataType="subViewModels:ViewportWindowViewModel" ShowMode="Transient">
                <Border CornerRadius="{DynamicResource ControlCornerRadius}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition
                                Width="{Binding Document.Palette, Converter={converters:PaletteItemsToWidthConverter}}" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="1" BorderThickness="0 0 1 0">
                            <ContextMenu Name="ContextMenu" BorderBrush="Transparent" BorderThickness="0" Margin="0" Padding="0">
                                <MenuItem PointerReleased="MenuItem_OnClick"
                                    ui:Translator.Key="SELECT_ALL"
                                    xaml:Menu.Command="PixiEditor.Selection.SelectAll" />
                                <MenuItem PointerReleased="MenuItem_OnClick"
                                          ui:Translator.Key="DESELECT"
                                          xaml:Menu.Command="PixiEditor.Selection.Clear" />
                                <Separator />
                                <MenuItem PointerReleased="MenuItem_OnClick"
                                          ui:Translator.Key="CUT"
                                          xaml:Menu.Command="PixiEditor.Clipboard.Cut" />
                                <MenuItem PointerReleased="MenuItem_OnClick"
                                          ui:Translator.Key="COPY"
                                          xaml:Menu.Command="PixiEditor.Clipboard.Copy" />
                                <MenuItem PointerReleased="MenuItem_OnClick"
                                          ui:Translator.Key="PASTE"
                                          xaml:Menu.Command="PixiEditor.Clipboard.Paste" />
                                <Separator />
                                <MenuItem  PointerReleased="MenuItem_OnClick"
                                           ui:Translator.Key="FLIP_LAYERS_HORIZONTALLY"
                                           xaml:Menu.Command="PixiEditor.Document.FlipLayersHorizontal" />
                                <MenuItem  PointerReleased="MenuItem_OnClick"
                                           ui:Translator.Key="FLIP_LAYERS_VERTICALLY"
                                           xaml:Menu.Command="PixiEditor.Document.FlipLayersVertical" />
                                <Separator />
                                <MenuItem PointerReleased="MenuItem_OnClick"
                                          ui:Translator.Key="ROT_LAYERS_90_D"
                                          xaml:Menu.Command="PixiEditor.Document.Rotate90DegLayers" />
                                <MenuItem PointerReleased="MenuItem_OnClick"
                                          ui:Translator.Key="ROT_LAYERS_180_D"
                                          xaml:Menu.Command="PixiEditor.Document.Rotate180DegLayers" />
                                <MenuItem PointerReleased="MenuItem_OnClick"
                                          ui:Translator.Key="ROT_LAYERS_-90_D"
                                          xaml:Menu.Command="PixiEditor.Document.Rotate270DegLayers" />
                            </ContextMenu>
                        </Border>
                        <ScrollViewer Margin="5" Grid.Column="0" HorizontalScrollBarVisibility="Disabled"
                                      VerticalScrollBarVisibility="Auto" MaxHeight="{Binding #ContextMenu.Items,
                                      Converter={converters:PaletteItemsHeightConverter}}">
                            <ItemsControl ItemsSource="{Binding Document.Palette}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel MaxWidth="{Binding Document.Palette, Converter={converters:PaletteItemsToWidthConverter}}" Orientation="Horizontal"
                                                   HorizontalAlignment="Left" VerticalAlignment="Top" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate x:DataType="palettes:PaletteColor">
                                        <palettes1:PaletteColorControl Cursor="Hand" CornerRadius="0"
                                                                       ui:Translator.TooltipKey="CLICK_SELECT_PRIMARY"
                                                                       PointerReleased="InputElement_OnPointerReleased"
                                                                       Width="22" Height="22" Color="{Binding}">
                                            <Interaction.Behaviors>
                                                <EventTriggerBehavior EventName="PointerPressed">
                                                    <InvokeCommandAction
                                                        Command="{xaml:Command PixiEditor.Colors.SelectColor, UseProvided=True}"
                                                        CommandParameter="{Binding}" />
                                                </EventTriggerBehavior>
                                            </Interaction.Behaviors>
                                        </palettes1:PaletteColorControl>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </ScrollViewer>
                    </Grid>
                </Border>
            </Flyout>
        </viewportControls:Viewport.ContextFlyout>
    </viewportControls:Viewport>
</UserControl>