Option Strict On
Option Explicit On

Imports Nexamas.UI.FloatRuntime

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Public SDK completion result for application-level surfaces.
    ''' </summary>
    Public NotInheritable Class MASApplicationResult

        Public Shared ReadOnly Property None As MASApplicationResult =
            New MASApplicationResult(MASApplicationResultStatus.None, Nothing, String.Empty)

        Public ReadOnly Property Status As MASApplicationResultStatus

        Public ReadOnly Property Value As Object

        Public ReadOnly Property ErrorMessage As String

        Friend Sub New(status As MASApplicationResultStatus, value As Object)
            Me.New(status, value, String.Empty)
        End Sub

        Friend Sub New(status As MASApplicationResultStatus,
                       value As Object,
                       errorMessage As String)
            Me.Status = status
            Me.Value = value
            Me.ErrorMessage = If(errorMessage, String.Empty)
        End Sub

        Public Shared Function Accepted(value As Object) As MASApplicationResult
            Return New MASApplicationResult(MASApplicationResultStatus.Accepted, value, String.Empty)
        End Function

        Public Shared Function Cancelled() As MASApplicationResult
            Return New MASApplicationResult(MASApplicationResultStatus.Cancelled, Nothing, String.Empty)
        End Function

        Public Shared Function Dismissed() As MASApplicationResult
            Return New MASApplicationResult(MASApplicationResultStatus.Dismissed, Nothing, String.Empty)
        End Function

        Public Shared Function Failed(errorMessage As String) As MASApplicationResult
            Return New MASApplicationResult(MASApplicationResultStatus.Failed, Nothing, errorMessage)
        End Function

        Friend Shared Function FromFloatResult(result As MASFloatResult,
                                               Optional valueConverter As Func(Of Object, Object) = Nothing) As MASApplicationResult
            If result Is Nothing Then Return MASApplicationResult.None

            Dim value As Object = result.Value
            If valueConverter IsNot Nothing AndAlso value IsNot Nothing Then
                value = valueConverter.Invoke(value)
            End If

            Return New MASApplicationResult(
                status:=MASApplicationSurfaceContractMapper.ToApplicationResultStatus(result.Status),
                value:=value,
                errorMessage:=result.ErrorMessage)
        End Function

        Friend Function ToFloatResult() As MASFloatResult
            Return New MASFloatResult(
                status:=MASApplicationSurfaceContractMapper.ToFloatResultStatus(Me.Status),
                value:=Me.Value,
                errorMessage:=Me.ErrorMessage)
        End Function

    End Class

End Namespace
