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 SISBusiness.Module
Imports DevExpress.Persistent.BaseImpl
Imports SISBusiness.Module.Win
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Imports System.Net
Imports System.IO
Imports System.Text
Imports Newtonsoft.Json

Partial Public Class SISBusinessWindowsFormsApplication
    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")
                TryCast(logonObject, ChangeLogon).Company = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnString).Values("\Company")
                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)
        Dim connstr As String = ""
        Dim _ChangeLogon As ChangeLogon


        MyBase.OnLoggingOn(args)

        _ChangeLogon = TryCast(args.LogonParameters, ChangeLogon)

        GLOBAL_Company = _ChangeLogon.Company

        'Me.ConnectionString = GetConnectionGLOBAL()
        Me.ConnectionString = GetSISSecurity()
        SISGetInfoSSL()
        Dim anm As System.Reflection.AssemblyName = System.Reflection.AssemblyName.GetAssemblyName(Application.StartupPath & "\SISBusiness.Module.dll")

        GLOBAL_MainModuleVersion = anm.Version.ToString
        GLOBAL_ConnectionString = connstr
        GLOBAL_WINFormCaption = String.Format("SISBusiness Framework version: {0} [{1}]", Application.ProductVersion, anm.Version.ToString)
        GLOBAL_WINFormCaption += String.Format(", ({0})", GLOBAL_Company_Info)
        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 SISBusinessWindowsFormsApplication
        Dim winApplication As New SISBusinessWindowsFormsApplication

        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

    Private Function GetSISSecurity() As String
        Dim _ConnectionString As String = ""
        Try

            Dim _url = "http://sis-r.hu/SISChat/SISSecuritySSL.ashx?id=" & UCase(GLOBAL_Company)
            Dim _request As HttpWebRequest = TryCast(WebRequest.Create(_url), HttpWebRequest)
            Dim _response As HttpWebResponse = TryCast(_request.GetResponse, HttpWebResponse)
            Dim _reader As StreamReader = New StreamReader(_response.GetResponseStream())
            _ConnectionString = _reader.ReadToEnd

            _ConnectionString = Encoding.UTF8.GetString(Convert.FromBase64String(_ConnectionString))
            _ConnectionString = AESDecrypt(_ConnectionString, "HrX12!!0Zm7764W2", "vXm657YS+!0@mmT")

        Catch ex As Exception

        End Try
        Return _ConnectionString
    End Function
    Private Sub SISGetInfoSSL()
        Dim _InfoString As String = ""
        Dim _GetInfoClass As New GetInfoClass
        Try
            Dim _url = "http://sis-r.hu/SISChat/SISGetInfoSSL.ashx?id=" & UCase(GLOBAL_Company)
            Dim _request As HttpWebRequest = TryCast(WebRequest.Create(_url), HttpWebRequest)
            Dim _response As HttpWebResponse = TryCast(_request.GetResponse, HttpWebResponse)
            Dim _reader As StreamReader = New StreamReader(_response.GetResponseStream())
            _InfoString = _reader.ReadToEnd

            _InfoString = Encoding.UTF8.GetString(Convert.FromBase64String(_InfoString))
            _InfoString = AESDecrypt(_InfoString, "HrX12!!0Zm7764W2", "vXm657YS+!0@mmT")

            _GetInfoClass = JsonConvert.DeserializeObject(Of GetInfoClass)(_InfoString)


            GLOBAL_Company_Info = "Server: " + _GetInfoClass.SQLServer + ", Database: " + _GetInfoClass.SQLDatabase
            GLOBAL_SQLType = _GetInfoClass.SQLType
        Catch ex As Exception
        End Try
    End Sub
End Class
Public Class GetInfoClass
    Property SQLType As eSQLType
    Property SQLServer As String
    Property SQLDatabase As String
End Class

