Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Base.ECM
Imports Microsoft.Win32

Public Class ClassModules
    Inherits BaseClass

    Private Const PM_REGNODE = "Process Manager"
    Private Const PM_EXENAME = "DD_ProcessManager.exe"

    Private Const CW_REGNODE = "Clipboard Watcher"
    Private Const CW_EXENAME = "DD_Clipboard_Watcher.exe"

    Private Const GLOBIX_REGNODE = "Global Indexer"
    Private Const GLOBIX_EXENAME = "Global_Indexer.exe"

    Private Const REGNODE_MANUFACTURER = "Digital Data"
    Private Const REGPATH_BASE = "SOFTWARE\\WOW6432Node"

    Private Config As SystemConfig

    Public Sub New(pLogConfig As LogConfig, pConfig As SystemConfig)
        MyBase.New(pLogConfig)
        Config = pConfig
    End Sub

    Public Function GetProductProgramPath(pProduct As Product) As String
        Dim oApplicationName As String = Nothing
        Dim oPath = GetProductPath(pProduct)

        If oPath Is Nothing Then
            Return Nothing
        End If

        Select Case pProduct
            Case Product.GlobalIndexer
                oApplicationName = GLOBIX_EXENAME

            Case Product.ClipboardWatcher
                oApplicationName = CW_EXENAME

            Case Product.ProcessManager
                oApplicationName = PM_EXENAME

        End Select

        If oApplicationName Is Nothing Then
            Return Nothing
        End If

        Return IO.Path.Combine(oPath, oApplicationName)
    End Function

    Public Function GetProductPath(pProduct As Product) As String
        Select Case pProduct
            Case Product.ProcessManager
                Return GetProductPathFor(pProduct, PM_REGNODE)

            Case Product.GlobalIndexer
                Return GetProductPathFor(pProduct, GLOBIX_REGNODE)

            Case Product.ClipboardWatcher
                Return GetProductPathFor(pProduct, CW_REGNODE)

            Case Else
                Return Nothing
        End Select
    End Function

    Private Function GetProductPathFor(pProduct As Product, pSubKey As String) As String
        Dim oPathFromConfig = GetProductPathFromConfig(pProduct)

        If oPathFromConfig IsNot Nothing Then
            Return oPathFromConfig
        Else
            Return GetProductPathFromRegistryFor(pSubKey)
        End If
    End Function

    Private Function GetProductPathFromConfig(pProduct As Product)
        Select Case pProduct
            Case Product.ProcessManager
                Return Config.ProductPaths.ProcessManagerPath

            Case Product.GlobalIndexer
                Return Config.ProductPaths.GlobalIndexerPath

            Case Product.ClipboardWatcher
                Return Config.ProductPaths.ClipboardWatcherPath

            Case Else
                Return Nothing
        End Select
    End Function

    Private Function GetProductPathFromRegistryFor(pSubKey As String) As String
        Try
            Dim oPathParts As New List(Of String) From {REGPATH_BASE, REGNODE_MANUFACTURER, pSubKey}
            Dim oRegistryPath = String.Join("\\", oPathParts)
            Dim oRoot = Registry.LocalMachine
            Dim oProduct = oRoot.OpenSubKey(oRegistryPath, RegistryKeyPermissionCheck.ReadSubTree)

            If oProduct Is Nothing Then
                Return Nothing
            End If

            Return oProduct.GetValue("Path")
        Catch ex As Exception
            Logger.Warn("Could not read [Process Manager] path from registry!")
            Logger.Error(ex)
            Return Nothing
        End Try
    End Function

End Class
