Option Strict On
Option Explicit On

Imports System
Imports System.Windows.Forms
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Internal native WinForms adapter used by the built-in top bar to perform window actions.
    ''' </summary>
    ''' <remarks>
    ''' This is implementation plumbing for MASTopBarComponent. SDK code must use the official
    ''' MASApplicationWindow.Shell/top-bar entry points instead of constructing native-window adapters.
    ''' </remarks>
    Friend NotInheritable Class TopBarNativeWindowAdapter
        Implements IDisposable
        Implements ITopBarHostActions

        Private ReadOnly _form As Form
        Private _disposed As Boolean

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

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

#Region "Internal Factory API"

        Friend Shared Function ForForm(form As Form) As TopBarNativeWindowAdapter
            Return New TopBarNativeWindowAdapter(form)
        End Function

        Friend Shared Function Create(form As Form) As TopBarNativeWindowAdapter
            Return ForForm(form)
        End Function

#End Region

        Public Sub BeginDrag(screenPt As SKPoint) Implements ITopBarHostActions.BeginDrag
            If _disposed Then Return

            Try
                _form.Capture = False
                NativeDrag()
            Catch masCaughtException1 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowNativeInterop(masCaughtException1)
            End Try
        End Sub

        Public Sub DragTo(screenPt As SKPoint) Implements ITopBarHostActions.DragTo
            If _disposed Then Return
            ' no-op: native OS drag in use
        End Sub

        Public Sub EndDrag() Implements ITopBarHostActions.EndDrag
            If _disposed Then Return
            ' no-op
        End Sub

        Friend Sub Minimize()
            If _disposed Then Return
            Try
                _form.WindowState = FormWindowState.Minimized
            Catch masCaughtException2 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowNativeInterop(masCaughtException2)
            End Try
        End Sub

        Friend Sub ToggleMaximizeRestore()
            If _disposed Then Return

            Try
                _form.WindowState =
                    If(_form.WindowState = FormWindowState.Maximized,
                       FormWindowState.Normal,
                       FormWindowState.Maximized)
            Catch masCaughtException3 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowNativeInterop(masCaughtException3)
            End Try
        End Sub

        Friend Sub CloseWindow()
            If _disposed Then Return
            Try
                _form.Close()
            Catch masCaughtException4 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowNativeInterop(masCaughtException4)
            End Try
        End Sub

        Private Sub NativeDrag()
            Const WM_NCLBUTTONDOWN As Integer = &HA1
            Const HTCAPTION As Integer = 2

            NativeMethods.ReleaseCapture()
            NativeMethods.SendMessage(_form.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
        End Sub

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

        Private NotInheritable Class NativeMethods
            <Runtime.InteropServices.DllImport("user32.dll")>
            Friend Shared Function ReleaseCapture() As Boolean
            End Function

            <Runtime.InteropServices.DllImport("user32.dll")>
            Friend Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As Integer, lParam As Integer) As IntPtr
            End Function
        End Class

    End Class

End Namespace
