Imports System.ComponentModel
Imports System.IO
Imports DD_LIB_Standards
Public Class ClassInit
    Public Sub New()
    End Sub

    Public Sub InitLogger()
        ClassLogger.Init("", "")
        clsLogger.LOGFILE_PATH = ClassLogger.logDateiname
    End Sub

    Public Function InitDatabase()
        Try
            Dim dbResult As Boolean
            clsDatabase.GUI = True
            If MyConnectionString <> String.Empty Then
                dbResult = clsDatabase.Init(MyConnectionString)
                Return dbResult
            Else

                Return False

            End If

        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in InitDatabase: " & ex.Message, True)
            Return False
        End Try

    End Function
    Public Sub InitBasics()
        Try
            Dim UPDATE_ID As Integer
            If InitInstallPath() = False Then

            End If
            Dim sql = String.Format("SELECT * FROM TBDD_MODULES WHERE UPPER(NAME) = UPPER('Record-Organizer')")
            Dim KONFIG_DT As DataTable = clsDatabase.Return_Datatable(sql, False)
            If KONFIG_DT.Rows.Count = 1 Then
                MyServer_UpdatePath = KONFIG_DT.Rows(0).Item("VERSION_UPATE_PATH")
            End If
            sql = String.Format("SELECT * FROM TBDD_VERSION_UPDATE WHERE GUID = (SELECT MAX(GUID) FROM TBDD_VERSION_UPDATE WHERE UPPER(MODULE_NAME) = UPPER('{0}'))", "Record-Organizer")
            Dim DT_UPDATE As DataTable = clsDatabase.Return_Datatable(sql, False)
            If Not IsNothing(DT_UPDATE) Then
                If DT_UPDATE.Rows.Count = 1 Then
                    UPDATE_ID = DT_UPDATE.Rows(0).Item("GUID")
                    VERSION_SERVER = DT_UPDATE.Rows(0).Item("VERSION_NO")
                    FORCE_UPDATE = DT_UPDATE.Rows(0).Item("FORCE_UPD")
                    VERSIONS_FOR_FORCE_UPDATE = DT_UPDATE.Rows(0).Item("VERSION_FORCE_UPD")
                    sql = String.Format("SELECT * FROM TBDD_VERSION_ITEMS WHERE UPDATE_ID = {0} ORDER BY GUID", UPDATE_ID)
                    DT_UPDATE_ITEMS = clsDatabase.Return_Datatable(sql)
                End If
            End If

        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in InitBasics: " & ex.Message, True)
        End Try

    End Sub

    Public Shared Function InitUser(Optional _User As String = "")
        Try
            Dim sql = ""
            USER_USERNAME = Environment.UserName
            sql = String.Format("SELECT * FROM TBDD_USER WHERE (LOWER(USERNAME) = LOWER('{0}'))", USER_USERNAME)
            Dim USER_DT As DataTable = clsDatabase.Return_Datatable(sql, True)
            If USER_DT.Rows.Count = 0 Then
                ClassLogger.Add(" - User '" & USER_USERNAME & "' not listed in Useradministration!", False)
                'MsgBox("Achtung: Sie sind nicht in der Userverwaltung hinterlegt." & vbNewLine & "Bitte setzen Sie sich mit dem Systembetreuer in Verbindung!", MsgBoxStyle.Critical, "Achtung:")
                'Me.Close()
                Dim msg = String.Format("You are not listed in the Useradministration." & vbNewLine & "Please contact the admin.")
                MsgBox(msg, MsgBoxStyle.Exclamation)
                Return False
            Else
                USER_GUID = USER_DT.Rows(0).Item("GUID")
                USER_LANGUAGE = USER_DT.Rows(0).Item("LANGUAGE")
                sql = String.Format("SELECT UPDATE_PATH FROM TBDD_VERSION_USER_UPDATE_PATH WHERE USER_ID = {0} AND UPPER(MODULE_NAME) = UPPER('{1}')", USER_GUID, "Record-Organizer")
                Dim USER_UPDATE_PATH = clsDatabase.Execute_Scalar(sql)
                If Not IsNothing(USER_UPDATE_PATH) Then
                    If USER_UPDATE_PATH <> String.Empty Then
                        MyServer_UpdatePath = USER_UPDATE_PATH
                    End If
                Else
                    MyServer_UpdatePath = String.Empty
                End If
                sql = String.Format("SELECT CASE VERSION_CLIENT WHEN '' THEN '{3}' ELSE VERSION_CLIENT END AS VERSION_CLIENT FROM VWDD_LOGIN_USER_HISTORY WHERE GUID = (select MAX(GUID) from VWDD_LOGIN_USER_HISTORY where USER_ID = {0} AND VERSION_CLIENT <> '' AND UPPER(MACHINE_NAME) = UPPER('{1}') AND UPPER(MODULE) = UPPER('{2}'))", USER_GUID, Environment.MachineName, "Record-Organizer", VERSION_SERVER)
                VERSION_USER = clsDatabase.Execute_Scalar(sql)
                If IsNothing(VERSION_USER) Then
                    sql = String.Format("SELECT CASE VERSION_CLIENT WHEN '' THEN '{2}' ELSE VERSION_CLIENT END AS VERSION_CLIENT FROM VWDD_LOGIN_USER_HISTORY WHERE GUID = (select MAX(GUID) from VWDD_LOGIN_USER_HISTORY where USER_ID = {0} AND VERSION_CLIENT <> '' AND UPPER(MODULE) = UPPER('{1}'))", USER_GUID, "Record-Organizer", VERSION_SERVER)
                    VERSION_USER = clsDatabase.Execute_Scalar(sql)
                    If IsNothing(VERSION_USER) Then
                        VERSION_USER = "1.0.0.0"
                    End If
                End If
                Return True
            End If
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in InitUser_Update_Spec: " & ex.Message, True)
            Return False
        End Try

    End Function
    Dim RecExe_found As Boolean = False
    Public Function InitInstallPath()
        Try
            Dim readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Digital Data\Record Organizer", "Path", Nothing)
            If Not IsNothing(readValue) Then
                MY_INSTALL_PATH = readValue
                Return True
            Else
                MY_INSTALL_PATH = ""
                For Each s In Directory.GetLogicalDrives()
                    If RecExe_found = False Then
                        DirSearch(s)
                    End If
                Next
                If RecExe_found = False Then
                    MsgBox("Could not find the path for RecordOrganizer - Please check your installation!", MsgBoxStyle.Critical)
                    Return False
                End If
            End If

        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in InitInstallPath: " & ex.Message, True)
            Return False
        End Try
    End Function
    Sub DirSearch(ByVal sDir As String)
        Dim d As String
        Dim f As String
        Try
            For Each d In Directory.GetDirectories(sDir)
                If RecExe_found = False Then
                    For Each f In Directory.GetFiles(d, "DD-Record-Organizer.exe")
                        RecExe_found = True
                        MY_INSTALL_PATH = Path.GetDirectoryName(f)
                        Exit Sub
                    Next
                End If
                If RecExe_found = False Then
                    DirSearch(d)
                End If
            Next
        Catch excpt As System.Exception
            ClassLogger.Add("Unexpected Error in DirSearch: " & excpt.Message, True)
        End Try
    End Sub
    Public Shared Function InsertEssential_Log(REFERENCE_KEY As Integer, REFERENCE_STRING As String, COMMENT As String)
        Try
            Dim insert = String.Format("INSERT INTO TBPMO_LOG_ESSENTIALS (REFERENCE_KEY,REFERENCE_STRING,COMMENT,ADDED_WHO) VALUES ({0},'{1}','{2}','{3}')", REFERENCE_KEY, REFERENCE_STRING, COMMENT, USER_USERNAME)
            Return clsDatabase.Execute_non_Query(insert, False)
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in InsertEssential_Log: " & ex.Message, True)
            Return False
        End Try
    End Function

End Class
