<UserControl x:Class="PixiEditor.Views.UserControls.Layers.LayerControl"
             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.Layers"
             xmlns:conv="clr-namespace:PixiEditor.Helpers.Converters"
             xmlns:controls="clr-namespace:PixiEditor.Views.UserControls"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors" 
             xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
             xmlns:helpers="clr-namespace:PixiEditor.Helpers.UI"
             xmlns:cmds="clr-namespace:PixiEditor.Models.Commands.XAML"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:views="clr-namespace:PixiEditor.Views"
             xmlns:ui="clr-namespace:PixiEditor.Extensions.UI;assembly=PixiEditor.Extensions"
             mc:Ignorable="d" 
             Focusable="True"
             d:DesignHeight="35" d:DesignWidth="250" Name="uc"
             MouseLeave="LayerItem_OnMouseLeave" MouseEnter="LayerItem_OnMouseEnter">
    <UserControl.Resources>
        <conv:BoolToBrushConverter x:Key="LayerBorderConverter" FalseBrush="White" TrueBrush="Black"/>
        <conv:BoolToBrushConverter x:Key="MaskBorderConverter" FalseBrush="Black" TrueBrush="White"/>
    </UserControl.Resources>
    <Border BorderThickness="0 0 0 0.5" BorderBrush="Gray" MinWidth="60" Focusable="True" Tag="{Binding ElementName=uc}">
        <Border.Background>
            <Binding ElementName="uc" Path="Layer.Selection">
                <Binding.Converter>
                    <conv:StructureMemberSelectionTypeToColorConverter
                        SoftColor="{StaticResource SoftSelectedLayerColor}"
                        HardColor="{StaticResource SelectedLayerColor}"
                        NoneColor="Transparent"
                        />
                </Binding.Converter>
            </Binding>
        </Border.Background>
        <i:Interaction.Behaviors>
            <behaviors:ClearFocusOnClickBehavior/>
        </i:Interaction.Behaviors>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="10"/>
                <RowDefinition Height="25"/>
            </Grid.RowDefinitions>
            <Grid AllowDrop="True" DragEnter="Grid_DragEnter" Drop="Grid_Drop_Top" DragLeave="Grid_DragLeave" Grid.Row="0" Grid.ColumnSpan="3" Background="Transparent"/>
            <Grid Grid.Row="1" Grid.RowSpan="3" Margin="0,-10,0,0" VerticalAlignment="Center" AllowDrop="False">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="24"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <CheckBox Style="{StaticResource ImageCheckBox}" VerticalAlignment="Center"
                      IsThreeState="False" HorizontalAlignment="Center" 
                      IsChecked="{Binding Path=Layer.IsVisibleBindable, ElementName=uc}" 
                      Grid.Column="0" Height="16" />
                <StackPanel Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Left">
                    <Rectangle Width="{Binding Path=(helpers:TreeViewItemHelper.Indent).Value, Mode=OneWay, RelativeSource={RelativeSource AncestorType=ItemsPresenter}}" Fill="Transparent" StrokeThickness="0"/>
                    <Border 
                        Visibility="{Binding Layer.ClipToMemberBelowEnabledBindable, ElementName=uc, Converter={conv:BoolToVisibilityConverter}}"
                        Background="{StaticResource PixiRed}" Width="3" Margin="1,1,2,1" CornerRadius="1"/>
                    <Border 
                        Width="32" Height="32" 
                        BorderThickness="1"
                        RenderOptions.BitmapScalingMode="NearestNeighbor"
                        MouseDown="LayerMouseDown">
                        <Border.Background>
                            <ImageBrush ImageSource="/Images/CheckerTile.png" TileMode="Tile" Viewport="0, 0, 0.20, 0.20"/>
                        </Border.Background>
                        <Border.BorderBrush>
                            <MultiBinding Converter="{StaticResource LayerBorderConverter}">
                                <Binding ElementName="uc" Path="Layer.ShouldDrawOnMask"/>
                                <Binding ElementName="uc" Path="Layer.HasMaskBindable"/>
                            </MultiBinding>
                        </Border.BorderBrush>
                        <Image Source="{Binding Layer.PreviewBitmap,ElementName=uc}" Stretch="Uniform" Width="30" Height="30"
                           RenderOptions.BitmapScalingMode="NearestNeighbor" IsHitTestVisible="False"/>
                    </Border>
                    <Border 
                        Width="32" Height="32" 
                        BorderThickness="1" 
                        Margin="3,0,0,0"
                        RenderOptions.BitmapScalingMode="NearestNeighbor"
                        Visibility="{Binding Layer.HasMaskBindable, ElementName=uc, Converter={conv:BoolToVisibilityConverter}}"
                        MouseDown="MaskMouseDown">
                        <Border.Background>
                            <ImageBrush ImageSource="/Images/CheckerTile.png" TileMode="Tile" Viewport="0, 0, 0.20, 0.20"/>
                        </Border.Background>
                        <Border.BorderBrush>
                            <MultiBinding Converter="{StaticResource MaskBorderConverter}">
                                <Binding ElementName="uc" Path="Layer.ShouldDrawOnMask"/>
                            </MultiBinding>
                        </Border.BorderBrush>
                        <Grid IsHitTestVisible="False">
                            <Image Source="{Binding Layer.MaskPreviewBitmap,ElementName=uc}" Stretch="Uniform" Width="30" Height="30"
                           RenderOptions.BitmapScalingMode="NearestNeighbor" IsHitTestVisible="False"/>
                            <Path 
                                Data="M 2 0 L 10 8 L 18 0 L 20 2 L 12 10 L 20 18 L 18 20 L 10 12 L 2 20 L 0 18 L 8 10 L 0 2 Z" 
                                Fill="{StaticResource PixiRed}" HorizontalAlignment="Center" VerticalAlignment="Center"
                                Visibility="{Binding Layer.MaskIsVisibleBindable, ElementName=uc, Converter={InverseBoolToVisibilityConverter}}"/>
                        </Grid>
                    </Border>
                    <StackPanel Margin="3,0,5,0">
                        <controls:EditableTextBlock
                            x:Name="editableTextBlock"
                            VerticalAlignment="Center"
                            d:Text="New Layer" FontSize="14"
                            Text="{Binding Layer.NameBindable, ElementName=uc, Mode=TwoWay}" />
                        
                        <StackPanel Orientation="Horizontal">
                            <TextBlock d:Text="100" Foreground="White" FontSize="11">
                                <TextBlock.Text>
                                    <Binding ElementName="uc" Path="Layer.OpacityBindable" Converter="{conv:MultiplyConverter}" StringFormat="N0">
                                        <Binding.ConverterParameter>
                                            <sys:Double>100.0</sys:Double>
                                        </Binding.ConverterParameter>
                                    </Binding>
                                </TextBlock.Text>
                            </TextBlock>
                            <TextBlock Foreground="White" FontSize="11">%</TextBlock>
                            <TextBlock  FontSize="11"
                                        Margin="5,0,0,0" 
                                        d:Text="Normal" 
                                        Foreground="White"
                                        Text="{Binding Layer.BlendModeBindable, ElementName=uc, Converter={conv:BlendModeToStringConverter}}"/>
                        </StackPanel>
                    </StackPanel>
                    <WrapPanel Orientation="Vertical" Margin="0,3,3,3">
                        <Image 
                            Source="/Images/Lock-alpha.png" Width="14" Height="14"
                            Visibility="{Binding Layer.LockTransparencyBindable, ElementName=uc, Converter={BoolToVisibilityConverter}}"/>
                    </WrapPanel>
                </StackPanel>
                <Grid Margin="0, 0, 0, -2.5" DragEnter="Grid_DragEnter" VerticalAlignment="Bottom" Height="10" Drop="Grid_Drop_Below"  DragLeave="Grid_DragLeave" Grid.Row="2" Grid.Column="0" AllowDrop="True"  Background="Transparent" Name="dropBelowGrid"/>
                <Grid Margin="0, 0, 0, -2.5" VerticalAlignment="Bottom" Height="10" Grid.Row="2" Grid.Column="1" Background="{Binding ElementName=dropBelowGrid, Path=Background}"/>

                <Grid Margin="0, 0, 0, -2.5" DragEnter="Grid_DragEnter" VerticalAlignment="Bottom" Height="10" Drop="Grid_Drop_Bottom" DragLeave="Grid_DragLeave" Grid.Row="2" Grid.Column="2" AllowDrop="True"  Background="Transparent"/>
            </Grid>
        </Grid>
        <Border.ContextMenu>
            <ContextMenu>
                <MenuItem ui:Translator.Key="DUPLICATE" Command="{cmds:Command PixiEditor.Layer.DuplicateSelectedLayer}"/>
                <MenuItem ui:Translator.Key="DELETE" Command="{cmds:Command PixiEditor.Layer.DeleteAllSelected}"/>
                <MenuItem ui:Translator.Key="RENAME" Click="RenameMenuItem_Click"/>
                <MenuItem 
                    IsCheckable="True" 
                    IsChecked="{Binding PlacementTarget.Tag.Layer.ClipToMemberBelowEnabledBindable, RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
                    ui:Translator.Key="CLIP_TO_BELOW"/>
                <MenuItem 
                    IsCheckable="True" 
                    IsChecked="{Binding PlacementTarget.Tag.Layer.LockTransparencyBindable, RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
                    ui:Translator.Key="LOCK_TRANSPARENCY"/>
                <Separator/>
                <MenuItem ui:Translator.Key="MOVE_UPWARDS" Command="{cmds:Command PixiEditor.Layer.MoveSelectedMemberUpwards}"/>
                <MenuItem ui:Translator.Key="MOVE_DOWNWARDS" Command="{cmds:Command PixiEditor.Layer.MoveSelectedMemberDownwards}"/>
                <Separator/>
                <MenuItem ui:Translator.Key="CREATE_MASK" Command="{cmds:Command PixiEditor.Layer.CreateMask}"/>
                <MenuItem ui:Translator.Key="DELETE_MASK" Command="{cmds:Command PixiEditor.Layer.DeleteMask}"/>
                <MenuItem 
                    IsCheckable="True" 
                    IsChecked="{Binding PlacementTarget.Tag.Layer.MaskIsVisibleBindable, RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
                    IsEnabled="{Binding PlacementTarget.Tag.Layer.HasMaskBindable, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                    ui:Translator.Key="ENABLE_MASK"/>
                <MenuItem ui:Translator.Key="APPLY_MASK" Command="{cmds:Command PixiEditor.Layer.ApplyMask}"/>
                <Separator/>
                <MenuItem ui:Translator.Key="MERGE_SELECTED" Command="{cmds:Command PixiEditor.Layer.MergeSelected}"/>
                <MenuItem ui:Translator.Key="MERGE_WITH_ABOVE" Command="{cmds:Command PixiEditor.Layer.MergeWithAbove}"/>
                <MenuItem ui:Translator.Key="MERGE_WITH_BELOW" Command="{cmds:Command PixiEditor.Layer.MergeWithBelow}"/>
            </ContextMenu>
        </Border.ContextMenu>
    </Border>
</UserControl>
