Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports DevExpress.ExpressApp.Web
Imports Nuvolar.Module.Web
Imports System.Web
Imports DevExpress.Xpo.DB.Helpers
Imports DevExpress.ExpressApp
Imports Nuvolar.Module
Imports System.Web.Security
Imports DevExpress.Xpo.DB
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Security

Partial Public Class NuvolarAspNetApplication
    Inherits WebApplication
    Protected Overrides Sub ReadSecuredLogonParameters()
        MyBase.ReadSecuredLogonParameters() ' the "UserName" is restored in the base method.
    End Sub
    Private canReadSecuredLogonParameters_Renamed As Boolean = True
    Protected Overrides Function CanReadSecuredLogonParameters() As Boolean
        If (Not canReadSecuredLogonParameters_Renamed) Then
            Return False
        End If
        Return MyBase.CanReadSecuredLogonParameters()
    End Function
    Protected Overrides Function OnLogonFailed(ByVal logonParameters As Object, ByVal e As Exception) As Boolean
        If CanReadSecuredLogonParameters() Then
            FormsAuthentication.SignOut()
            canReadSecuredLogonParameters_Renamed = False
            Try
                Start()
            Finally
                canReadSecuredLogonParameters_Renamed = True
            End Try
            Return True
        Else
            Return MyBase.OnLogonFailed(logonParameters, e)
        End If
    End Function
    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.SettingsStorageOnCookies).LoadOption("", "DBConnection")
                TryCast(logonObject, ChangeLogon).SQLServer = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnCookies).LoadOption("", "SQLServer")
                Select Case TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnCookies).LoadOption("", "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 "eu-cdbr-azure-north-e.cloudapp.net"
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "bf614abf4c83ef", "edbc0ac0", "trevor")
                    Case "84.1.95.74"
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "malomko10", _ChangeLogon.DBConnection)
                    Case "192.168.100.182", "192.168.1.31", "nuvolar.northeurope.cloudapp.azure.com", "192.168.1.75", "84.1.150.148", "localhost"
                        _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 "192.168.1.22"
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "M1cr0s0ft!", _ChangeLogon.DBConnection)
                    Case Else
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "M1cr0s0ft!12345", _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 = "Nuvolar Framework version: " & Application.ProductVersion & " [" & anm.Version.ToString & "]"
        'GLOBAL_WINFormCaption += ", (" & _plattform & ") Server: " & UCase(_ChangeLogon.SQLServer) & ", Database: " & UCase(_ChangeLogon.DBConnection)
        'GLOBAL_ApplicationStartupPath = Application.StartupPath & "\"


    End Sub

   

End Class
