Imports System.IO
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Logging.LogConfig

Public Class ZUGFeRDService
    Private _logConfig As LogConfig
    Private _logger As Logger
    Private _firebird As Firebird
    Private _mssql As MSSQLServer = Nothing

    Private _threadRunner As ThreadRunner

    Protected Overrides Sub OnStart(ByVal args() As String)
        _logConfig = New LogConfig(PathType.CustomPath, Path.Combine(My.Application.Info.DirectoryPath, "Log"), Nothing, "Digital Data", "DDZUGFeRDService")
        _logConfig.Debug = True
        _logger = _logConfig.GetLogger()
        _logger.Info($"{My.Settings.SERVICE_NAME} is starting.")

        Dim oDataSource As String = My.Settings.DB_DATASOURCE
        Dim oDatabase As String = My.Settings.DB_DATABASE
        Dim oUser As String = My.Settings.DB_USER
        Dim oPassword As String = My.Settings.DB_PASSWORD
        Dim oJobInterval As Integer = My.Settings.JOB_INTERVAL
        Dim oMSSQLConnectionString As String = My.Settings.MSSQL_CONNECTIONSTRING
        Dim oMSSQLEnabled As Boolean = My.Settings.MSSQL_ENABLED
        Dim oGDPictureKey As String = My.Settings.GDPICTURE_KEY

        _firebird = New Firebird(_logConfig, oDataSource, oDatabase, oUser, oPassword)

        If oMSSQLEnabled = True Then
            _mssql = New MSSQLServer(_logConfig, oMSSQLConnectionString)

            If _mssql.DBInitialized = False Then
                _logger.Warn("MSSQL Connection could not be initialized. Disabling MSSQL.")
                _mssql = Nothing
            End If
        End If

        Try
            _threadRunner = New ThreadRunner(_logConfig, _firebird, _mssql)
            _threadRunner.Start(oJobInterval)
        Catch ex As Exception
            _logger.Error(ex)
        End Try
    End Sub

    Protected Overrides Sub OnStop()
        _logger.Info($"{My.Settings.SERVICE_NAME} is stopping.")

        _threadRunner.Stop()
    End Sub

End Class
