Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports DevExpress.ExpressApp.Win
Imports DevExpress.ExpressApp.Updating
Imports DevExpress.ExpressApp
Imports System.Configuration
Imports DevExpress.ExpressApp.Security
Imports Nuvolar.Module
Imports DevExpress.Persistent.BaseImpl
Imports Nuvolar.Module.Win
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Partial Public Class NuvolarWindowsFormsApplication
    Inherits WinApplication
    Implements IApplicationFactory
    Friend WithEvents SystemModule1 As DevExpress.ExpressApp.SystemModule.SystemModule
    Protected Overrides Sub ReadLastLogonParametersCore(ByVal storage As DevExpress.ExpressApp.Utils.SettingsStorage, ByVal logonObject As Object)
        Dim standardLogonParameters As AuthenticationStandardLogonParameters = TryCast(logonObject, AuthenticationStandardLogonParameters)
        If (standardLogonParameters IsNot Nothing) AndAlso String.IsNullOrEmpty(standardLogonParameters.UserName) Then
            MyBase.ReadLastLogonParametersCore(storage, logonObject)
            Try
                TryCast(logonObject, ChangeLogon).DBConnection = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnString).Values("\DBConnection")
                TryCast(logonObject, ChangeLogon).SQLServer = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnString).Values("\SQLServer")
                Select Case TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnString).Values("\SQLType")
                    Case "MSSQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.MSSQL
                    Case "PostgreSQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.PostgreSQL
                    Case "MySQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.MySQL
                End Select
            Catch
            End Try
        End If
    End Sub
    Protected Overrides Sub OnLoggingOn(ByVal args As LogonEventArgs)
        MyBase.OnLoggingOn(args)

        Dim _connectionString As String
        Dim _ChangeLogon As ChangeLogon
        Dim _plattform As String = ""

        _ChangeLogon = TryCast(args.LogonParameters, ChangeLogon)

        GLOBAL_SQLType = _ChangeLogon.SQLType
        GLOBAL_SQLServer = _ChangeLogon.SQLServer
        GLOBAL_SQLDatabase = _ChangeLogon.DBConnection


        Select Case _ChangeLogon.SQLType
            Case eSQLType.MSSQL
                Select Case _ChangeLogon.SQLServer
                    Case "192.168.1.75"
                        _connectionString = " User ID=sa; "
                        _connectionString &= String.Format(" Application Name=Nuvolar Framework ({0});", UCase(_ChangeLogon.UserName))
                        _connectionString &= " Password=M1cr0s0ft!;"
                        _connectionString &= " Pooling=false;"
                        _connectionString &= String.Format(" Data Source={0};", _ChangeLogon.SQLServer)
                        _connectionString &= " Initial Catalog=" & _ChangeLogon.DBConnection
                        _plattform = "MS SQL"
                        GLOBAL_SQLUser = "sa"
                        GLOBAL_SQLPassword = "M1cr0s0ft!"
                    Case Else
                        _connectionString = " User ID=sa; "
                        _connectionString &= String.Format(" Application Name=Nuvolar Framework ({0});", UCase(_ChangeLogon.UserName))
                        _connectionString &= " Password=pcl718;"
                        _connectionString &= " Pooling=false;"
                        _connectionString &= String.Format(" Data Source={0};", _ChangeLogon.SQLServer)
                        _connectionString &= " Initial Catalog=" & _ChangeLogon.DBConnection
                        _plattform = "MS SQL"
                        GLOBAL_SQLUser = "sa"
                        GLOBAL_SQLPassword = "pcl718"
                End Select

            Case eSQLType.MySQL
                Select Case _ChangeLogon.SQLServer
                    Case "84.1.95.74"
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "malomko10", _ChangeLogon.DBConnection)
                    Case "192.168.100.182"
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "M1cr0s0ft!12345", _ChangeLogon.DBConnection)
                    Case "81.183.209.96"
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "M1cr0s0ft!", _ChangeLogon.DBConnection)
                    Case "localhost"
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "M1cr0s0ft!", _ChangeLogon.DBConnection)
                    Case Else
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "pcl718", _ChangeLogon.DBConnection)

                End Select
                _plattform = "MySQL"
                GLOBAL_SQLUser = "root"
                GLOBAL_SQLPassword = "pcl718"
            Case eSQLType.PostgreSQL
                _connectionString = DevExpress.Xpo.DB.PostgreSqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "pcl718", _ChangeLogon.DBConnection)
                _plattform = "postgreSQL"
            Case Else
                _connectionString = " User ID=sa; "
                _connectionString &= " Password=pcl718;"
                _connectionString &= " Pooling=false;"
                _connectionString &= String.Format(" Data Source={0};", _ChangeLogon.SQLServer)
                _connectionString &= " Initial Catalog=" & _ChangeLogon.DBConnection
                GLOBAL_SQLUser = "sa"
                GLOBAL_SQLPassword = "pcl718"
        End Select

        Dim anm As System.Reflection.AssemblyName = System.Reflection.AssemblyName.GetAssemblyName(Application.StartupPath & "\Nuvolar.Module.dll")

        Me.ConnectionString = _connectionString

        GLOBAL_MainModuleVersion = anm.Version.ToString
        GLOBAL_ConnectionString = _connectionString
        GLOBAL_WINFormCaption = String.Format("Nuvolar Framework version: {0} [{1}]", Application.ProductVersion, anm.Version)
        GLOBAL_WINFormCaption += String.Format(", ({0}) Server: {1}, Database: {2}", _plattform, UCase(_ChangeLogon.SQLServer), UCase(_ChangeLogon.DBConnection))
        GLOBAL_ApplicationStartupPath = Application.StartupPath & "\"
    End Sub
    Protected Overrides Function OnLogonFailed(ByVal logonParameters As Object, ByVal e As Exception) As Boolean
        If WinChangeDatabaseHelper.SkipLogonDialog Then
            Return True
        End If
        Return MyBase.OnLogonFailed(logonParameters, e)
    End Function
    Public Shared Function CreateApplication() As NuvolarWindowsFormsApplication
        Dim winApplication As New NuvolarWindowsFormsApplication

        CType(winApplication.Security, SecurityBase).Authentication = New WinChangeDatabaseStandardAuthentication()
        Return winApplication
    End Function
    Private Function IApplicationFactory_CreateApplication() As WinApplication Implements IApplicationFactory.CreateApplication
        Return CreateApplication()
    End Function
    Protected Overrides Function GetModelAssemblyFilePath() As String
        Return Nothing
    End Function
End Class

