Imports System.Data.SqlClient
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Language
Imports DigitalData.Modules.Logging

Public Class GlobalState
    Private ReadOnly _LogConfig As LogConfig
    Private ReadOnly _Logger As Logger
    Private ReadOnly _MSSQL_IDB As MSSQLServer
    Private ReadOnly _MSSQL_ECM As MSSQLServer

    Public Property ObjectStores As New List(Of ObjectStore)
    Public Property Connections As New List(Of DatabaseConnection)
    Public Property ClientConfig As New Config.ClientConfiguration

    Public Property TableStore As New DataSet

    Public Sub New(LogConfig As LogConfig, MSSQL_IDB As MSSQLServer, MSSQL_ECM As MSSQLServer)
        _LogConfig = LogConfig
        _Logger = LogConfig.GetLogger()
        _MSSQL_IDB = MSSQL_IDB
        _MSSQL_ECM = MSSQL_ECM
    End Sub

    Public Function GetObjectStore(pName As String) As ObjectStore
        Return ObjectStores.
            Where(Function(store) store.Title.ToUpper = pName.ToUpper).
            FirstOrDefault()
    End Function

    Public Function GetConnection(pConnectionId As Long) As DatabaseConnection
        Return Connections.
            Where(Function(conn) conn.Id = pConnectionId).
            FirstOrDefault()
    End Function

    Public Function GetCachedTables() As List(Of String)
        Try
            Dim oTables As DataTableCollection = TableStore.Tables
            Dim oList As New List(Of String)

            For Each oTable As DataTable In oTables
                oList.Add(oTable.TableName)
            Next

            Return oList
        Catch ex As Exception
            _Logger.Error(ex)
            Return New List(Of String)
        End Try
    End Function

    Public Function GetConnectionString(pConnectionId As Long) As String
        Dim oConnection = GetConnection(pConnectionId)
        Dim oBuilder As New SqlConnectionStringBuilder With {
            .DataSource = oConnection.Server,
            .InitialCatalog = oConnection.Database,
            .UserID = oConnection.Username,
            .Password = oConnection.Password
        }

        Return MSSQLServer.DecryptConnectionString(oBuilder.ToString)
    End Function


    Public Sub LoadObjectStores()
        _Logger.Info("Loading Object Stores")
        Try
            Dim oSQL As String = "SELECT * FROM VWIDB_OBJECTSTORE"
            Dim oDatatable As DataTable = _MSSQL_IDB.GetDatatable(oSQL)

            ObjectStores.Clear()

            _Logger.Info("Found [{0}] Object Stores", oDatatable.Rows.Count)

            For Each oRow As DataRow In oDatatable.Rows
                Dim oStore As New ObjectStore() With {
                    .Id = oRow.Item("OST_ID"),
                    .IsArchive = oRow.Item("OS_IS_ARCHIVE"),
                    .Path = oRow.Item("IDB_PRAEFIX"),
                    .Title = oRow.Item("OS_TITLE")
                }
                _Logger.Info("New Object Store [{0}]", oStore.Title)
                ObjectStores.Add(oStore)
            Next
        Catch ex As Exception
            _Logger.Error(ex)
        End Try
    End Sub

    Public Sub LoadConnections()
        _Logger.Info("Loading Database Connections")
        Try
            Dim oSQL As String = "SELECT * FROM TBDD_CONNECTION"
            Dim oDatatable As DataTable = _MSSQL_ECM.GetDatatable(oSQL)

            Connections.Clear()

            _Logger.Info("Found [{0}] Connections", oDatatable.Rows.Count)

            For Each oRow As DataRow In oDatatable.Rows
                Dim oConnection As New DatabaseConnection() With {
                    .Id = oRow.ItemEx(Of Integer)("GUID"),
                    .Active = oRow.ItemEx(Of Boolean)("AKTIV"),
                    .Database = oRow.ItemEx(Of String)("DATENBANK"),
                    .Title = oRow.ItemEx(Of String)("BEZEICHNUNG"),
                    .Password = oRow.ItemEx(Of String)("PASSWORD"),
                    .Provider = oRow.ItemEx(Of String)("SQL_PROVIDER"),
                    .Server = oRow.ItemEx(Of String)("SERVER"),
                    .Username = oRow.ItemEx(Of String)("USERNAME")
                }
                _Logger.Info("New Connection [{0}]", oConnection.Title)
                Connections.Add(oConnection)
            Next
        Catch ex As Exception
            _Logger.Error(ex)
        End Try
    End Sub

    Class ObjectStore
        Public Id As Long
        Public Title As String
        Public IsArchive As Boolean
        Public Path As String
    End Class

    Class DatabaseConnection
        Public Id As Long
        Public Title As String
        Public Provider As String
        Public Server As String
        Public Database As String
        Public Username As String
        Public Password As String
        Public Active As Boolean
    End Class
End Class
