Imports DigitalData.Modules.Database

Public Class ClassParamRefresh
    Public Shared Sub Refresh_Params(DT_CHECKUSER As DataTable)
        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




        If DT_CHECKUSER.Rows.Count = 1 Then
            If oLicenseoverSQL = False Then
                USERCOUNT_LOGGED_IN = DT_CHECKUSER.Rows(0).Item("USERCOUNT_LOGGED_IN")
            End If

            Try
                WORKING_MODE = DT_CHECKUSER.Rows(0).Item("WORKING_MODE")
            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
            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
                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.Debug($"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.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
                ElseIf oMode = "PM.IDBWITHWMFS" Then
                    IDB_USES_WMFILESTORE = True
                ElseIf oMode = "PM.NO_BASICCONF" Then
                    BASIC_CONF_VISIBLE = False
                    LOGGER.Debug($"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.Debug($"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.Debug($"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.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.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.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("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

                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
End Class
