Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Controls
Imports Nexamas.UI.FloatRuntime

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Beginner-facing toast facade for one MASApplicationWindow.
    ''' It hides the float-backed toast runtime and exposes only intent-level toast operations.
    ''' </summary>
    Public NotInheritable Class MASApplicationWindowToasts
        Implements IDisposable

        Private ReadOnly _service As MASToastFloatService
        Private _disposed As Boolean

        Friend Sub New(service As MASToastFloatService)
            If service Is Nothing Then Throw New ArgumentNullException(NameOf(service))
            _service = service
        End Sub

        Public Function Show(options As MASToastOptions) As Integer
            Return ShowEx(options).ToastId
        End Function

        Friend Function ShowEx(options As MASToastOptions) As MASApplicationToastShowResult
            ThrowIfDisposed()

            Dim toastId As Integer = 0
            Dim result As MASFloatShowResult = _service.ShowWithSurfaceResult(options, toastId)

            Return New MASApplicationToastShowResult(
                toastId:=toastId,
                surface:=MASApplicationSurfaceShowResult.FromFloatShowResult(result))
        End Function

        Public Function Info(message As String,
                             Optional title As String = "") As Integer
            Return InfoEx(message, title).ToastId
        End Function

        Friend Function InfoEx(message As String,
                               Optional title As String = "") As MASApplicationToastShowResult
            ThrowIfDisposed()
            Return ShowEx(MASToastOptions.Info(message, title))
        End Function

        Public Function Success(message As String,
                                Optional title As String = "") As Integer
            Return SuccessEx(message, title).ToastId
        End Function

        Friend Function SuccessEx(message As String,
                                  Optional title As String = "") As MASApplicationToastShowResult
            ThrowIfDisposed()
            Return ShowEx(MASToastOptions.Success(message, title))
        End Function

        Public Function Warning(message As String,
                                Optional title As String = "") As Integer
            Return WarningEx(message, title).ToastId
        End Function

        Friend Function WarningEx(message As String,
                                  Optional title As String = "") As MASApplicationToastShowResult
            ThrowIfDisposed()
            Return ShowEx(MASToastOptions.Warning(message, title))
        End Function

        Public Function Failure(message As String,
                                Optional title As String = "") As Integer
            Return FailureEx(message, title).ToastId
        End Function

        Friend Function FailureEx(message As String,
                                  Optional title As String = "") As MASApplicationToastShowResult
            ThrowIfDisposed()
            Return ShowEx(MASToastOptions.Danger(message, title))
        End Function

        Public Function [Error](message As String,
                                Optional title As String = "") As Integer
            Return ErrorEx(message, title).ToastId
        End Function

        Friend Function ErrorEx(message As String,
                                Optional title As String = "") As MASApplicationToastShowResult
            ThrowIfDisposed()
            Return ShowEx(MASToastOptions.Danger(message, title))
        End Function

        Public Function Danger(message As String,
                               Optional title As String = "") As Integer
            Return Failure(message, title)
        End Function

        Friend Function DangerEx(message As String,
                                 Optional title As String = "") As MASApplicationToastShowResult
            Return FailureEx(message, title)
        End Function

        Public Function Dismiss(toastId As Integer) As Boolean
            ThrowIfDisposed()
            Return _service.Dismiss(toastId)
        End Function

        Public Sub ClearAll()
            ThrowIfDisposed()
            _service.ClearAll()
        End Sub

        Public ReadOnly Property HasAnyToast As Boolean
            Get
                ThrowIfDisposed()
                Return _service.HasAnyToast
            End Get
        End Property

        Private Sub ThrowIfDisposed()
            If _disposed Then Throw New ObjectDisposedException(NameOf(MASApplicationWindowToasts))
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
            If _disposed Then Return
            _disposed = True

            Try
                _service.Dispose()
            Catch masCaughtException1 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowBoundary(masCaughtException1)
            End Try
        End Sub

    End Class

End Namespace