Option Strict On
Option Explicit On

Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Friend NotInheritable Class ButtonContentPolicy

        Private Sub New()
        End Sub

        Friend Structure ButtonContentVisualResult
            Friend TextColor As SKColor
            Friend IconColor As SKColor
            Friend ContentOffsetY As Single
        End Structure

        Friend Shared Function Resolve(ctx As MASThemeContext,
                                       personality As MASButtonPersonality,
                                       intent As MASButtonIntent,
                                       state As MASInteractionState,
                                       dpi As Single) As ButtonContentVisualResult

            Dim result As ButtonContentVisualResult = CreateFallback()

            If ctx Is Nothing Then Return result

            Dim app As IMASAppTheme = ctx.Theme
            If app Is Nothing Then Return result

            Dim textTheme As MASButtonTextTheme =
                ResolveTextTheme(ctx, app, personality, intent)

            If ButtonPolicyDefaults.Has(state, MASInteractionState.Disabled) Then
                result.TextColor = ResolveVisibleColor(textTheme.DisabledText, MASAppThemeContracts.Foundation(app).TextColors.DisabledText)
                result.IconColor = ResolveVisibleColor(textTheme.DisabledIcon, MASAppThemeContracts.Foundation(app).TextColors.DisabledText)
            Else
                result.TextColor = ResolveVisibleColor(textTheme.Text, MASAppThemeContracts.Foundation(app).TextColors.PrimaryText)
                result.IconColor = ResolveVisibleColor(textTheme.Icon, MASAppThemeContracts.Foundation(app).TextColors.PrimaryText)
            End If

            result.ContentOffsetY =
                ResolveContentOffsetY(personality, state)

            Return result

        End Function

        Private Shared Function CreateFallback() As ButtonContentVisualResult
            Return New ButtonContentVisualResult With {
                .TextColor = SKColors.Transparent,
                .IconColor = SKColors.Transparent,
                .ContentOffsetY = 0.0F
            }
        End Function


        Private Shared Function ResolveVisibleColor(candidate As SKColor,
                                                    fallback As SKColor) As SKColor
            If candidate.Alpha > 0 Then Return candidate
            If fallback.Alpha > 0 Then Return fallback
            Return SKColors.Black
        End Function

        Private Shared Function ResolveTextTheme(ctx As MASThemeContext,
                                                 app As IMASAppTheme,
                                                 personality As MASButtonPersonality,
                                                 intent As MASButtonIntent) As MASButtonTextTheme

            If intent = MASButtonIntent.Danger Then
                Return New MASButtonTextTheme(
                    text:=MASAppThemeContracts.Foundation(app).TextColors.InverseText,
                    icon:=MASAppThemeContracts.Foundation(app).TextColors.InverseText,
                    disabledText:=MASAppThemeContracts.Foundation(app).TextColors.DisabledText,
                    disabledIcon:=MASAppThemeContracts.Foundation(app).TextColors.DisabledText)
            End If

            Dim btnTheme As IMASButtonTheme = ctx.ButtonTheme

            If btnTheme IsNot Nothing Then
                Return btnTheme.Text(personality, intent)
            End If

            Return New MASButtonTextTheme(
                text:=MASAppThemeContracts.Foundation(app).TextColors.PrimaryText,
                icon:=MASAppThemeContracts.Foundation(app).TextColors.PrimaryText,
                disabledText:=MASAppThemeContracts.Foundation(app).TextColors.DisabledText,
                disabledIcon:=MASAppThemeContracts.Foundation(app).TextColors.DisabledText)

        End Function

        Private Shared Function ResolveContentOffsetY(personality As MASButtonPersonality,
                                                      state As MASInteractionState) As Single

            If ButtonPolicyDefaults.Has(state, MASInteractionState.Disabled) Then
                Return 0.0F
            End If

            If ButtonPolicyDefaults.Has(state, MASInteractionState.Pressed) Then
                If personality = MASButtonPersonality.Tal Then
                    Return 0.35F
                End If

                Return 0.75F
            End If

            Return 0.0F

        End Function

    End Class

End Namespace