Imports DigitalData.Modules.Database

Public Class ClassParamRefresh
    Public Shared Sub Refresh_Params(DT_CHECKUSER As DataTable, pMode As String)
        LOGGER.Debug("Refresh_Params starting ...")
        Dim oStopwatch As New RefreshHelper.SW("Refresh_Params")
        FORCE_LAYOUT_OVERVIEW = False
        SHOW_CHARTS = True
        Dim oLicenseoverSQL As Boolean = False

        If Not IsNothing(BASEDATA_DT_TBDD_SQL_COMMANDS) Then
            If BASEDATA_DT_TBDD_SQL_COMMANDS.Rows.Count > 0 Then
                For Each oRow As DataRow In BASEDATA_DT_TBDD_SQL_COMMANDS.Rows
                    If oRow.Item("TITLE") = "LICENSE_COUNT_PM" Then
                        Dim oSQL = oRow.Item("SQL_COMMAND")
                        USERCOUNT_LOGGED_IN = DatabaseFallback.GetScalarValueECM(oSQL)
                        oLicenseoverSQL = True
                    ElseIf oRow.Item("TITLE") = "PM_PROFILES_USER" Then
                        SQL_PROFILES_USER = oRow.Item("SQL_COMMAND")
                        LOGGER.Debug($"SQL FROM SQLCommands DD: {SQL_PROFILES_USER}")
                    ElseIf oRow.Item("TITLE") = "PM_VALIDATOR_SEARCH_DEF" Then
                        SQL_VALIDATOR_SEARCHES = oRow.Item("SQL_COMMAND")
                        LOGGER.Debug($"VALIDATOR-SQL FROM SQLCommands DD: {SQL_VALIDATOR_SEARCHES}")
                    ElseIf oRow.Item("TITLE") = "TF_WORKFLOW_MESSAGE" Then
                        SQL_WF_MESSAGES = oRow.Item("SQL_COMMAND")
                        LOGGER.Debug($"WF_MESSAGES-SQL FROM SQLCommands DD: {SQL_WF_MESSAGES}")
                    End If
                Next
            End If
        End If

        LOGGER.Debug("Refresh_Params BASEDATA_DT_TBDD_SQL_COMMANDS done!")


        If DT_CHECKUSER.Rows.Count = 1 Then
            If oLicenseoverSQL = False Then
                USERCOUNT_LOGGED_IN = DT_CHECKUSER.Rows(0).Item("USERCOUNT_LOGGED_IN")
                LOGGER.Debug(String.Format("Refresh_Params USERCOUNT_LOGGED_IN {0}", USERCOUNT_LOGGED_IN.ToString))
            End If

            Try
                WORKING_MODE = DT_CHECKUSER.Rows(0).Item("WORKING_MODE")
                LOGGER.Debug(String.Format("Refresh_Params WORKING_MODE {0}", WORKING_MODE.ToString))
            Catch ex As Exception
                WORKING_MODE = ""
            End Try
            Try
                ADDITIONAL_TITLE = DT_CHECKUSER.Rows(0).Item("ADDITIONAL_TITLE")

                If ADDITIONAL_TITLE = String.Empty Then
                    ADDITIONAL_TITLE = My.Application.Info.ProductName
                End If
                LOGGER.Debug(String.Format("Refresh_Params ADDITIONAL_TITLE {0}", ADDITIONAL_TITLE.ToString))
            Catch ex As Exception
                ADDITIONAL_TITLE = My.Application.Info.ProductName
            End Try
            TITLE_NOTIFICATIONS = ADDITIONAL_TITLE
            Dim oSplitWorkMode As String() = WORKING_MODE.Split("#")

            ' Use For Each loop over words and display them.
            Dim oMode As String
            For Each oMode In oSplitWorkMode
                ProcessWorkingModeParameter(oMode, pMode)
                'LOGGER.Debug($"oWorkingMode Parameter: {oMode}")
                'If oMode = "PM.FORCE_LAYOUT_OVERVIEW" Then
                '    FORCE_LAYOUT_OVERVIEW = True
                '    LOGGER.Debug($"FORCE_LAYOUT_OVERVIEW [{FORCE_LAYOUT_OVERVIEW}]")
                'ElseIf oMode = "PM.NO_MASS_VALIDATOR" Then
                '    SHOW_MASS_VALIDATOR = False
                '    LOGGER.Info($"SHOW_MASS_VALIDATOR [{SHOW_MASS_VALIDATOR}]")
                'ElseIf oMode = "PM.NO_CHARTS" Then
                '    SHOW_CHARTS = False
                '    LOGGER.Debug($"SHOW_CHARTS [{SHOW_CHARTS}]")
                'ElseIf oMode = "PM.DEBUG_LOG" Then
                '    DEBUG = True
                '    LOGCONFIG.Debug = True
                'ElseIf oMode = "PM.LOG_HOTSPOTS" Then
                '    LOG_HOTSPOTS = True
                'ElseIf oMode.StartsWith("OPERATION_MODE_FS") Then
                '    OPERATION_MODE_FS = oMode.Replace("OPERATION_MODE_FS=", "")
                '    If OPERATION_MODE_FS = ClassConstants.OpModeFS_IDBWM Then
                '        IDB_USES_WMFILESTORE = True
                '    End If
                '    LOGGER.Info("OPERATION_MODE_FS: " + OPERATION_MODE_FS)
                'ElseIf oMode = "PM.IDBWITHWMFS" Then
                '    IDB_USES_WMFILESTORE = True
                'ElseIf oMode = "PM.NO_BASICCONF" Then
                '    BASIC_CONF_VISIBLE = False
                '    LOGGER.Info($"BASIC_CONF_VISIBLE [{BASIC_CONF_VISIBLE}]")
                'ElseIf oMode = "PM.SYS_LOCKED_MAINTENANCE" Then
                '    SYS_LOCKED_MAINTENANCE = True
                'ElseIf oMode.StartsWith("PM.IDB_CONID!") Then
                '    If CONNECTION_STRING_IDB <> "" And dbIDBInitialized = True Then
                '        IDB_ACTIVE = True
                '        Continue For
                '    Else
                '        Dim Database_IDB As MSSQLServer = Nothing
                '        Dim CON_ID = oMode.Replace("PM.IDB_CONID!", "")
                '        Dim oConString = DatabaseFallback.GetConnectionString(CON_ID)
                '        CONNECTION_STRING_IDB = oConString
                '        Database_IDB = New MSSQLServer(LOGCONFIG, CONNECTION_STRING_IDB)
                '        If Database_IDB.DBInitialized = True Then
                '            IDB_ACTIVE = True
                '            LOGGER.Info($"IDB is active - IDB-Connection [{CON_ID}]!")
                '            DatabaseFallback.InitializeIDB(Database_IDB)
                '        Else
                '            IDB_ACTIVE = False
                '        End If
                '    End If
                'ElseIf oMode = "PM.EXCEL_OVERVIEW" Then
                '    EXCEL_OVERVIEW = True
                '    LOGGER.Info($"EXCEL_OVERVIEW [{EXCEL_OVERVIEW}]")
                'ElseIf oMode = "PM.MONITORING" Then
                '    MONITORING_ACTIVE = True
                '    LOGGER.Debug($"MONITORING_ACTIVE [{MONITORING_ACTIVE}]")
                'ElseIf oMode = "PM.GHOSTMODE" Then
                '    GHOSTMODE_ACTIVE = True
                '    LOGGER.Info($"GHOSTMODE_ACTIVE [{GHOSTMODE_ACTIVE}]")
                'ElseIf oMode.StartsWith("PM.COLORSCHEME") Then
                '    Dim oColorScheme = oMode.Replace("PM.COLORSCHEME=", "")
                '    RIBBON_COLOR_SCHEME = oColorScheme.ToUpper
                '    LOGGER.Debug($"RIBBON_COLOR_SCHEME [{RIBBON_COLOR_SCHEME}]")
                'ElseIf oMode.StartsWith("PM.NO_DETAIL_PROFILES") Then
                '    NO_DETAIL_PROFILES = True
                '    LOGGER.Debug($"NO_DETAIL_PROFILES [{NO_DETAIL_PROFILES}]")
                'ElseIf oMode = "PM.NO_POPUP" Then
                '    POPUP_REMINDER_ACTIVE = False
                '    LOGGER.Debug($"POPUP_REMINDER_ACTIVE [{POPUP_REMINDER_ACTIVE}]")
                'ElseIf oMode.StartsWith("PM.POPUP_TIMELAPS_OVERRIDE") Then
                '    Dim oTimeLapse = oMode.Replace("PM.POPUP_TIMELAPS_OVERRIDE=", "")
                '    LOGGER.Info($"CENTRAL POPUP_TIMELAPS_OVERRIDE [{oTimeLapse}]")
                '    Try
                '        CONFIG.Config.ReminderTimer = oTimeLapse
                '    Catch ex As Exception
                '    End Try

                'ElseIf oMode.StartsWith("PM.CONV_IDENTIFICATION") Then
                '    Dim oIdent = oMode.Replace("PM.CONV_IDENTIFICATION=", "")
                '    CONV_IDENTIFICATION = oIdent
                'ElseIf oMode.StartsWith("PM.INACTIVITY_DURATION") Then
                '    Dim oInactivityDuration = oMode.Replace("PM.INACTIVITY_DURATION=", "")
                '    Try
                '        INACTIVITY_DURATION = CInt(oInactivityDuration)
                '        LOGGER.Debug($"InactivityDuration [{INACTIVITY_DURATION}]")
                '    Catch ex As Exception
                '        LOGGER.Warn($"INACTIVITY_DURATION = 0 - {ex.Message}")
                '        INACTIVITY_DURATION = 0
                '    End Try
                'ElseIf oMode.StartsWith("PM.COL_LAST_EDITED") Then
                '    Dim oLEDITEDCOL = oMode.Replace("PM.COL_LAST_EDITED=", "")
                '    Try
                '        LAST_EDITED_COLUMN = oLEDITEDCOL
                '        LOGGER.Debug($"LAST_EDITED_COLUMN: {oLEDITEDCOL}")
                '    Catch ex As Exception
                '        LAST_EDITED_COLUMN = ""

                '    End Try
                'ElseIf oMode.StartsWith("PM.COL_LAST_ADDED") Then
                '    Dim oLEDITEDCOL = oMode.Replace("PM.COL_LAST_ADDED=", "")
                '    Try
                '        LAST_ADDED_COLUMN = oLEDITEDCOL
                '        LOGGER.Debug($"LAST_ADDED_COLUMN: {oLEDITEDCOL}")
                '    Catch ex As Exception
                '        LAST_ADDED_COLUMN = ""

                '    End Try
                'ElseIf oMode.StartsWith("PM.MON_COL_CHANGED_WHEN") Then
                '    Dim oLEDITEDCOL = oMode.Replace("PM.MON_COL_CHANGED_WHEN=", "")
                '    Try
                '        MON_EDITED_COLUMN = oLEDITEDCOL
                '        LOGGER.Debug($"MON_EDITED_COLUMN: {oLEDITEDCOL}")
                '    Catch ex As Exception

                '    End Try
                'ElseIf oMode.StartsWith("PM.MON_COL_ADDED_WHEN") Then
                '    Dim oLEDITEDCOL = oMode.Replace("PM.MON_COL_ADDED_WHEN=", "")
                '    Try
                '        MON_ADDED_COLUMN = oLEDITEDCOL
                '        LOGGER.Debug($"MON_COL_ADDED_WHEN: {oLEDITEDCOL}")
                '    Catch ex As Exception

                '    End Try
                'ElseIf oMode.StartsWith("PM.USE_APPSERVER") Then
                '    Dim oUSE_APPSERVER = oMode.Replace("PM.USE_APPSERVER=", "")
                '    Try
                '        USE_APPSERVER = CBool(oUSE_APPSERVER)
                '    Catch ex As Exception
                '        USE_APPSERVER = False
                '    End Try
                'ElseIf oMode.StartsWith("PM.COPYWM2TEMP") Then
                '    Dim oCOPYWM2TEMP = oMode.Replace("PM.COPYWM2TEMP=", "")
                '    Try
                '        COPY_WMFILE_2TEMP = CBool(oCOPYWM2TEMP)
                '    Catch ex As Exception
                '        COPY_WMFILE_2TEMP = False
                '    End Try
                'ElseIf oMode.StartsWith("PM.MAP_SHARE_DRIVE") Then
                '    Dim oMAP_SHARE_DRIVE = oMode.Replace("PM.MAP_SHARE_DRIVE=", "")
                '    Try
                '        MAP_SHARE_DRIVE = oMAP_SHARE_DRIVE
                '    Catch ex As Exception
                '        oMAP_SHARE_DRIVE = String.Empty
                '    End Try
                'ElseIf oMode.StartsWith("PM.MAP_BLACKLIST") Then
                '    Dim oMAP_BLACKLIST = oMode.Replace("PM.MAP_BLACKLIST=", "")
                '    Try
                '        MAP_BLACKLIST = oMAP_BLACKLIST
                '    Catch ex As Exception
                '        MAP_BLACKLIST = String.Empty
                '    End Try
                'ElseIf oMode.StartsWith("PM.SEARCH1") Then
                '    Dim oSearch1 = oMode.Replace("PM.SEARCH1=", "")
                '    Try
                '        SEARCH1 = oSearch1
                '    Catch ex As Exception
                '        SEARCH1 = ""
                '    End Try
                'ElseIf oMode.StartsWith("PM.SEARCH2") Then
                '    Dim oSearch2 = oMode.Replace("PM.SEARCH2=", "")
                '    Try
                '        SEARCH2 = oSearch2
                '    Catch ex As Exception
                '        SEARCH2 = ""
                '    End Try
                'ElseIf oMode.StartsWith("PM.TRAFFICLIGHT_ICON") Then
                '    Dim oParam = oMode.Replace("PM.TRAFFICLIGHT_ICON=", "")
                '    Try
                '        TL_ICON = CBool(oParam)
                '    Catch ex As Exception
                '        TL_ICON = False
                '    End Try
                'ElseIf oMode.StartsWith("PM.TITLE_NOTIFICATIONS") Then
                '    Dim oParam = oMode.Replace("PM.TITLE_NOTIFICATIONS=", "")
                '    Try
                '        TITLE_NOTIFICATIONS = oParam
                '    Catch ex As Exception
                '        TITLE_NOTIFICATIONS = ""
                '    End Try
                'ElseIf oMode.StartsWith("TF.InheritanceMsgAmount") And pMode = "Load" Then
                '    Dim oParam = oMode.Replace("TF.InheritanceMsgAmount=", "")
                '    Try
                '        InheritanceMsgAmount = oParam
                '    Catch ex As Exception

                '    End Try
                'ElseIf oMode.StartsWith("TF.InheritanceCalcReset") And pMode = "Load" Then
                '    Dim oParam = oMode.Replace("TF.InheritanceCalcReset=", "")
                '    Try
                '        If CBool(oParam) = True Then
                '            LOGGER.Info("Inheritance_Counts werden  auf 0 zurückgesetzt bzw gelöscht")
                '            Dim oDELETESQL = "DELETE FROM TBDD_USER_KEY_VALUE_PAIR 
                '             WHERE FK_USER_ID = " & USER_ID & " And [FK_MODULE_ID] = '" & USER_MODULE_ID & "' AND KEY_NAME LIKE 'INHERITANCE_CONFIRM_%'"
                '            DatabaseECM.ExecuteNonQuery(oDELETESQL)

                '            UserInheritance_ConfirmationByColumn = Nothing
                '            CONFIG.Save()
                '        End If
                '    Catch ex As Exception

                '    End Try
                'ElseIf oMode.StartsWith("PM.START_CW") Then
                '    Dim oAfterReplace = oMode.Replace("PM.START_CW=", "")
                '    Try
                '        START_CW = True
                '        LOGGER.Debug("PM.START_CW = True")
                '        Dim oSplit As String() = oAfterReplace.Split("~")
                '        START_CW_CAPTION = oSplit(0)
                '        Try
                '            START_CW_LOC_VARIANT = oSplit(1)
                '        Catch ex As Exception
                '            START_CW_LOC_VARIANT = "-1"
                '            LOGGER.Warn($"Could not read START_CW_LOCVARIANT : {ex.Message}")
                '        End Try

                '    Catch ex As Exception
                '        LOGGER.Warn($"Could not read START_CW Param: {ex.Message}")
                '        START_CW = False
                '    End Try
                'ElseIf oMode.StartsWith("PM.RibbonPageCustTitle") Then
                '    Dim oAfterReplace = oMode.Replace("PM.RibbonPageCustTitle=", "")
                '    RibbonPageCustTitle = oAfterReplace
                'ElseIf oMode.StartsWith("PM.RibbonPageCustItm1") Then
                '    Dim oAfterReplace = oMode.Replace("PM.RibbonPageCustItm1=", "")
                '    RibbonPageCustItm1 = oAfterReplace
                'ElseIf oMode.StartsWith("PM.ButtonExport2FolderCaption") Then
                '    Dim oAfterReplace = oMode.Replace("PM.ButtonExport2FolderCaption=", "")
                '    ButtonExport2Folder_Caption = oAfterReplace
                'ElseIf oMode.StartsWith("PM.ButtonExport2Folder_Mode") Then
                '    Dim oAfterReplace = oMode.Replace("PM.ButtonExport2Folder_Mode=", "")
                '    ButtonExport2Folder_Mode = oAfterReplace
                'ElseIf oMode.StartsWith("PM.ButtonExport2Folder_RootFolder") Then
                '    Dim oAfterReplace = oMode.Replace("PM.ButtonExport2Folder_RootFolder=", "")
                '    ButtonExport2Folder_RootFolder = oAfterReplace
                'ElseIf oMode = "MAINFORM_ALLOW_LOGFILES" Then
                '    MAINFORM_CREATE_LOG_FILES = True
                'Else
                '    LOGGER.Info($"Wrong oMode: {oMode}")
                'End If
            Next
            'If IDB_ACTIVE Then
            '    If CONNECTION_STRING_APP_SERVER <> String.Empty Then
            '        If USE_APPSERVER = True Then
            '            CONNECTION_STRING_IDB_READ = CONNECTION_STRING_APP_SERVER
            '        Else
            '            CONNECTION_STRING_IDB_READ = CONNECTION_STRING_IDB
            '        End If

            '    Else
            '        CONNECTION_STRING_IDB_READ = CONNECTION_STRING_IDB
            '    End If
            '    CONNECTION_STRING_IDB_WRITE = CONNECTION_STRING_IDB
            'End If
        End If
        oStopwatch.Done()
    End Sub
    Private Shared Sub ProcessWorkingModeParameter(oMode As String, pMode As String)
        LOGGER.Debug($"Processing Working Mode Parameter: {oMode}")

        Select Case True
            Case oMode = "PM.FORCE_LAYOUT_OVERVIEW"
                SetBooleanParameter(FORCE_LAYOUT_OVERVIEW, True, "FORCE_LAYOUT_OVERVIEW")

            Case oMode = "PM.NO_MASS_VALIDATOR"
                SetBooleanParameter(SHOW_MASS_VALIDATOR, False, "SHOW_MASS_VALIDATOR", LogLevel.Info)

            Case oMode = "PM.NO_CHARTS"
                SetBooleanParameter(SHOW_CHARTS, False, "SHOW_CHARTS")

            Case oMode = "PM.DEBUG_LOG"
                DEBUG = True
                LOGCONFIG.Debug = True
                LOGGER.Debug($"DEBUG_LOG = True")

            Case oMode = "PM.LOG_HOTSPOTS"
                SetBooleanParameter(LOG_HOTSPOTS, True, "LOG_HOTSPOTS")

            Case oMode.StartsWith("OPERATION_MODE_FS")
                OPERATION_MODE_FS = ExtractParameterValue(oMode, "OPERATION_MODE_FS")
                If OPERATION_MODE_FS = ClassConstants.OpModeFS_IDBWM Then
                    IDB_USES_WMFILESTORE = True
                End If
                LOGGER.Info($"OPERATION_MODE_FS = {OPERATION_MODE_FS}")

            Case oMode = "PM.IDBWITHWMFS"
                SetBooleanParameter(IDB_USES_WMFILESTORE, True, "IDB_USES_WMFILESTORE", LogLevel.Info)

            Case oMode = "PM.NO_BASICCONF"
                SetBooleanParameter(BASIC_CONF_VISIBLE, False, "BASIC_CONF_VISIBLE", LogLevel.Info)

            Case oMode = "PM.SYS_LOCKED_MAINTENANCE"
                SetBooleanParameter(SYS_LOCKED_MAINTENANCE, True, "SYS_LOCKED_MAINTENANCE")

            Case oMode.StartsWith("PM.IDB_CONID!")
                ProcessIDBConnection(oMode)

            Case oMode = "PM.EXCEL_OVERVIEW"
                SetBooleanParameter(EXCEL_OVERVIEW, True, "EXCEL_OVERVIEW", LogLevel.Info)

            Case oMode = "PM.MONITORING"
                SetBooleanParameter(MONITORING_ACTIVE, True, "MONITORING_ACTIVE")

            Case oMode = "PM.GHOSTMODE"
                SetBooleanParameter(GHOSTMODE_ACTIVE, True, "GHOSTMODE_ACTIVE", LogLevel.Info)

            Case oMode.StartsWith("PM.COLORSCHEME")
                RIBBON_COLOR_SCHEME = ExtractParameterValue(oMode, "PM.COLORSCHEME").ToUpper
                LOGGER.Debug($"RIBBON_COLOR_SCHEME = {RIBBON_COLOR_SCHEME}")

            Case oMode.StartsWith("PM.NO_DETAIL_PROFILES")
                SetBooleanParameter(NO_DETAIL_PROFILES, True, "NO_DETAIL_PROFILES")

            Case oMode = "PM.NO_POPUP"
                SetBooleanParameter(POPUP_REMINDER_ACTIVE, False, "POPUP_REMINDER_ACTIVE")

            Case oMode.StartsWith("PM.POPUP_TIMELAPS_OVERRIDE")
                ProcessPopupTimeLapse(oMode)

            Case oMode.StartsWith("PM.CONV_IDENTIFICATION")
                CONV_IDENTIFICATION = ExtractParameterValue(oMode, "PM.CONV_IDENTIFICATION")
                LOGGER.Debug($"CONV_IDENTIFICATION = {CONV_IDENTIFICATION}")

            Case oMode.StartsWith("PM.INACTIVITY_DURATION")
                ProcessIntegerParameter(oMode, "PM.INACTIVITY_DURATION", INACTIVITY_DURATION, "InactivityDuration")

            Case oMode.StartsWith("PM.COL_LAST_EDITED")
                LAST_EDITED_COLUMN = ExtractParameterValue(oMode, "PM.COL_LAST_EDITED")
                LOGGER.Debug($"LAST_EDITED_COLUMN = {LAST_EDITED_COLUMN}")

            Case oMode.StartsWith("PM.COL_LAST_ADDED")
                LAST_ADDED_COLUMN = ExtractParameterValue(oMode, "PM.COL_LAST_ADDED")
                LOGGER.Debug($"LAST_ADDED_COLUMN = {LAST_ADDED_COLUMN}")

            Case oMode.StartsWith("PM.MON_COL_CHANGED_WHEN")
                MON_EDITED_COLUMN = ExtractParameterValue(oMode, "PM.MON_COL_CHANGED_WHEN")
                LOGGER.Debug($"MON_EDITED_COLUMN = {MON_EDITED_COLUMN}")

            Case oMode.StartsWith("PM.MON_COL_ADDED_WHEN")
                MON_ADDED_COLUMN = ExtractParameterValue(oMode, "PM.MON_COL_ADDED_WHEN")
                LOGGER.Debug($"MON_ADDED_COLUMN = {MON_ADDED_COLUMN}")

            Case oMode.StartsWith("PM.USE_APPSERVER")
                ProcessBooleanParameter(oMode, "PM.USE_APPSERVER", USE_APPSERVER, "USE_APPSERVER")

            Case oMode.StartsWith("PM.COPYWM2TEMP")
                ProcessBooleanParameter(oMode, "PM.COPYWM2TEMP", COPY_WMFILE_2TEMP, "COPY_WMFILE_2TEMP")

            Case oMode.StartsWith("PM.MAP_SHARE_DRIVE")
                MAP_SHARE_DRIVE = ExtractParameterValue(oMode, "PM.MAP_SHARE_DRIVE")
                LOGGER.Debug($"MAP_SHARE_DRIVE = {MAP_SHARE_DRIVE}")

            Case oMode.StartsWith("PM.MAP_BLACKLIST")
                MAP_BLACKLIST = ExtractParameterValue(oMode, "PM.MAP_BLACKLIST")
                LOGGER.Debug($"MAP_BLACKLIST = {MAP_BLACKLIST}")

            Case oMode.StartsWith("PM.SEARCH1")
                SEARCH1 = ExtractParameterValue(oMode, "PM.SEARCH1")
                LOGGER.Debug($"SEARCH1 = {SEARCH1}")

            Case oMode.StartsWith("PM.SEARCH2")
                SEARCH2 = ExtractParameterValue(oMode, "PM.SEARCH2")
                LOGGER.Debug($"SEARCH2 = {SEARCH2}")

            Case oMode.StartsWith("PM.TRAFFICLIGHT_ICON")
                ProcessBooleanParameter(oMode, "PM.TRAFFICLIGHT_ICON", TL_ICON, "TL_ICON")

            Case oMode.StartsWith("PM.TITLE_NOTIFICATIONS")
                TITLE_NOTIFICATIONS = ExtractParameterValue(oMode, "PM.TITLE_NOTIFICATIONS")
                LOGGER.Debug($"TITLE_NOTIFICATIONS = {TITLE_NOTIFICATIONS}")

            Case oMode.StartsWith("TF.InheritanceMsgAmount") AndAlso pMode = "Load"
                InheritanceMsgAmount = ExtractParameterValue(oMode, "TF.InheritanceMsgAmount")
                LOGGER.Debug($"InheritanceMsgAmount = {InheritanceMsgAmount}")

            Case oMode.StartsWith("TF.InheritanceCalcReset") AndAlso pMode = "Load"
                ProcessInheritanceReset(oMode)

            Case oMode.StartsWith("PM.START_CW")
                ProcessStartCW(oMode)

            Case oMode.StartsWith("PM.RibbonPageCustTitle")
                RibbonPageCustTitle = ExtractParameterValue(oMode, "PM.RibbonPageCustTitle")
                LOGGER.Debug($"RibbonPageCustTitle = {RibbonPageCustTitle}")

            Case oMode.StartsWith("PM.RibbonPageCustItm1")
                RibbonPageCustItm1 = ExtractParameterValue(oMode, "PM.RibbonPageCustItm1")
                LOGGER.Debug($"RibbonPageCustItm1 = {RibbonPageCustItm1}")

            Case oMode.StartsWith("PM.ButtonExport2FolderCaption")
                ButtonExport2Folder_Caption = ExtractParameterValue(oMode, "PM.ButtonExport2FolderCaption")
                LOGGER.Debug($"ButtonExport2FolderCaption = {ButtonExport2Folder_Caption}")

            Case oMode.StartsWith("PM.ButtonExport2Folder_Mode")
                ButtonExport2Folder_Mode = ExtractParameterValue(oMode, "PM.ButtonExport2Folder_Mode")
                LOGGER.Debug($"ButtonExport2Folder_Mode = {ButtonExport2Folder_Mode}")

            Case oMode.StartsWith("PM.ButtonExport2Folder_RootFolder")
                ButtonExport2Folder_RootFolder = ExtractParameterValue(oMode, "PM.ButtonExport2Folder_RootFolder")
                LOGGER.Debug($"ButtonExport2Folder_RootFolder = {ButtonExport2Folder_RootFolder}")

            Case oMode = "MAINFORM_ALLOW_LOGFILES"
                SetBooleanParameter(MAINFORM_CREATE_LOG_FILES, True, "MAINFORM_CREATE_LOG_FILES")

            Case Else
                LOGGER.Info($"Unknown Working Mode Parameter: {oMode}")
        End Select
    End Sub
    Private Shared Function ExtractParameterValue(parameterString As String, prefix As String) As String
        Return parameterString.Replace($"{prefix}=", "")
    End Function

    Private Shared Sub SetBooleanParameter(ByRef target As Boolean, value As Boolean, paramName As String, Optional logLevel As LogLevel = LogLevel.Debug)
        target = value
        LogParameterValue(paramName, value.ToString(), logLevel)
    End Sub

    Private Shared Sub ProcessBooleanParameter(oMode As String, prefix As String, ByRef target As Boolean, paramName As String)
        Dim value = ExtractParameterValue(oMode, prefix)
        Try
            target = CBool(value)
            LOGGER.Debug($"{paramName} = {target}")
        Catch ex As Exception
            target = False
            LOGGER.Warn($"Failed to parse {paramName}: {ex.Message}")
        End Try
    End Sub

    Private Shared Sub ProcessIntegerParameter(oMode As String, prefix As String, ByRef target As Integer, paramName As String)
        Dim value = ExtractParameterValue(oMode, prefix)
        Try
            target = CInt(value)
            LOGGER.Debug($"{paramName} = {target}")
        Catch ex As Exception
            LOGGER.Warn($"Failed to parse {paramName}: {ex.Message}. Setting to 0")
            target = 0
        End Try
    End Sub

    Private Shared Sub ProcessIDBConnection(oMode As String)
        Dim Database_IDB As MSSQLServer = Nothing
        Dim CON_ID = oMode.Replace("PM.IDB_CONID!", "")
        Dim oConString = DatabaseFallback.GetConnectionString(CON_ID)
        CONNECTION_STRING_IDB = oConString
        Database_IDB = New MSSQLServer(LOGCONFIG, CONNECTION_STRING_IDB)

        If Database_IDB.DBInitialized = True Then
            IDB_ACTIVE = True
            LOGGER.Info($"IDB is active - IDB-Connection [{CON_ID}]")
            DatabaseFallback.InitializeIDB(Database_IDB)
        Else
            IDB_ACTIVE = False
            LOGGER.Warn($"Failed to initialize IDB with Connection [{CON_ID}]")
        End If
        If CONNECTION_STRING_IDB <> "" AndAlso Database_IDB.DBInitialized = True Then
            IDB_ACTIVE = True
            LOGGER.Debug("IDB already initialized and active")
            Return
        End If
    End Sub

    Private Shared Sub ProcessPopupTimeLapse(oMode As String)
        Dim oTimeLapse = ExtractParameterValue(oMode, "PM.POPUP_TIMELAPS_OVERRIDE")
        LOGGER.Info($"POPUP_TIMELAPS_OVERRIDE = {oTimeLapse}")
        Try
            CONFIG.Config.ReminderTimer = oTimeLapse
        Catch ex As Exception
            LOGGER.Warn($"Failed to set ReminderTimer: {ex.Message}")
        End Try
    End Sub

    Private Shared Sub ProcessInheritanceReset(oMode As String)
        Dim oParam = ExtractParameterValue(oMode, "TF.InheritanceCalcReset")
        Try
            If CBool(oParam) = True Then
                LOGGER.Info("Inheritance_Counts werden auf 0 zurückgesetzt bzw gelöscht")
                Dim oDELETESQL = "DELETE FROM TBDD_USER_KEY_VALUE_PAIR 
                             WHERE FK_USER_ID = " & USER_ID & " And [FK_MODULE_ID] = '" & USER_MODULE_ID & "' AND KEY_NAME LIKE 'INHERITANCE_CONFIRM_%'"
                DatabaseECM.ExecuteNonQuery(oDELETESQL)
                UserInheritance_ConfirmationByColumn = Nothing
                CONFIG.Save()
            End If
        Catch ex As Exception
            LOGGER.Warn($"Failed to process InheritanceCalcReset: {ex.Message}")
        End Try
    End Sub

    Private Shared Sub ProcessStartCW(oMode As String)
        Dim oAfterReplace = ExtractParameterValue(oMode, "PM.START_CW")
        Try
            START_CW = True
            LOGGER.Debug("PM.START_CW = True")
            Dim oSplit As String() = oAfterReplace.Split("~")
            START_CW_CAPTION = oSplit(0)
            Try
                START_CW_LOC_VARIANT = oSplit(1)
                LOGGER.Debug($"START_CW_LOC_VARIANT = {START_CW_LOC_VARIANT}")
            Catch ex As Exception
                START_CW_LOC_VARIANT = "-1"
                LOGGER.Warn($"Could not read START_CW_LOC_VARIANT: {ex.Message}")
            End Try
        Catch ex As Exception
            LOGGER.Warn($"Could not read START_CW Param: {ex.Message}")
            START_CW = False
        End Try
    End Sub

    Private Shared Sub LogParameterValue(paramName As String, value As String, logLevel As LogLevel)
        Select Case logLevel
            Case LogLevel.Info
                LOGGER.Info($"{paramName} = {value}")
            Case Else
                LOGGER.Debug($"{paramName} = {value}")
        End Select
    End Sub

    Private Enum LogLevel
        Debug
        Info
    End Enum
End Class
