Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.General

    ''' <summary>
    ''' Resolves interaction fill/ring colors and strengths from theme data plus Visual interaction profiles.
    ''' It does not draw and does not mutate theme state.
    ''' </summary>
    Friend NotInheritable Class MASInteractionStateVisualResolver

        Private Sub New()
        End Sub

        Friend Shared Function ResolveFillColor(theme As IMASAppTheme,
                                                kind As CommonEnums.ControlKind,
                                                state As MASInteractionState,
                                                Optional danger As Boolean = False) As SKColor

            If theme Is Nothing Then Return SKColors.Transparent
            If MASAppThemeContracts.Foundation(theme).Interaction Is Nothing Then Return SKColors.Transparent

            Dim visualState As MASInteractionState = NormalizeVisualState(state)

            Select Case visualState

                Case MASInteractionState.Disabled
                    If MASAppThemeContracts.Foundation(theme).Interaction.DisabledFill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DisabledFill.Color

                Case MASInteractionState.Pressed
                    If danger Then
                        If MASAppThemeContracts.Foundation(theme).Interaction.DangerPressed.Fill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DangerPressed.Fill.Color
                    Else
                        If MASAppThemeContracts.Foundation(theme).Interaction.Pressed.Fill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.Pressed.Fill.Color
                    End If

                Case MASInteractionState.Selected
                    If danger Then
                        If MASAppThemeContracts.Foundation(theme).Interaction.DangerSelected.Fill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DangerSelected.Fill.Color
                    Else
                        If MASAppThemeContracts.Foundation(theme).Interaction.Selected.Fill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.Selected.Fill.Color
                    End If

                Case MASInteractionState.Focused
                    If danger Then
                        If MASAppThemeContracts.Foundation(theme).Interaction.DangerFocused.Fill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DangerFocused.Fill.Color
                    Else
                        If MASAppThemeContracts.Foundation(theme).Interaction.Focused.Fill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.Focused.Fill.Color
                    End If

                Case MASInteractionState.Hovered
                    If danger Then
                        If MASAppThemeContracts.Foundation(theme).Interaction.DangerHover.Fill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DangerHover.Fill.Color
                    Else
                        If MASAppThemeContracts.Foundation(theme).Interaction.Hover.Fill.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.Hover.Fill.Color
                    End If

            End Select

            Return SKColors.Transparent
        End Function

        Friend Shared Function ResolveRingColor(theme As IMASAppTheme,
                                                kind As CommonEnums.ControlKind,
                                                state As MASInteractionState,
                                                Optional danger As Boolean = False) As SKColor

            If theme Is Nothing Then Return SKColors.Transparent
            If MASAppThemeContracts.Foundation(theme).Interaction Is Nothing Then Return SKColors.Transparent

            Dim visualState As MASInteractionState = NormalizeVisualState(state)

            Select Case visualState

                Case MASInteractionState.Pressed
                    If danger Then
                        If MASAppThemeContracts.Foundation(theme).Interaction.DangerPressed.Ring.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DangerPressed.Ring.Color
                    Else
                        If MASAppThemeContracts.Foundation(theme).Interaction.Pressed.Ring.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.Pressed.Ring.Color
                    End If

                Case MASInteractionState.Selected
                    If danger Then
                        If MASAppThemeContracts.Foundation(theme).Interaction.DangerSelected.Ring.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DangerSelected.Ring.Color
                    Else
                        If MASAppThemeContracts.Foundation(theme).Interaction.Selected.Ring.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.Selected.Ring.Color
                    End If

                Case MASInteractionState.Focused
                    If danger Then
                        If MASAppThemeContracts.Foundation(theme).Interaction.DangerFocused.Ring.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DangerFocused.Ring.Color
                    Else
                        If MASAppThemeContracts.Foundation(theme).Interaction.Focused.Ring.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.Focused.Ring.Color
                    End If

                Case MASInteractionState.Hovered
                    If danger Then
                        If MASAppThemeContracts.Foundation(theme).Interaction.DangerHover.Ring.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.DangerHover.Ring.Color
                    Else
                        If MASAppThemeContracts.Foundation(theme).Interaction.Hover.Ring.Enabled Then Return MASAppThemeContracts.Foundation(theme).Interaction.Hover.Ring.Color
                    End If

            End Select

            Return SKColors.Transparent
        End Function

        Friend Shared Function ResolveFillStrength(kind As CommonEnums.ControlKind,
                                                   state As MASInteractionState) As Single

            Dim visualState As MASInteractionState = NormalizeVisualState(state)

            Dim p As InteractionPersonalityProfiles.InteractionProfile =
                InteractionPersonalityProfiles.ResolveProfile(kind)

            Select Case visualState

                Case MASInteractionState.Hovered
                    Return ResolveLayerStrength(p.Hover.Fill)

                Case MASInteractionState.Pressed
                    Return ResolveLayerStrength(p.Pressed.Fill)

                Case MASInteractionState.Selected
                    Return ResolveLayerStrength(p.Selected.Fill)

                Case MASInteractionState.Focused
                    Return ResolveLayerStrength(p.Focused.Fill)

                Case MASInteractionState.Disabled
                    Return ResolveLayerStrength(p.DisabledFill)

                Case Else
                    Return 0.0F

            End Select
        End Function

        Friend Shared Function ResolveRingStrength(kind As CommonEnums.ControlKind,
                                                   state As MASInteractionState) As Single

            Dim visualState As MASInteractionState = NormalizeVisualState(state)

            Dim p As InteractionPersonalityProfiles.InteractionProfile =
                InteractionPersonalityProfiles.ResolveProfile(kind)

            Select Case visualState

                Case MASInteractionState.Hovered
                    Return ResolveLayerStrength(p.Hover.Ring)

                Case MASInteractionState.Pressed
                    Return ResolveLayerStrength(p.Pressed.Ring)

                Case MASInteractionState.Selected
                    Return ResolveLayerStrength(p.Selected.Ring)

                Case MASInteractionState.Focused
                    Return ResolveLayerStrength(p.Focused.Ring)

                Case Else
                    Return 0.0F

            End Select
        End Function

        Friend Shared Function NormalizeVisualState(state As MASInteractionState) As MASInteractionState

            If Has(state, MASInteractionState.Disabled) Then Return MASInteractionState.Disabled
            If Has(state, MASInteractionState.Pressed) Then Return MASInteractionState.Pressed
            If Has(state, MASInteractionState.Selected) Then Return MASInteractionState.Selected
            If Has(state, MASInteractionState.Focused) Then Return MASInteractionState.Focused
            If Has(state, MASInteractionState.Hovered) Then Return MASInteractionState.Hovered

            Return MASInteractionState.None

        End Function

        Private Shared Function Has(value As MASInteractionState,
                                    state As MASInteractionState) As Boolean
            Return (value And state) = state
        End Function

        Private Shared Function ResolveLayerStrength(layer As InteractionPersonalityProfiles.InteractionLayerTuning) As Single
            If Not layer.Enabled Then Return 0.0F
            Return Clamp01(layer.Strength)
        End Function

        Private Shared Function Clamp01(v As Single) As Single
            If Single.IsNaN(v) OrElse Single.IsInfinity(v) Then Return 0.0F
            If v < 0.0F Then Return 0.0F
            If v > 1.0F Then Return 1.0F
            Return v
        End Function

    End Class

End Namespace