Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports System.Windows.Forms
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Components.DropDownMenu
Imports Nexamas.UI.Controls
Imports Nexamas.UI.FloatRuntime
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Composition
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASSplitButton

#Region "Rendering"

        Protected Overrides Sub Render(canvas As SKCanvas,
                                       ctx As MASThemeContext,
                                       pixelBounds As SKRect)
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If pixelBounds.Width <= 1.0F OrElse pixelBounds.Height <= 1.0F Then Return

            _lastContext = ctx
            _lastPixelBounds = PixelSnap.SnapRectHalf(pixelBounds)

            Dim dpi As Single = PixelSnap.SafeDpi(ctx.Dpi)
            Dim rects As MASSplitButtonSegmentRects = MASSplitButtonSegmentPolicy.ResolveSegmentRects(_lastPixelBounds, dpi, IsRightToLeft())
            If rects.Main.Width <= 1.0F OrElse rects.Menu.Width <= 1.0F Then Return

            Dim mainState As MASButtonState = MASButtonState.CreateDefault(CanActivate())
            mainState.IsHovered = _hoverMain
            mainState.IsPressed = _pressedMain OrElse _motionPressedSegment = MASSplitButtonSegment.Main
            mainState.IsFocused = HasKeyboardFocus AndAlso _keyboardSegment = MASSplitButtonSegment.Main

            Dim menuState As MASButtonState = MASButtonState.CreateDefault(CanActivate())
            menuState.IsHovered = _hoverMenu OrElse IsMenuOpen
            menuState.IsPressed = _pressedMenu OrElse _motionPressedSegment = MASSplitButtonSegment.Menu
            menuState.IsFocused = (HasKeyboardFocus AndAlso _keyboardSegment = MASSplitButtonSegment.Menu) OrElse IsMenuOpen

            _renderer.RenderSingle(
                canvas:=canvas,
                ctx:=ctx,
                rect:=rects.Main,
                dpi:=dpi,
                spec:=New MASButtonSpec(_text, MASButtonPersonality.MAS, _intent, False, False, CanActivate()),
                state:=mainState,
                contract:=_buttonRenderContract,
                drawIcon:=_drawIcon,
                drawIconEmphasis:=If(_drawIconEmphasis, _drawIcon),
                iconPlacement:=_iconPlacement)

            _renderer.RenderSingle(
                canvas:=canvas,
                ctx:=ctx,
                rect:=rects.Menu,
                dpi:=dpi,
                spec:=New MASButtonSpec(String.Empty, MASButtonPersonality.MAS, _intent, False, IsMenuOpen, CanActivate()),
                state:=menuState,
                contract:=_buttonRenderContract,
                drawIcon:=AddressOf DrawChevronIcon,
                drawIconEmphasis:=AddressOf DrawChevronIcon,
                iconPlacement:=MASButtonIconPlacement.Left)

            DrawSegmentDivider(canvas, ctx, rects, dpi)
        End Sub

        Private Sub DrawSegmentDivider(canvas As SKCanvas,
                                       ctx As MASThemeContext,
                                       rects As MASSplitButtonSegmentRects,
                                       dpi As Single)
            If canvas Is Nothing OrElse ctx Is Nothing Then Return

            Dim textTheme As IMASTextColorTheme = ctx.TextColorTheme
            Dim lineColor As SKColor = If(textTheme Is Nothing, SKColors.Gray.WithAlpha(70), textTheme.MutedText.WithAlpha(75))

            Using p As New SKPaint()
                p.IsAntialias = True
                p.Style = SKPaintStyle.Stroke
                p.StrokeWidth = Math.Max(1.0F, 1.0F * dpi)
                p.Color = lineColor
                canvas.DrawLine(rects.Menu.Left, rects.Menu.Top + (6.0F * dpi), rects.Menu.Left, rects.Menu.Bottom - (6.0F * dpi), p)
            End Using
        End Sub

        Private Sub DrawChevronIcon(canvas As SKCanvas,
                                    bounds As SKRect,
                                    dpi As Single,
                                    color As SKColor)
            If canvas Is Nothing Then Return
            If bounds.Width <= 1.0F OrElse bounds.Height <= 1.0F Then Return

            dpi = PixelSnap.SafeDpi(dpi)
            Dim cx As Single = bounds.MidX
            Dim cy As Single = bounds.MidY + (1.0F * dpi)
            Dim halfW As Single = Math.Max(4.0F * dpi, bounds.Width * 0.16F)
            Dim halfH As Single = Math.Max(2.5F * dpi, bounds.Height * 0.09F)

            Using p As New SKPaint()
                p.IsAntialias = True
                p.Style = SKPaintStyle.Stroke
                p.StrokeWidth = Math.Max(1.6F, 1.6F * dpi)
                p.StrokeCap = SKStrokeCap.Round
                p.Color = color
                canvas.DrawLine(cx - halfW, cy - halfH, cx, cy + halfH, p)
                canvas.DrawLine(cx, cy + halfH, cx + halfW, cy - halfH, p)
            End Using
        End Sub

#End Region

    End Class

End Namespace
