Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Draws only media button content: icon and optional label.
    ''' </summary>
    Friend NotInheritable Class MASMediaButtonContentPainter

        Private Sub New()
        End Sub

        Friend Shared Sub Draw(canvas As SKCanvas,
                               ctx As MASThemeContext,
                               rect As SKRect,
                               button As MASMediaButtonModel,
                               dpi As Single,
                               palette As MASMediaButtonPalette,
                               contentOffsetY As Single)
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If button.Compact Then
                DrawCompactContent(canvas, button, rect, dpi, palette, contentOffsetY)
                Return
            End If

            DrawTileContent(canvas, ctx, button, rect, dpi, palette, contentOffsetY)
        End Sub

        Private Shared Sub DrawCompactContent(canvas As SKCanvas,
                                              button As MASMediaButtonModel,
                                              rect As SKRect,
                                              dpi As Single,
                                              palette As MASMediaButtonPalette,
                                              contentOffsetY As Single)
            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)
            Dim iconSize As Single = Math.Min(22.0F * safeDpi, rect.Height * 0.58F)
            Dim compactIconRect As New SKRect(rect.MidX - iconSize * 0.5F,
                                              rect.MidY - iconSize * 0.5F + contentOffsetY,
                                              rect.MidX + iconSize * 0.5F,
                                              rect.MidY + iconSize * 0.5F + contentOffsetY)
            If palette.IconShadow.Alpha > 0 Then
                MASMediaButtonGlyphPainter.Draw(canvas,
                                                button.GlyphKind,
                                                MASMediaButtonLayoutResolver.OffsetRect(compactIconRect, 0.0F, 0.55F * safeDpi),
                                                safeDpi,
                                                palette.IconShadow)
            End If
            MASMediaButtonGlyphPainter.Draw(canvas, button.GlyphKind, compactIconRect, safeDpi, palette.Icon)
        End Sub

        Private Shared Sub DrawTileContent(canvas As SKCanvas,
                                           ctx As MASThemeContext,
                                           button As MASMediaButtonModel,
                                           rect As SKRect,
                                           dpi As Single,
                                           palette As MASMediaButtonPalette,
                                           contentOffsetY As Single)
            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)
            Dim profile As MASMediaButtonContentProfile = MASMediaButtonLayoutResolver.ResolveContentProfile(rect, safeDpi)
            Dim offsetY As Single = contentOffsetY + profile.OpticalOffsetY

            Dim tileIconRect As New SKRect(rect.MidX - profile.IconSize * 0.5F,
                                           profile.IconTop + offsetY,
                                           rect.MidX + profile.IconSize * 0.5F,
                                           profile.IconTop + profile.IconSize + offsetY)
            If palette.IconShadow.Alpha > 0 Then
                MASMediaButtonGlyphPainter.Draw(canvas,
                                                button.GlyphKind,
                                                MASMediaButtonLayoutResolver.OffsetRect(tileIconRect, 0.0F, 0.55F * safeDpi),
                                                safeDpi,
                                                palette.IconShadow)
            End If
            MASMediaButtonGlyphPainter.Draw(canvas, button.GlyphKind, tileIconRect, safeDpi, palette.Icon)

            Dim labelRect As New SKRect(rect.Left + profile.LabelSideInset,
                                        profile.LabelTop + offsetY,
                                        rect.Right - profile.LabelSideInset,
                                        profile.LabelBottom + offsetY)
            If palette.LabelShadow.Alpha > 0 Then
                DrawTileLabel(canvas,
                              ctx,
                              button.Text,
                              MASMediaButtonLayoutResolver.OffsetRect(labelRect, 0.0F, 0.55F * safeDpi),
                              safeDpi,
                              palette.LabelShadow,
                              profile.LabelTextSize)
            End If
            DrawTileLabel(canvas, ctx, button.Text, labelRect, safeDpi, palette.Label, profile.LabelTextSize)
        End Sub

        Private Shared Sub DrawTileLabel(canvas As SKCanvas,
                                         ctx As MASThemeContext,
                                         text As String,
                                         bounds As SKRect,
                                         dpi As Single,
                                         color As SKColor,
                                         requestedTextSize As Single)
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If String.IsNullOrEmpty(text) OrElse bounds.Width <= 1.0F OrElse bounds.Height <= 1.0F Then Return

            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)
            Using p As SKPaint = ctx.Typography.CreatePaint(MASTypography.MASTextStyle.Button, color)
                If p Is Nothing Then Return
                p.IsAntialias = True
                p.SubpixelText = True
                p.LcdRenderText = True
                p.TextAlign = SKTextAlign.Center
                p.FakeBoldText = False
                p.TextSize = requestedTextSize

                Dim measured As Single = p.MeasureText(text)
                Dim maxWidth As Single = Math.Max(1.0F, bounds.Width - 1.0F * safeDpi)
                If measured > maxWidth Then
                    Dim scaled As Single = p.TextSize * (maxWidth / measured)
                    p.TextSize = Math.Max(12.2F * safeDpi, scaled)
                End If

                Dim baseline As Single = ctx.Typography.BaselineY(bounds, p)
                canvas.DrawText(text, bounds.MidX, baseline, p)
            End Using
        End Sub

    End Class

End Namespace
