Imports System.ComponentModel
Imports System.Globalization
Imports DLLLicenseManager
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Config
Imports System.Threading

Public Class ClassInit

    Public _lizenzManager As ClassLicenseManager

    Public Sub New()
    End Sub

    <STAThread()>
    Public Sub InitLogger()
        LOGCONFIG = New LogConfig(LogConfig.PathType.CustomPath,
                                       Application.LocalUserAppDataPath & "\Log",
                                        Nothing,
                                        My.Application.Info.CompanyName,
                                        My.Application.Info.ProductName)
        LOGGER = LOGCONFIG.GetLogger("ProcessManager")

        LOGGER.Info("## ProcessManager started - {0}", Now)
    End Sub

    <STAThread()>
    Public Sub InitUserConfig()
        Try
            Dim AppdataPath = Application.UserAppDataPath
            Dim CommonAppDataPath = Application.CommonAppDataPath
            Dim StartupPath = Application.StartupPath
            If My.Settings.UseAppConfigConString = True Then
                AppdataPath = StartupPath
                CommonAppDataPath = StartupPath
            End If

            CONFIG = New ConfigManager(Of ClassConfig)(LOGCONFIG, AppdataPath, CommonAppDataPath, StartupPath)
            LOGGER.Info("Config loaded")

            If CONFIG.Config.ConnectionStringTest <> String.Empty And CONFIG.Config.TestMode = True Then
                LOGGER.Debug("Test Connection String loaded")
                CONNECTION_STRING = DecryptConnectionString(CONFIG.Config.ConnectionStringTest)
            Else
                LOGGER.Debug("Connection String loaded")
                CONNECTION_STRING = DecryptConnectionString(CONFIG.Config.ConnectionString)
            End If

            If CONFIG.Config.ConnectionStringAppServer <> String.Empty Then
                LOGGER.Debug("ConnectionStringAppServer will be used")
                CONNECTION_STRING_APP_SERVER = DecryptConnectionString(CONFIG.Config.ConnectionStringAppServer)
                CONNECTION_STRING_READ = CONNECTION_STRING_APP_SERVER
            Else
                CONNECTION_STRING_READ = CONNECTION_STRING
            End If

            'VIEWER_UNIVERSAL = CONFIG.Config.UniversalViewerPath
            'VIEWER_XCHANGE = CONFIG.Config.XChangeViewerPath
            'VIEWER_SUMATRA = CONFIG.Config.SumatraViewerPath
            'VIEWER_ZOOM_LEVEL = CONFIG.Config.PDFViewerZoomLevel
            'VIEWER_PDF = CONFIG.Config.PDFViewer
            'VIEWER_ALL = CONFIG.Config.DefaultViewer

            INDEX_DMS_ERSTELLT = CONFIG.Config.IndexDmsErstellt
            INDEX_DMS_ERSTELLT_ZEIT = CONFIG.Config.IndexDmsErstelltZeit

            USER_MANAGER_PATH = CONFIG.Config.UserManagerPath

            LOG_ERRORS_ONLY = CONFIG.Config.LogErrorsOnly
            LOGCONFIG.Debug = Not LOG_ERRORS_ONLY
            TEST_MODE = CONFIG.Config.TestMode
        Catch ex As Exception
            LOGGER.Error(ex)
        End Try


        'Settings_Load()
    End Sub

    Private Function DecryptConnectionString(EncryptedConnectionString As String) As String
        Dim oBuilder As New SqlClient.SqlConnectionStringBuilder With {
            .ConnectionString = EncryptedConnectionString
        }

        If oBuilder.ConnectionString.Contains("Password=") Then
            Dim oPlaintextPassword As String
            Dim oDecryptor As New ClassEncryption("!35452didalog=")

            Try
                oPlaintextPassword = oDecryptor.DecryptData(oBuilder.Password)
            Catch ex As Exception
                LOGGER.Error(ex)
                LOGGER.Debug("Password {0} could not be decrypted. Assuming plaintext password.")
                oPlaintextPassword = oBuilder.Password
            End Try
            Return EncryptedConnectionString.Replace(oBuilder.Password, oPlaintextPassword)
        Else
            Return EncryptedConnectionString
        End If
    End Function

    <STAThread()>
    Public Function InitDatabase()
        Dim dbResult As Boolean

        If CONNECTION_STRING <> String.Empty Then
            dbResult = ClassDatabase.Init()
        Else
            MsgBox("No Databaseconnection configured. (First Start or Appdata not accessible)" & vbNewLine & "Basic-Config will be loaded.", MsgBoxStyle.Information)
            ERROR_STATE = "NO DB-CONNECTION"
            frmKonfig.ShowDialog()
            dbResult = ClassDatabase.Init()
        End If
        If CONNECTION_STRING_APP_SERVER <> String.Empty Then
            If ClassDatabase.Init(CONNECTION_STRING_APP_SERVER) = False Then
                MsgBox("ApplicationServer can not be reached! Check Your configuration.", MsgBoxStyle.Information)
                If dbResult = True Then
                    CONNECTION_STRING_READ = CONNECTION_STRING
                End If
            End If
        End If

            If dbResult = False Then
            ERROR_STATE = "FAILED DBCONNECTION"
            MsgBox("Error in init database. (Connection failed) More information in the logfile.", MsgBoxStyle.Critical)
            Return False
        Else
            Return True
        End If
    End Function

    <STAThread()>
    Sub Refresh_Licence()
        Dim oStopWatch As New RefreshHelper.SW("Refresh_Licence")
        Try
            Me._lizenzManager = New ClassLicenseManager("#DigitalData35452!#", "")
            Dim lic As String = BASEDATA_DT_CONFIG.Rows(0).Item("LIZENZEN")
            Dim licString = Me._lizenzManager.DecodeLicenseKey(lic)
            Dim split() = licString.ToString.Split("#")

            If lic <> "" Then
                LICENSE_COUNT = split(0)
            Else
                LICENSE_COUNT = 0
            End If

            If LICENSE_VALID = False Then
                MsgBox("Your license has expired!" & vbNewLine & "Last valid date: " & split(1) & vbNewLine & "Please contact Your sysadmin", MsgBoxStyle.Exclamation)
                LICENSE_EXPIRED = True
                LICENSE_COUNT = 0
            End If
            LOGGER.Debug("license initialized....")

            LICENSE_PROFILES = split(2)
        Catch ex As Exception
            LOGGER.Error(ex)
            LOGGER.Info("Unexpected error in Refresh license: " & ex.Message, True)
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fehler bei Licensemanager:")
        End Try
        oStopWatch.done
    End Sub
    <STAThread()>
    Public Sub InitUserLogin(Optional pUSRNAME As String = "")
        Dim oStopWatch As New RefreshHelper.SW("InitUserLogin")
        Try
            If pUSRNAME <> String.Empty Then
                USER_USERNAME = pUSRNAME
            Else
                USER_USERNAME = Environment.UserName
            End If

            Try
                Dim oSQLSW As New RefreshHelper.SW("VWDD_USER_CLIENT")
                Dim oSQL = $"SELECT * FROM VWDD_USER_CLIENT WHERE UPPER(USERNAME) = UPPER('{USER_USERNAME}')"
                DT_CLIENT_USER = ClassDatabase.Return_Datatable(oSQL, "InitUserLogin1")
                oSQLSW.Done()

                If DT_CLIENT_USER.Rows.Count > 1 Then
                    frmClientLogin.ShowDialog()
                ElseIf DT_CLIENT_USER.Rows.Count = 1 Then
                    CLIENT_SELECTED = DT_CLIENT_USER.Rows(0).Item("CLIENT_ID")
                Else
                    ERROR_STATE = "NO CLIENT"
                    Dim oDT As DataTable = ClassAllgemeineFunktionen.GUI_LANGUAGE_INFO("No Client relation")
                    'Related to a client
                    '  Throw New Exception("You are not configured in the Useradministration." & vbNewLine & "Please contact the system administrator!")
                    MsgBox(oDT.Rows(0).Item("STRING1") & vbNewLine & oDT.Rows(0).Item("STRING2") & vbNewLine & vbNewLine & oSQL, MsgBoxStyle.Exclamation)
                    Exit Sub
                End If
            Catch ex As Exception
                LOGGER.Error(ex)
                LOGGER.Info("Unexpected error in checking CLIENT: " & ex.Message)
                CLIENT_SELECTED = 1
            End Try

            LOGGER.Debug("Username: " & USER_USERNAME)
            Dim oFnct As New RefreshHelper.SW("FNDD_CHECK_USER_MODULE")
            Dim sql = String.Format("SELECT * FROM [dbo].[FNDD_CHECK_USER_MODULE] ('{0}','PM',{1})", USER_USERNAME, CLIENT_SELECTED)
            Dim DT_CHECKUSER_MODULE As DataTable = ClassDatabase.Return_Datatable(sql, "InitUserLogin2")
            oFnct.Done()
            If DT_CHECKUSER_MODULE.Rows.Count = 0 Then
                LOGGER.Info("DT_CHECKUSER_MODULE.Rows.Count = 0", True)
                ERROR_STATE = "NO USER"
                MsgBox("Sorry - Something went wrong in getting Your rights." & vbNewLine & "Please contact the system administrator!", MsgBoxStyle.Exclamation)
                Exit Sub
            End If


            If DT_CHECKUSER_MODULE.Rows.Count = 1 Then
                If DT_CHECKUSER_MODULE.Rows(0).Item("USER_ID") <> 0 Then
                    USER_EXISTS = True
                Else
                    USER_EXISTS = False
                End If

                USER_ID = DT_CHECKUSER_MODULE.Rows(0).Item("USER_ID")
                USER_SURNAME = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_SURNAME")), "", DT_CHECKUSER_MODULE.Rows(0).Item("USER_SURNAME"))
                USER_PRENAME = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_PRENAME")), "", DT_CHECKUSER_MODULE.Rows(0).Item("USER_PRENAME"))
                USER_SHORTNAME = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_SHORTNAME")), "", DT_CHECKUSER_MODULE.Rows(0).Item("USER_SHORTNAME"))
                USER_EMAIL = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_EMAIL")), "", DT_CHECKUSER_MODULE.Rows(0).Item("USER_EMAIL"))
                USER_LANGUAGE = DT_CHECKUSER_MODULE.Rows(0).Item("USER_LANGUAGE")

                USER_IN_MODULE = DT_CHECKUSER_MODULE.Rows(0).Item("MODULE_ACCESS")
                USER_IS_ADMIN = DT_CHECKUSER_MODULE.Rows(0).Item("IS_ADMIN")
                USER_RIGHT_FILE_DELETE = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_RIGHT_FILE_DEL")), False, DT_CHECKUSER_MODULE.Rows(0).Item("USER_RIGHT_FILE_DEL"))
                USER_DATE_FORMAT = DT_CHECKUSER_MODULE.Rows(0).Item("USER_DATE_FORMAT")
                USERCOUNT_LOGGED_IN = DT_CHECKUSER_MODULE.Rows(0).Item("USERCOUNT_LOGGED_IN")

                ClassParamRefresh.Refresh_Params(DT_CHECKUSER_MODULE)
                FINALINDICES = New ClassFinalIndex()
                FINALINDICES.init()
                If IDB_ACTIVE = True Then
                    IDBData = New ClassIDBData()
                End If

                Try
                    USER_RIGHT_VIEW_ONLY = DT_CHECKUSER_MODULE.Rows(0).Item("USER_RIGHT2")
                Catch ex As Exception
                    USER_RIGHT_VIEW_ONLY = False
                End Try
                Try
                    USER_RIGHT3 = DT_CHECKUSER_MODULE.Rows(0).Item("USER_RIGHT3")
                Catch ex As Exception
                    USER_RIGHT3 = False
                End Try
                Try
                    USER_RIGHT4 = DT_CHECKUSER_MODULE.Rows(0).Item("USER_RIGHT4")
                Catch ex As Exception
                    USER_RIGHT4 = False
                End Try
                LOGGER.Debug("User exists....")
                'Am System anmelden
                Refresh_Licence()
                'Check_User_Exists_in_PMGroups()
                'Dim Culture = CultureInfo.CreateSpecificCulture("en-US")
                '' The following line provides localization for the application's user interface. 
                'Thread.CurrentThread.CurrentUICulture = Culture
                '' The following line provides localization for data formats. 
                'Thread.CurrentThread.CurrentCulture = Culture
                '' Set this culture as the default culture for all threads in this application.  
                '' Note: The following properties are supported in the .NET Framework 4.5+ 
                'CultureInfo.DefaultThreadCurrentCulture = Culture
                'CultureInfo.DefaultThreadCurrentUICulture = Culture


                If ClassAllgemeineFunktionen.LoginOut("LOGIN") = True Then
                    USERCOUNT_LOGGED_IN += 1
                End If
                LOGGER.Debug("Count Users logged in: " & USERCOUNT_LOGGED_IN.ToString)
                If LICENSE_COUNT < USERCOUNT_LOGGED_IN And LICENSE_EXPIRED = False Then
                    MsgBox("Die Anzahl der aktuell angemeldeten User (" & USERCOUNT_LOGGED_IN.ToString & ") überschreitet die Anzahl der aktuellen Lizenzen!" & vbNewLine & "Anzahl der Lizenzen: " & LICENSE_COUNT.ToString & vbNewLine & "Bitte setzen Sie sich mit dem Systembetreuer in Verbindung!", MsgBoxStyle.Critical, "Achtung:")
                    LOGGER.Info("Die Anzahl der aktuell angemeldeten User (" & USERCOUNT_LOGGED_IN.ToString & ") überschreitet die Anzahl der Lizenzen (" & LICENSE_COUNT & ") für Process Manager!")
                    If USER_IS_ADMIN = False Then
                        ClassAllgemeineFunktionen.LoginOut("LOGOUT")
                        ERROR_STATE = "START INCOMPLETE"
                    End If
                End If
                'Alles OK bis hierhin...nun die FolderwatchKonfig laden
                LOGGER.Debug("Init Userlogin successfull completed....")
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            LOGGER.Info("Unexpected Error in InitUserLogin: " & ex.Message, True)
            ERROR_STATE = "START INCOMPLETE"
        End Try
        oStopWatch.Done()
    End Sub
    <STAThread()>
    Public Shared Sub InitBasics()
        Try
            Dim oStopWatch As New RefreshHelper.SW("InitBasics")
            Dim oSql = String.Format("select * from TBPM_KONFIGURATION WHERE GUID = 1")
            BASEDATA_DT_CONFIG = ClassDatabase.Return_Datatable(oSql, "InitBasics1")
            Settings_LoadBasicConfig()
            oSql = "select * from TBDD_CONNECTION WHERE AKTIV = 1"
            BASEDATA_DT_TBDD_CONNECTION = ClassDatabase.Return_Datatable(oSql, "InitBasics2")


            oSql = "Select * FROM TBDD_3RD_PARTY_MODULES WHERE ACTIVE = 1"
            Dim oDT As DataTable = ClassDatabase.Return_Datatable(oSql, "InitBasics3")
            For Each oROW As DataRow In oDT.Rows
                If oROW.Item("NAME") = "GDPICTURE" Then
                    GDPICTURE_LICENSE = oROW.Item("LICENSE")
                End If
            Next
            oSql = "SELECT * FROM TBDD_GUI_LANGUAGE_PHRASE WHERE MODULE IN ('PM','All Modules')"
            BASEDATA_DT_GUI_LANGUAGE_PHRASES = ClassDatabase.Return_Datatable(oSql, "InitBasics4")

            BASEDATA_DT_PROFILES_SEARCHES_DOC = ClassDatabase.Return_Datatable("select * from TBPM_PROFILE_SEARCH where TYPE = 'DOC' AND ACTIVE = 1 ORDER BY PROFILE_ID,TAB_INDEX", "InitBasics5")
            BASEDATA_DT_PROFILE_SEARCHES_DOC = BASEDATA_DT_PROFILES_SEARCHES_DOC.Clone()

            BASEDATA_DTGRID_GROUPS = ClassDatabase.Return_Datatable($"SELECT * FROM TBPM_MAIN_VIEW_GROUPS WHERE ACTIVE = 1", "InitBasics6")
            oSql = "SELECT * FROM TBPM_CHART"
            BASEDATA_DT_CHARTS = ClassDatabase.Return_Datatable(oSql, "InitBasics7")
            oStopWatch.Done()
        Catch ex As Exception
            LOGGER.Error(ex)
            LOGGER.Info("Unexpected Error in InitBasics: " & ex.Message, True)
            ERROR_STATE = "Basics not initialized"
        End Try
    End Sub
    <STAThread()>
    Public Shared Sub InitBasics2()
        Try
            Dim oStopWatch As New RefreshHelper.SW("InitBasics2")
            Dim oSql = String.Format("SELECT * FROM VWPM_PROFILE_USER WHERE USER_ID = {0}", USER_ID)
            BASEDATA_DT_VW_PROFILE_USER = ClassDatabase.Return_Datatable(oSql, "InitBasics2_1")
            If BASEDATA_DT_VW_PROFILE_USER.Rows.Count = 0 Then
                LOGGER.Warn($"no profiles for user: '{USER_USERNAME}' configured - Check SQL [{oSql}]!", False)
            End If


            oStopWatch.Done()
        Catch ex As Exception
            LOGGER.Error(ex)
            LOGGER.Info("Unexpected Error in InitBasics2: " & ex.Message, True)
            ERROR_STATE = "Basics2 not initialized"
        End Try
    End Sub
    <STAThread()>
    Private Shared Function Settings_LoadBasicConfig()
        Try
            Dim sql As String = "select * from tbdd_Modules where SHORT_NAME = 'PM'"
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql, "Settings_LoadBasicConfig")
            If DT.Rows.Count = 1 Then
                Try
                    VERSION_DELIMITER = DT.Rows(0).Item("VERSION_DELIMITER")
                    FILE_DELIMITER = DT.Rows(0).Item("FILE_DELIMITER")
                    LICENSE_VALID = DT.Rows(0).Item("LICENSE_VALID")
                    WMSESSION_STARTSTOP_STARTUP = DT.Rows(0).Item("WMSESSION_STARTSTOP_STARTUP")
                Catch ex As Exception
                    LOGGER.Error(ex)
                    LOGGER.Info("Unexpected Error in Settings_LoadBasicConfig: " & ex.Message, True)
                End Try
            Else
                Return False
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox("Error in Settings_LoadBasicConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
        Return True
    End Function

End Class
