Option Strict On
Option Explicit On

Imports System
Imports System.IO
Imports System.Runtime.InteropServices

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Opens the native Windows Properties sheet for an existing file-system item.
    ''' This helper is intentionally isolated from FileExplorer view logic so Shell interop remains
    ''' behind one small boundary and callers do not depend on raw ShellExecuteEx details.
    ''' </summary>
    Friend NotInheritable Class MASWindowsShellProperties

        Private Const SEE_MASK_INVOKEIDLIST As UInteger = CUInt(&HC)
        Private Const SW_SHOW As Integer = 5

        Private Sub New()
        End Sub

        Friend Shared Function ShowProperties(path As String) As Boolean
            Dim normalizedPath As String = If(path, String.Empty).Trim()
            If normalizedPath.Length <= 0 Then Return False

            Try
                If Not File.Exists(normalizedPath) AndAlso Not Directory.Exists(normalizedPath) Then
                    Return False
                End If

                Dim info As New SHELLEXECUTEINFO() With {
                    .cbSize = Marshal.SizeOf(GetType(SHELLEXECUTEINFO)),
                    .fMask = SEE_MASK_INVOKEIDLIST,
                    .hwnd = IntPtr.Zero,
                    .lpVerb = "properties",
                    .lpFile = normalizedPath,
                    .lpParameters = Nothing,
                    .lpDirectory = Nothing,
                    .nShow = SW_SHOW,
                    .hInstApp = IntPtr.Zero,
                    .lpIDList = IntPtr.Zero,
                    .lpClass = Nothing,
                    .hkeyClass = IntPtr.Zero,
                    .dwHotKey = 0UI,
                    .hIconOrMonitor = IntPtr.Zero,
                    .hProcess = IntPtr.Zero
                }

                Return ShellExecuteEx(info)

            Catch masCaughtException1 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowNativeInterop(masCaughtException1)
                Return False
            End Try
        End Function

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
        Private Structure SHELLEXECUTEINFO
            Friend cbSize As Integer
            Friend fMask As UInteger
            Friend hwnd As IntPtr

            <MarshalAs(UnmanagedType.LPTStr)>
            Friend lpVerb As String

            <MarshalAs(UnmanagedType.LPTStr)>
            Friend lpFile As String

            <MarshalAs(UnmanagedType.LPTStr)>
            Friend lpParameters As String

            <MarshalAs(UnmanagedType.LPTStr)>
            Friend lpDirectory As String

            Friend nShow As Integer
            Friend hInstApp As IntPtr
            Friend lpIDList As IntPtr

            <MarshalAs(UnmanagedType.LPTStr)>
            Friend lpClass As String

            Friend hkeyClass As IntPtr
            Friend dwHotKey As UInteger
            Friend hIconOrMonitor As IntPtr
            Friend hProcess As IntPtr
        End Structure

        <DllImport("shell32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function ShellExecuteEx(ByRef lpExecInfo As SHELLEXECUTEINFO) As Boolean
        End Function

    End Class

End Namespace
