Option Strict On
Option Explicit On

Imports System
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    Friend NotInheritable Class MASSurfaceMaterialPaintUtils
        Private Sub New()
        End Sub

        Friend Shared Function RoundRect(rectPx As SKRect, radiusPx As Single) As SKRoundRect
            Dim rr As New SKRoundRect()

            Dim safeRadius As Single = radiusPx
            If Single.IsNaN(safeRadius) OrElse Single.IsInfinity(safeRadius) OrElse safeRadius < 0.0F Then
                safeRadius = 0.0F
            End If

            Dim maxRadius As Single = Math.Max(0.0F, Math.Min(rectPx.Width, rectPx.Height) * 0.5F)
            If safeRadius > maxRadius Then safeRadius = maxRadius

            rr.SetRectRadii(rectPx, New SKPoint() {
                New SKPoint(safeRadius, safeRadius),
                New SKPoint(safeRadius, safeRadius),
                New SKPoint(safeRadius, safeRadius),
                New SKPoint(safeRadius, safeRadius)
            })

            Return rr
        End Function

        Friend Shared Function WithAlpha(color As SKColor, alpha As Byte) As SKColor
            Return New SKColor(color.Red, color.Green, color.Blue, alpha)
        End Function

        Friend Shared Function Mix(a As SKColor,
                                   b As SKColor,
                                   amountB As Single,
                                   Optional alpha As Byte? = Nothing) As SKColor

            If Single.IsNaN(amountB) OrElse Single.IsInfinity(amountB) Then amountB = 0.0F
            If amountB < 0.0F Then amountB = 0.0F
            If amountB > 1.0F Then amountB = 1.0F

            Dim amountA As Single = 1.0F - amountB

            Dim r As Byte = ClampByte(CInt(a.Red * amountA + b.Red * amountB))
            Dim g As Byte = ClampByte(CInt(a.Green * amountA + b.Green * amountB))
            Dim bl As Byte = ClampByte(CInt(a.Blue * amountA + b.Blue * amountB))

            Dim al As Byte
            If alpha.HasValue Then
                al = alpha.Value
            Else
                al = ClampByte(CInt(a.Alpha * amountA + b.Alpha * amountB))
            End If

            Return New SKColor(r, g, bl, al)
        End Function

        Friend Shared Function LevelFactor(level As Integer,
                                           minValue As Single,
                                           maxValue As Single) As Single

            If level < 1 Then level = 1
            If level > 6 Then level = 6

            Return minValue + ((level - 1) / 5.0F) * (maxValue - minValue)
        End Function

        Friend Shared Function SurfaceTop(profile As MASSurfaceThemeProfile) As SKColor
            If profile Is Nothing Then Return New SKColor(238, 244, 249, 245)
            Return profile.Material.Top
        End Function

        Friend Shared Function SurfaceMid(profile As MASSurfaceThemeProfile) As SKColor
            If profile Is Nothing Then Return New SKColor(225, 234, 242, 244)
            Return profile.Material.Mid
        End Function

        Friend Shared Function SurfaceBottom(profile As MASSurfaceThemeProfile) As SKColor
            If profile Is Nothing Then Return New SKColor(211, 223, 234, 242)
            Return profile.Material.Bottom
        End Function

        Friend Shared Function Accent(profile As MASSurfaceThemeProfile) As SKColor
            If profile Is Nothing Then Return New SKColor(86, 155, 226, 255)
            Return profile.Material.Accent
        End Function

        Private Shared Function ClampByte(value As Integer) As Byte
            If value < 0 Then Return 0
            If value > 255 Then Return 255
            Return CByte(value)
        End Function

    End Class

End Namespace