Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language.Utils

Public Class ClassDataASorDB
    Private _Logger As Logger
    Public Sub New(LogConfig As LogConfig)
        _Logger = LogConfig.GetLogger
    End Sub
    Public Function GetDatatable(pDB As String, pSQL As String, pAppServDT As String, pAppServFilter As String, Optional pAppServSort As String = "", Optional pForce As String = "") As DataTable
        Try
            Dim oReturnDT As DataTable = Nothing
            If My.Application.Service.IsActive = True And pForce = String.Empty Then
                Try
                    Dim oTableResult As TableResult = My.Application.Service.Client.GetDatatableByName(pAppServDT, pAppServFilter, pAppServSort)
                    oReturnDT = oTableResult.Table
                    If IsNothing(oReturnDT) Then
                        _Logger.Warn($"Datatable from ApPServData is nothing [{pAppServDT} - {pAppServFilter}] - Failover via DB")
                        Return GetDatatable(pDB, pSQL, "", "", "", "DB")
                    End If
                Catch ex As Exception
                    _Logger.Warn($"Error getting ApPServData [{pAppServDT} - {pAppServFilter}]")
                    Return GetDatatable(pDB, pSQL, "", "", "", "DB")
                End Try
            Else
                If pDB = "DD_ECM" Then
                    oReturnDT = My.DatabaseECM.GetDatatable(pSQL)
                ElseIf pDB = "IDB" Then
                    oReturnDT = My.DatabaseIDB.GetDatatable(pSQL)
                End If

            End If
            Return oReturnDT
        Catch ex As Exception
            _Logger.Error(ex)
        End Try
    End Function

    Public Function CheckModuleData() As Boolean
        Try
            Dim oSql = String.Format("SELECT * FROM [dbo].[FNDD_CHECK_USER_MODULE] ('{0}','CW',{1})", My.Application.User.UserName)
            Dim DT_CHECKUSER_MODULE As DataTable
            DT_CHECKUSER_MODULE = GetDatatable("DD_ECM", oSql, "TBDD_USER_MODULE", $"USERNAME = '{My.Application.User.UserName.ToLower}' AND MODULE_SHORT = 'CW'", "", "")

            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)
                Return False
            End If
            If DT_CHECKUSER_MODULE.Rows.Count = 1 Then
                _Logger.Info(">> Login Username: " & My.Application.User.UserName, False)
                _Logger.Info(">> Login time: " & Now.ToString, False)
                My.Application.User.UserId = DT_CHECKUSER_MODULE.Rows(0).Item("USER_ID")
                My.Application.User.Surname = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_SURNAME")), "", DT_CHECKUSER_MODULE.Rows(0).Item("USER_SURNAME"))
                My.Application.User.GivenName = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_PRENAME")), "", DT_CHECKUSER_MODULE.Rows(0).Item("USER_PRENAME"))
                My.Application.User.ShortName = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_SHORTNAME")), "", DT_CHECKUSER_MODULE.Rows(0).Item("USER_SHORTNAME"))
                My.Application.User.Email = IIf(IsDBNull(DT_CHECKUSER_MODULE.Rows(0).Item("USER_EMAIL")), "", DT_CHECKUSER_MODULE.Rows(0).Item("USER_EMAIL"))
                My.Application.User.Language = DT_CHECKUSER_MODULE.Rows(0).Item("USER_LANGUAGE")
                My.Application.User.LanguageId = DT_CHECKUSER_MODULE.Rows(0).Item("USER_LANGUAGE_ID")
                My.Application.User.DateFormat = DT_CHECKUSER_MODULE.Rows(0).Item("USER_DATE_FORMAT")

                My.Application.User.IsAdmin = DT_CHECKUSER_MODULE.Rows(0).Item("IS_ADMIN")
                ADDITIONAL_TITLE = NotNull(DT_CHECKUSER_MODULE.Rows(0).Item("ADDITIONAL_TITLE"), My.Application.Info.ProductName)

                Return True
            Else
                _Logger.Info(" - User '" & My.Application.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
            End If
        Catch ex As Exception
            _Logger.Error(ex)
            Return False
        End Try

    End Function
    Public Sub Refresh_Connections()
        Try
            Dim oSql = String.Format("SELECT * FROM TBDD_CONNECTION WHERE AKTIV = 1")
            My.Tables.DTDD_CONNECTION = GetDatatable("DD_ECM", oSql, "TBDD_CONNECTION", "", "", "")
        Catch ex As Exception
            _Logger.Error(ex)
            MsgBox("Unexpected Error in Refresh_Connections: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
End Class
