Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.FloatRuntime

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Public SDK handle for a dialog, picker, explorer, panel, or another application-owned surface.
    ''' It intentionally hides the internal FloatRuntime handle.
    ''' </summary>
    Public NotInheritable Class MASApplicationSurfaceHandle

        Private ReadOnly _handle As MASFloatHandle

        Friend Sub New(handle As MASFloatHandle)
            If handle Is Nothing Then Throw New ArgumentNullException(NameOf(handle))
            _handle = handle
        End Sub

        Friend ReadOnly Property FloatHandleCore As MASFloatHandle
            Get
                Return _handle
            End Get
        End Property

        Public ReadOnly Property Id As Long
            Get
                Return _handle.Id
            End Get
        End Property

        Public ReadOnly Property SurfaceKey As String
            Get
                Return _handle.FloatKey
            End Get
        End Property

        Public ReadOnly Property OwnerKey As String
            Get
                Return _handle.OwnerKey
            End Get
        End Property

        Public ReadOnly Property State As MASApplicationSurfaceState
            Get
                Return MASApplicationSurfaceContractMapper.ToApplicationSurfaceState(_handle.State)
            End Get
        End Property

        Public ReadOnly Property IsOpen As Boolean
            Get
                Return _handle.IsOpen
            End Get
        End Property

        Public ReadOnly Property IsClosing As Boolean
            Get
                Return _handle.IsClosing
            End Get
        End Property

        Public ReadOnly Property IsClosed As Boolean
            Get
                Return _handle.IsClosed
            End Get
        End Property

        Public Function Close(Optional reason As MASApplicationCloseReason = MASApplicationCloseReason.Programmatic) As Boolean
            Return _handle.Close(MASApplicationSurfaceContractMapper.ToFloatCloseReason(reason))
        End Function

        Friend Shared Function FromFloatHandle(handle As MASFloatHandle) As MASApplicationSurfaceHandle
            If handle Is Nothing Then Return Nothing
            Return New MASApplicationSurfaceHandle(handle)
        End Function

        Public Overrides Function ToString() As String
            Return String.Format(
                Globalization.CultureInfo.InvariantCulture,
                "MASApplicationSurfaceHandle(Id={0}, SurfaceKey={1}, OwnerKey={2}, State={3})",
                Me.Id,
                Me.SurfaceKey,
                Me.OwnerKey,
                Me.State)
        End Function

    End Class

End Namespace
