<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:ui="clr-namespace:ht.UI">

    <!-- Standard Boolean to Visibility Converter -->
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />

    <!-- CONTEXT MENU SEPARATOR TEMPLATE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <ControlTemplate x:Key="Template.ContextMenuSeparator" TargetType="Separator">
        <Rectangle
            Height="2"
            Fill="{DynamicResource Color.Separator}"
            Margin="32,4,16,4"/>
    </ControlTemplate>

    <!-- CONTEXT MENU SEPARATOR STYLE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <Style x:Key="Style.ContextMenuSeparator" TargetType="Separator">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Template" Value="{StaticResource Template.ContextMenuSeparator}"/>
    </Style>

    <!-- CONTEXT MENU HEADER TEMPLATE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <!-- The Header is used when the context menu has one or more items that open submenus -->
    <!-- It defines the visual appearance and content of both the header item and the submenu -->
    <ControlTemplate x:Key="Template.ContextMenuHeader" TargetType="MenuItem">
        <Grid>

            <!-- The Border defines the container of the header item -->
            <Border
                x:Name="Header"
                Background="{DynamicResource Color.BackgroundContextMenuItem}"
                Height="40">

                <!-- This grid defines 3 columns in which we can display an icon, a label and an arrow that indicates a nested submenu -->
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
                        <ColumnDefinition Width="*" SharedSizeGroup="Header"/>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
                    </Grid.ColumnDefinitions>

                    <!-- This displays an icon in front of the label -->
                    <!-- The icon is sourced from the MenuItem Icon property defined in the ContextMenu control -->
                    <!-- If the Icon property's value is Null then this won't be visible -->
                    <Decorator Grid.Column="0" MinWidth="32">
                        <ContentPresenter
                            x:Name="Icon"
                            Margin="0"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            ContentSource="Icon"/>
                    </Decorator>

                    <!-- This displays the header item label -->
                    <!-- The label string is sourced from the MenuItem Header property defined in the ContextMenu control -->
                    <ContentPresenter
                        x:Name="HeaderHost"
                        Grid.Column="1"
                        Margin="0"
                        ContentSource="Header"
                        VerticalAlignment="Center"/>

                    <!-- This displays the arrow icon that indicates a nested submenu -->
                    <Decorator Grid.Column="2" MinWidth="24">
                        <Path
                            x:Name="Arrow"
                            Data="{DynamicResource Icon.MenuArrow}"
                            Margin="0,0,16,0"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Stroke="{DynamicResource Color.IconFill}"/>
                    </Decorator>
                </Grid>
            </Border>

            <!-- This part defines the appearance and content of the submenu associated with the header -->
            <!-- It essentially replicates the visual style of the Context Menu template defined further down -->
            <!-- The Popup control defines the behaviour and placement of the submenu -->
            <Popup
                IsOpen="{TemplateBinding IsSubmenuOpen}"
                HorizontalOffset="-4"
                VerticalOffset="-8"
                Placement="Right"
                PopupAnimation="None"
                AllowsTransparency="True"
                HasDropShadow="True">

                <!-- The Border defines the submenu container -->
                <Border
                    Background="{DynamicResource Color.BackgroundContextMenu}"
                    BorderBrush="{DynamicResource Color.BorderContextMenu}"
                    BorderThickness="2"
                    CornerRadius="2"
                    Padding="0,6"
                    Effect="{DynamicResource Shadow.ContextMenu}">

                    <!-- The ItemsPresenter loads items in the submenu -->
                    <!-- Items are sourced from MenuItems nested under other MenuItems in the ContextMenu -->
                    <ItemsPresenter />
                </Border>
            </Popup>
        </Grid>

        <!-- This triggers a hover effect on the header item by changing its background color when highlighted -->
        <ControlTemplate.Triggers>
            <Trigger Property="IsHighlighted" Value="True">
                <Setter Property="Background" Value="{DynamicResource Color.BackgroundContextMenuItemHovered}" TargetName="Header"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- CONTEXT MENU ITEM TEMPLATE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <!-- The MenuItem is used by items in the ContextMenu that don't have a nested submenu  -->
    <!-- It defines the visual appearance and content of the item -->
    <ControlTemplate x:Key="Template.ContextMenuItem" TargetType="MenuItem">

        <!-- The Border defines the container of the item -->
        <Border
            x:Name="Header"
            Background="{DynamicResource Color.BackgroundContextMenuItem}"
            Height="40">

            <!-- This grid defines 4 columns in which we can display an icon, a label and a kebind shortcut -->
            <!-- The 4th column is used as a margin -->
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
                    <ColumnDefinition Width="*" SharedSizeGroup="Header"/>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <!-- The decorator and grid wrap the display of the MenuItem Icon and Checkmark -->
                <Decorator Grid.Column="0" MinWidth="32">
                    <Grid>

                        <!-- This displays an icon in front of the label -->
                        <!-- The icon is sourced from the MenuItem Icon property defined in the ContextMenu control -->
                        <!-- If the Icon property's value is Null then this won't be visible -->
                        <ContentPresenter
                            x:Name="Icon"
                            Margin="0"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            ContentSource="Icon"/>

                        <!-- This displays a checkmark in front of the label -->
                        <!-- The checkmark will only be visible if the MenuItem IsCheckable property is set to "True" -->
                        <!-- The checkmark takes the place of the icon. Both cannot be displayed at the same time -->
                        <Path
                            x:Name="Check"
                            Margin="0"
                            Data="{DynamicResource Icon.Checkmark}"
                            Fill="{DynamicResource Color.IconFill}"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Visibility="Hidden"/>
                    </Grid>
                </Decorator>

                <!-- This displays the menu item label -->
                <!-- The label string is sourced from the MenuItem Header property defined in the ContextMenu control -->
                <ContentPresenter
                    x:Name="HeaderHost"
                    Grid.Column="1"
                    Margin="0"
                    ContentSource="Header"
                    VerticalAlignment="Center"/>

                <!-- This displays the menu item keybind shortcut -->
                <!-- The shortcut is sourced from the MenuItem InputGestureText property defined in the ContextMenu control -->
                <!-- To add keyboard shortcuts to specific MenuItems you must define command and key bindings. -->
                <!-- The property InputgestureText only represents an informative text -->
                <TextBlock
                    x:Name="InputGestureText"
                    Grid.Column="2"
                    FontFamily="{DynamicResource Font.CoreSans}"
                    FontSize="{DynamicResource Font.Size.Default}"
                    Text="{TemplateBinding InputGestureText}"
                    Foreground="{DynamicResource Color.TextDisable}"
                    VerticalAlignment="Center"
                    Margin="12,0,0,0"/>

                <Decorator Grid.Column="3" MinWidth="24"/>
            </Grid>
        </Border>

        <ControlTemplate.Triggers>

            <!-- This trigger collapses the Icon if the MenuItem is checkable -->
            <Trigger Property="IsCheckable" Value="True">
                <Setter TargetName="Icon" Property="Visibility" Value="Collapsed"/>
            </Trigger>

            <!-- This trigger displays the Checkmark if the MenuItem is checkable or checked -->
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsCheckable" Value="True"/>
                    <Condition Property="IsChecked" Value="True"/>
                </MultiTrigger.Conditions>

                <Setter TargetName="Check" Property="Visibility" Value="Visible"/>
            </MultiTrigger>

            <!-- This triggers a hover effect on the Menu Item by changing its background color when highlighted -->
            <Trigger Property="IsHighlighted" Value="True">
                <Setter Property="Background" Value="{DynamicResource Color.BackgroundContextMenuItemHovered}" TargetName="Header"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- CONTEXT MENU ITEMS PANEL TEMPLATE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <!-- This defines the layout of the items in the ContextMenu -->
    <ItemsPanelTemplate x:Key="Template.ContextMenuItemsPanel">
        <StackPanel Grid.IsSharedSizeScope="True" KeyboardNavigation.DirectionalNavigation="Cycle"/>
    </ItemsPanelTemplate>

    <Style x:Key="Style.ContextMenuItem" TargetType="MenuItem">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="FocusVisualStyle" Value="{DynamicResource Style.Focus}"/>
        <Setter Property="ItemsPanel" Value="{StaticResource Template.ContextMenuItemsPanel}"/>

        <Style.Triggers>
            <!-- This trigger changes the text color in all MenuItems when they are disabled -->
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{DynamicResource Color.TextDisable}"/>
            </Trigger>

            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsEnabled" Value="False"/>
                    <Condition Property="Focusable" Value="False"/>
                </MultiTrigger.Conditions>

                <Setter Property="Foreground" Value="{DynamicResource Color.TextPlaceholder}"/>
            </MultiTrigger>

            <!-- These triggers allow us to target specifically the Header and Menu Items and load their templates -->
            <!-- We can also target individual properties if we need to change them for any reason -->
            <Trigger Property="Role" Value="SubmenuHeader">
                <Setter Property="FontFamily" Value="{DynamicResource Font.CoreSans}"/>
                <Setter Property="FontSize" Value="{DynamicResource Font.Size.Default}"/>
                <Setter Property="Template" Value="{StaticResource Template.ContextMenuHeader}"/>
            </Trigger>

            <Trigger Property="Role" Value="SubmenuItem">
                <Setter Property="FontFamily" Value="{DynamicResource Font.CoreSans}"/>
                <Setter Property="FontSize" Value="{DynamicResource Font.Size.Default}"/>
                <Setter Property="Template" Value="{StaticResource Template.ContextMenuItem}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <!-- CONTEXT MENU GLOBAL TEMPLATE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <ControlTemplate x:Key="Template.ContextMenuControl" TargetType="{x:Type ContextMenu}">
        <ControlTemplate.Resources>

            <!-- CONTEXT MENU ITEM STYLES -->
            <!--////////////////////////////////////////////////////////////////////-->
            <!-- This style targets all MenuItem controls in the ContextMenu -->
            <Style TargetType="MenuItem" BasedOn="{StaticResource Style.ContextMenuItem}" />
        </ControlTemplate.Resources>

        <!-- This Border defines the ContextMenu container -->
        <Border
            x:Name="ContextMenuBase"
            Background="{DynamicResource Color.BackgroundContextMenu}"
            BorderBrush="{DynamicResource Color.BorderContextMenu}"
            BorderThickness="2"
            CornerRadius="2"
            Padding="0,6"
            Effect="{DynamicResource Shadow.ContextMenu}">

            <!-- The ItemsPresenter loads items in the ContextMenu -->
            <!-- Items are sourced from top level MenuItems defined in the ContextMenu control -->
            <ItemsPresenter />
        </Border>

        <!-- This trigger changes the text color in all elements defined in the ContextMenu when the menu is disabled -->
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{DynamicResource Color.TextDisable}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- CONTEXT MENU GLOBAL STYLE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <!-- This style loads the ContextMenu template, the ItemsPanel template and defines the behavior of the Context Menu -->
    <!-- We set this style in ContextMenu controls to load *all* the above defined templates and styles -->
    <Style x:Key="Style.ContextMenu" TargetType="ContextMenu" BasedOn="{StaticResource {x:Type ItemsControl}}">
        <Style.Resources>
            <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="Separator" BasedOn="{StaticResource Style.ContextMenuSeparator}"/>
        </Style.Resources>

        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="ForceCursor" Value="True"/>
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="ItemsPanel" Value="{DynamicResource Template.ContextMenuItemsPanel}"/>
        <Setter Property="Template" Value="{DynamicResource Template.ContextMenuControl}"/>
    </Style>

    <!-- CONTEXT MENU SLOT RESOURCE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <!-- This defines the ContextMenu we want to use for Inventory and Equipment slots as a resource -->
    <ContextMenu x:Key="ContextMenu.Slot" Style="{DynamicResource Style.ContextMenu}">
        <MenuItem
            Header="Equip"
            Command="{Binding DataContext.EquipItem, ElementName=Root}"
            CommandParameter="{Binding}"
            Visibility="{Binding CanEquip, Converter={StaticResource BoolToVisConverter}}">

            <b:Interaction.Triggers>
                <b:EventTrigger EventName="Loaded">
                    <noesis:SetFocusAction />
                </b:EventTrigger>
            </b:Interaction.Triggers>

        </MenuItem>

        <MenuItem
            Header="Unequip"
            Command="{Binding DataContext.UnEquipItem, ElementName=Root}"
            CommandParameter="{Binding}"
            Visibility="{Binding CanEquip, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            Header="Use"
            Command="{Binding DataContext.UseItem, ElementName=Root}"
            CommandParameter="{Binding}"
            Visibility="{Binding CanUse, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            Header="Split In Half"
            Command="{Binding DataContext.SplitCommand, ElementName=Root}"
            CommandParameter="{Binding}"
            Visibility="{Binding CanSplit, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            Header="Split"
            Command="{Binding DataContext.ManualSplitCommand, ElementName=Root}"
            CommandParameter="{Binding}"
            Visibility="{Binding CanSplit, Converter={StaticResource BoolToVisConverter}}"/>

        <Separator Visibility="{Binding ShowContextSeparator, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            Header="Drop"
            Command="{Binding DataContext.DropCommand, ElementName=Root}"
            CommandParameter="{Binding}"
            Visibility="Visible"/>

        <Separator/>
    </ContextMenu>

    <!-- CONTEXT MENU CREATIVE ITEM SLOT RESOURCE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <!-- This defines the ContextMenu we want to use for Creative Item slots as a resource -->
    <ContextMenu x:Key="ContextMenu.SlotCreative" Style="{DynamicResource Style.ContextMenu}">
        <MenuItem
            Header="ADD TO INVENTORY"
            FontFamily="{DynamicResource Font.CoreSansBold}"
            FontSize="{DynamicResource Font.Font.Size.Medium}"
            IsEnabled="False"
            Focusable="False"/>

        <MenuItem
            Header="One Item"
            Command="{Binding DataContext.CreativeLibrary.ContextMenuCreativeSlotInventoryOneItemCommand, ElementName=Root}"
            CommandParameter="{Binding}"/>

        <MenuItem
            Header="Max Stack"
            Command="{Binding DataContext.CreativeLibrary.ContextMenuCreativeSlotInventoryMaxStackCommand, ElementName=Root}"
            CommandParameter="{Binding}"/>

        <Separator/>

        <MenuItem
            Header="ADD TO HOTBAR"
            FontFamily="{DynamicResource Font.CoreSansBold}"
            FontSize="{DynamicResource Font.Font.Size.Medium}"
            IsEnabled="False"
            Focusable="False"/>

        <MenuItem
            Header="One Item"
            Command="{Binding DataContext.CreativeLibrary.ContextMenuCreativeSlotHotbarOneItemCommand, ElementName=Root}"
            CommandParameter="{Binding}"/>

        <MenuItem
            Header="Max Stack"
            Command="{Binding DataContext.CreativeLibrary.ContextMenuCreativeSlotHotbarMaxStackCommand, ElementName=Root}"
            CommandParameter="{Binding}"/>
    </ContextMenu>

    <!-- CONTEXT MENU SOCIAL ITEM RESOURCE -->
    <!--////////////////////////////////////////////////////////////////////-->
    <!-- We need to add boolean checks here that will control the visibility of the different actions based on context -->
    <ContextMenu x:Key="ContextMenu.SocialItem" Style="{DynamicResource Style.ContextMenu}">
        <b:Interaction.Triggers>
            <b:EventTrigger EventName="Opened">
                <b:InvokeCommandAction Command="{Binding ContextMenuOpenedCommand}" CommandParameter="{Binding}"/>
            </b:EventTrigger>

            <b:EventTrigger EventName="Closed">
                <b:InvokeCommandAction Command="{Binding ContextMenuClosedCommand}" CommandParameter="{Binding}"/>
            </b:EventTrigger>
        </b:Interaction.Triggers>

        <MenuItem
            x:Name="Action.AddFriend"
            Header="Add as Friend"
            Command="{Binding AddFriendCommand}"
            CommandParameter="{Binding}"
            Visibility="{Binding ShouldShowAddFriendContextMenuItem, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            x:Name="Action.Invite"
            Header="Invite to Group"
            Command="{Binding InviteToGroupCommand}"
            CommandParameter="{Binding}"
            Visibility="{Binding ShouldShowInviteToGroupContextMenuItem, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            x:Name="Action.LeaveGroup"
            Header="Leave Group"
            Command="{Binding LeaveGroupCommand}"
            CommandParameter="{Binding}"
            Visibility="{Binding ShouldShowLeaveGroupContextMenuItem, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            x:Name="Action.JoinPlayer"
            Header="Join Player"
            Command="{Binding JoinPlayerCommand}"
            CommandParameter="{Binding}"
            Visibility="{Binding ShouldShowJoinPlayerContextMenuItem, Converter={StaticResource BoolToVisConverter}}"/>

        <Separator x:Name="Separator"
            Visibility="{Binding ShouldShowContextMenuSeparator, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            x:Name="Action.Kick"
            Header="Kick"
            Command="{Binding KickFromGroupCommand}"
            CommandParameter="{Binding}"
            Visibility="{Binding ShouldShowKickFromGroupContextMenuItem, Converter={StaticResource BoolToVisConverter}}"/>

        <MenuItem
            x:Name="Action.Unblock"
            Header="Unblock"
            Command="{Binding UnblockPlayerCommand}"
            CommandParameter="{Binding}"
            Visibility="{Binding ShouldShowUnblockContextMenuItem, Converter={StaticResource BoolToVisConverter}}">

            <b:Interaction.Triggers>
                <b:EventTrigger EventName="Click">
                    <b:ChangePropertyAction PropertyName="IsOpen" Value="True" TargetName="Social.Modal"/>
                    <b:ChangePropertyAction PropertyName="ContentTemplate" Value="{DynamicResource Content.Modal.Unblock}" TargetName="Social.Modal.Content"/>
                </b:EventTrigger>
            </b:Interaction.Triggers>
        </MenuItem>

        <MenuItem
            x:Name="Action.Unfriend"
            Header="Unfriend"
            Command="{Binding UnfriendCommand}"
            CommandParameter="{Binding}"
            Visibility="{Binding ShouldShowUnfriendContextMenuItem, Converter={StaticResource BoolToVisConverter}}">

            <b:Interaction.Triggers>
                <b:EventTrigger EventName="Click">
                    <b:ChangePropertyAction PropertyName="IsOpen" Value="True" TargetName="Social.Modal"/>
                    <b:ChangePropertyAction PropertyName="ContentTemplate" Value="{DynamicResource Content.Modal.Unfriend}" TargetName="Social.Modal.Content"/>
                </b:EventTrigger>
            </b:Interaction.Triggers>
        </MenuItem>

        <MenuItem
            x:Name="Action.Block"
            Header="Block"
            Command="{Binding BlockPlayerCommand}"
            CommandParameter="{Binding}"
            Visibility="{Binding ShouldShowBlockContextMenuItem, Converter={StaticResource BoolToVisConverter}}"/>
    </ContextMenu>
</ResourceDictionary>
