Option Strict On
Option Explicit On

Imports System
Imports System.Drawing
Imports DrawingColor = System.Drawing.Color
Imports System.Windows.Forms

Namespace Nexamas.UI.WinForms

    Friend Enum MASMenuButtonChromeState
        Normal = 0
        Hover = 1
        Pressed = 2
        Focused = 3
        Disabled = 4
    End Enum

    Friend NotInheritable Class MASMenuButtonChrome
        Friend Sub New(fillTop As DrawingColor,
                       fillBottom As DrawingColor,
                       border As DrawingColor,
                       wash As DrawingColor,
                       text As DrawingColor,
                       glyph As DrawingColor,
                       focusRing As DrawingColor)
            Me.FillTop = fillTop
            Me.FillBottom = fillBottom
            Me.Border = border
            Me.Wash = wash
            Me.Text = text
            Me.Glyph = glyph
            Me.FocusRing = focusRing
        End Sub

        Friend ReadOnly Property FillTop As DrawingColor
        Friend ReadOnly Property FillBottom As DrawingColor
        Friend ReadOnly Property Border As DrawingColor
        Friend ReadOnly Property Wash As DrawingColor
        Friend ReadOnly Property Text As DrawingColor
        Friend ReadOnly Property Glyph As DrawingColor
        Friend ReadOnly Property FocusRing As DrawingColor
    End Class

    ''' <summary>
    ''' Friend-owned premium alignment policy for the legacy WinForms MASMenuButton trigger.
    ''' It keeps visual state, menu-open eligibility, RTL text geometry, and color decisions out
    ''' of the public compatibility control while preserving MASApplicationWindow.Shell as the
    ''' official ownership route for RootHost and DropDownMenuPopupService.
    ''' </summary>
    Friend NotInheritable Class MASMenuButtonChromePolicy
        Private Sub New()
        End Sub

        Friend Shared Function NormalizeText(value As String) As String
            Return If(value, String.Empty)
        End Function

        Friend Shared Function ResolveState(isEnabled As Boolean,
                                            isHovering As Boolean,
                                            isPressing As Boolean,
                                            isFocused As Boolean) As MASMenuButtonChromeState
            If Not isEnabled Then Return MASMenuButtonChromeState.Disabled
            If isPressing Then Return MASMenuButtonChromeState.Pressed
            If isHovering Then Return MASMenuButtonChromeState.Hover
            If isFocused Then Return MASMenuButtonChromeState.Focused
            Return MASMenuButtonChromeState.Normal
        End Function

        Friend Shared Function CanShowMenu(isDesignMode As Boolean,
                                           isEnabled As Boolean,
                                           hasRootHost As Boolean,
                                           hasMenuBuilder As Boolean) As Boolean
            If isDesignMode Then Return False
            If Not isEnabled Then Return False
            If Not hasRootHost Then Return False
            If Not hasMenuBuilder Then Return False
            Return True
        End Function

        Friend Shared Function ShouldOpenFromKey(keyCode As Keys,
                                                 altPressed As Boolean) As Boolean
            Return keyCode = Keys.Enter OrElse
                   keyCode = Keys.Space OrElse
                   keyCode = Keys.F4 OrElse
                   keyCode = Keys.Down OrElse
                   (altPressed AndAlso keyCode = Keys.Down)
        End Function

        Friend Shared Function ShouldCloseFromKey(keyCode As Keys) As Boolean
            Return keyCode = Keys.Escape
        End Function

        Friend Shared Function ResolveChrome(state As MASMenuButtonChromeState) As MASMenuButtonChrome
            Dim baseTop As DrawingColor = DrawingColor.FromArgb(255, 250, 252, 255)
            Dim baseBottom As DrawingColor = DrawingColor.FromArgb(255, 232, 238, 248)
            Dim border As DrawingColor = DrawingColor.FromArgb(150, 135, 148, 170)
            Dim textColor As DrawingColor = DrawingColor.FromArgb(255, 34, 42, 56)
            Dim glyphColor As DrawingColor = DrawingColor.FromArgb(220, 64, 76, 96)
            Dim wash As DrawingColor = DrawingColor.FromArgb(0, 255, 255, 255)
            Dim focusRing As DrawingColor = DrawingColor.FromArgb(168, 72, 130, 220)

            Select Case state
                Case MASMenuButtonChromeState.Disabled
                    baseTop = Blend(baseTop, DrawingColor.FromArgb(245, 245, 245), 0.62F)
                    baseBottom = Blend(baseBottom, DrawingColor.FromArgb(232, 232, 232), 0.7F)
                    border = DrawingColor.FromArgb(92, border)
                    textColor = DrawingColor.FromArgb(145, textColor)
                    glyphColor = DrawingColor.FromArgb(122, glyphColor)

                Case MASMenuButtonChromeState.Pressed
                    baseTop = Blend(baseTop, DrawingColor.FromArgb(210, 222, 242), 0.36F)
                    baseBottom = Blend(baseBottom, DrawingColor.FromArgb(190, 208, 236), 0.34F)
                    border = DrawingColor.FromArgb(186, 82, 116, 176)
                    wash = DrawingColor.FromArgb(22, 70, 110, 190)

                Case MASMenuButtonChromeState.Hover, MASMenuButtonChromeState.Focused
                    baseTop = Blend(baseTop, DrawingColor.White, 0.2F)
                    baseBottom = Blend(baseBottom, DrawingColor.FromArgb(214, 228, 250), 0.22F)
                    border = DrawingColor.FromArgb(176, 92, 128, 190)
                    wash = DrawingColor.FromArgb(16, 85, 132, 210)
            End Select

            Return New MASMenuButtonChrome(baseTop, baseBottom, border, wash, textColor, glyphColor, focusRing)
        End Function

        Friend Shared Function ResolveCornerRadius(bounds As RectangleF) As Single
            Return Math.Max(5.0F, Math.Min(10.0F, bounds.Height * 0.28F))
        End Function

        Friend Shared Function ResolveGlyphWidth(bounds As Rectangle) As Integer
            If bounds.Height <= 1 Then Return 18
            Return Math.Max(18, CInt(Math.Ceiling(Math.Min(24.0F, bounds.Height * 0.45F))))
        End Function

        Friend Shared Function ResolveTextRect(bounds As Rectangle,
                                               padding As Padding,
                                               glyphWidth As Integer,
                                               rightToLeft As RightToLeft) As Rectangle
            Dim safeGlyphWidth As Integer = Math.Max(18, glyphWidth)
            Dim safeLeft As Integer = Math.Max(0, padding.Left)
            Dim safeRight As Integer = Math.Max(0, padding.Right)

            If rightToLeft = Global.System.Windows.Forms.RightToLeft.Yes Then
                Return New Rectangle(
                    bounds.Left + safeLeft + safeGlyphWidth,
                    bounds.Top,
                    Math.Max(1, bounds.Width - safeLeft - safeRight - safeGlyphWidth),
                    bounds.Height)
            End If

            Return New Rectangle(
                bounds.Left + safeLeft,
                bounds.Top,
                Math.Max(1, bounds.Width - safeLeft - safeRight - safeGlyphWidth),
                bounds.Height)
        End Function

        Friend Shared Function ResolveTextFlags(rightToLeft As RightToLeft) As TextFormatFlags
            Dim flags As TextFormatFlags = TextFormatFlags.VerticalCenter Or TextFormatFlags.EndEllipsis Or TextFormatFlags.SingleLine
            If rightToLeft = Global.System.Windows.Forms.RightToLeft.Yes Then
                flags = flags Or TextFormatFlags.RightToLeft Or TextFormatFlags.Right
            Else
                flags = flags Or TextFormatFlags.Left
            End If
            Return flags
        End Function

        Friend Shared Function ResolveChevronCenterX(bounds As Rectangle,
                                                     padding As Padding,
                                                     rightToLeft As RightToLeft) As Single
            If rightToLeft = Global.System.Windows.Forms.RightToLeft.Yes Then
                Return bounds.Left + Math.Max(12.0F, CSng(Math.Max(0, padding.Left)) + 7.0F)
            End If

            Return bounds.Right - Math.Max(12.0F, CSng(Math.Max(0, padding.Right)) + 7.0F)
        End Function

        Friend Shared Function ResolveChevronSize(bounds As Rectangle) As Single
            Return Math.Max(4.0F, Math.Min(6.0F, bounds.Height * 0.18F))
        End Function

        Private Shared Function Blend(a As DrawingColor,
                                      b As DrawingColor,
                                      amount As Single) As DrawingColor
            Dim t As Single = Math.Max(0.0F, Math.Min(1.0F, amount))
            Dim inv As Single = 1.0F - t

            Return DrawingColor.FromArgb(
                CInt(Math.Round((a.A * inv) + (b.A * t))),
                CInt(Math.Round((a.R * inv) + (b.R * t))),
                CInt(Math.Round((a.G * inv) + (b.G * t))),
                CInt(Math.Round((a.B * inv) + (b.B * t))))
        End Function
    End Class

End Namespace
