Option Strict On
Option Explicit On

Imports System
Imports System.Windows.Forms

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Window-level helper for applying DWM/classic shadow behavior to a WinForms host.
    ''' This is host integration logic, but still generic and reusable across any external project.
    ''' </summary>
    Friend NotInheritable Class MASDwmShadowHost
        Implements IDisposable

        Private ReadOnly _form As Form

        Friend Property Mode As MASDwmShadow.ShadowMode = MASDwmShadow.ShadowMode.PopupSafe
        Friend Property ApplyDelayMs As Integer = 0
        Friend Property UsePulseFix As Boolean = False

        Private _attached As Boolean
        Private _disposed As Boolean

        Friend Sub New(owner As Form)
            If owner Is Nothing Then Throw New ArgumentNullException(NameOf(owner))
            _form = owner
        End Sub

        Friend ReadOnly Property Owner As Form
            Get
                Return _form
            End Get
        End Property

        Friend ReadOnly Property IsAttached As Boolean
            Get
                Return _attached AndAlso Not _disposed
            End Get
        End Property

        Friend Sub Attach()
            If _disposed OrElse _attached Then Return
            _attached = True

            AddHandler _form.HandleCreated, AddressOf OnHandleCreated
            AddHandler _form.Shown, AddressOf OnShown
            AddHandler _form.FormClosed, AddressOf OnFormClosed

            If _form.IsHandleCreated AndAlso Not _form.IsDisposed Then
                Apply()
            End If
        End Sub

        Friend Sub Detach()
            If Not _attached Then Return

            _attached = False

            Try
                RemoveHandler _form.HandleCreated, AddressOf OnHandleCreated
            Catch masCaughtException1 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException1)
            End Try
            Try
                RemoveHandler _form.Shown, AddressOf OnShown
            Catch masCaughtException2 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException2)
            End Try
            Try
                RemoveHandler _form.FormClosed, AddressOf OnFormClosed
            Catch masCaughtException3 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException3)
            End Try
        End Sub

        Friend Sub Apply()
            If _disposed Then Return
            If _form Is Nothing OrElse _form.IsDisposed Then Return
            If Not _form.IsHandleCreated Then Return

            Try
                MASDwmShadow.ApplyDelayMs = Math.Max(0, ApplyDelayMs)
                MASDwmShadow.UsePulseFix = UsePulseFix
                MASDwmShadow.Enable(_form.Handle, Mode)
            Catch masCaughtException4 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException4)
            End Try
        End Sub

        Private Sub OnHandleCreated(sender As Object, e As EventArgs)
            Apply()
        End Sub

        Private Sub OnShown(sender As Object, e As EventArgs)
            Apply()
        End Sub

        Private Sub OnFormClosed(sender As Object, e As FormClosedEventArgs)
            Dispose()
        End Sub

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

            Detach()
        End Sub

    End Class

End Namespace
