Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.FloatRuntime

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Public SDK event data delivered after an application surface closes.
    ''' </summary>
    Public NotInheritable Class MASApplicationClosedEventArgs
        Inherits EventArgs

        Public ReadOnly Property Handle As MASApplicationSurfaceHandle

        Public ReadOnly Property SurfaceKey As String

        Public ReadOnly Property OwnerKey As String

        Public ReadOnly Property Result As MASApplicationResult

        Public ReadOnly Property CloseReason As MASApplicationCloseReason

        Public ReadOnly Property WasAccepted As Boolean
            Get
                Return Me.Result.Status = MASApplicationResultStatus.Accepted
            End Get
        End Property

        Public ReadOnly Property WasCancelled As Boolean
            Get
                Return Me.Result.Status = MASApplicationResultStatus.Cancelled
            End Get
        End Property

        Public ReadOnly Property WasDismissed As Boolean
            Get
                Return Me.Result.Status = MASApplicationResultStatus.Dismissed
            End Get
        End Property

        Public ReadOnly Property Failed As Boolean
            Get
                Return Me.Result.Status = MASApplicationResultStatus.Failed
            End Get
        End Property

        Friend Sub New(handle As MASApplicationSurfaceHandle,
                       result As MASApplicationResult,
                       closeReason As MASApplicationCloseReason)
            If handle Is Nothing Then Throw New ArgumentNullException(NameOf(handle))

            Me.Handle = handle
            Me.SurfaceKey = handle.SurfaceKey
            Me.OwnerKey = handle.OwnerKey
            Me.Result = If(result, MASApplicationResult.None)
            Me.CloseReason = closeReason
        End Sub

        Friend Shared Function FromFloatEventArgs(args As MASFloatClosedEventArgs,
                                                  Optional valueConverter As Func(Of Object, Object) = Nothing) As MASApplicationClosedEventArgs
            If args Is Nothing Then Return Nothing

            Return New MASApplicationClosedEventArgs(
                handle:=MASApplicationSurfaceHandle.FromFloatHandle(args.Handle),
                result:=MASApplicationResult.FromFloatResult(args.Result, valueConverter),
                closeReason:=MASApplicationSurfaceContractMapper.ToApplicationCloseReason(args.CloseReason))
        End Function

    End Class

End Namespace
