Imports DigitalData.Modules.Messaging
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Database
Imports FirebirdSql.Data.FirebirdClient
Imports System.IO

Public Class frmEmailAccount
    Private _bindingSource As BindingSource
    Private _tableName As String = "TBEDM_EMAIL_ACCOUNT"
    Private _tableAdapter As FbDataAdapter
    Private _dataSet As New DataSet()
    Private _Logger As Logger
    Private _MyLogger As LogConfig
    Private _firebird As Firebird
    Private _Email As Email
    Private oTable As DataTable
    Private Sub Button5_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnsendtestmail_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub ToolStripButton15_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub ToolStripButton21_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub ToolStripButton22_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub frmEmailAccount_Load(sender As Object, e As EventArgs) Handles Me.Load
        _MyLogger = New LogConfig(LogConfig.PathType.CustomPath, Path.Combine(My.Application.Info.DirectoryPath, "Log"))
        _Logger = _MyLogger.GetLogger()
        _firebird = New Firebird(_MyLogger, My.Settings.FB_DATASOURCE, My.Settings.FB_DATABASE, My.Settings.FB_USER, My.Settings.FB_PW)
        _Email = New Email(_MyLogger)


        Dim oConnection As FbConnection
        oConnection = New FbConnection(GetConnectionString())
        oConnection.Open()

        _dataSet.Tables.Add(New DataTable(_tableName) With {.CaseSensitive = True})

        BindingSourceForm.DataSource = _dataSet
        BindingSourceForm.DataMember = _tableName

        _tableAdapter = New FbDataAdapter() With {
            .SelectCommand = GetSelectCommand(oConnection),
            .InsertCommand = GetInsertCommand(oConnection),
            .UpdateCommand = GetUpdateCommand(oConnection),
            .DeleteCommand = New FbCommand(),
            .ContinueUpdateOnError = True
        }
        _tableAdapter.Fill(_dataSet, _tableName)
        oTable = _dataSet.Tables.Item(_tableName)

        txtGUID.DataBindings.Add(New Binding("Text", BindingSourceForm, "GUID"))
        txtPROFILE_NAME.DataBindings.Add(New Binding("Text", BindingSourceForm, "PROFILE_NAME"))
        txtEMAIL_FROM.DataBindings.Add(New Binding("Text", BindingSourceForm, "EMAIL_FROM"))
        txtSERVER_IN.DataBindings.Add(New Binding("Text", BindingSourceForm, "SERVER_IN"))
        txtSERVER_OUT.DataBindings.Add(New Binding("Text", BindingSourceForm, "SERVER_OUT"))
        txtEMAIL_USER.DataBindings.Add(New Binding("Text", BindingSourceForm, "EMAIL_USER"))
        txtEMAIL_PW.DataBindings.Add(New Binding("Text", BindingSourceForm, "EMAIL_PW"))
        ' txtPORT_IN.DataBindings.Add(New Binding("Text", BindingSourceForm, "PORT_IN"))
        txtPORT_OUT.DataBindings.Add(New Binding("Text", BindingSourceForm, "PORT_OUT"))
        cmbAUTH_TYPE.DataBindings.Add(New Binding("Text", BindingSourceForm, "AUTH_TYPE"))
        cmbPOP_IMAP.DataBindings.Add(New Binding("Text", BindingSourceForm, "POP_IMAP"))
        ADDED_WHOTextBox.DataBindings.Add(New Binding("Text", BindingSourceForm, "CREATEDWHO"))
        ADDED_WHENTextBox.DataBindings.Add(New Binding("Text", BindingSourceForm, "CREATEDWHEN"))
        CHANGED_WHOTextBox.DataBindings.Add(New Binding("Text", BindingSourceForm, "CHANGEDWHO"))
        CHANGED_WHENTextBox.DataBindings.Add(New Binding("Text", BindingSourceForm, "CHANGEDWHEN"))



        GridControl1.DataSource = BindingSourceForm
        BindingNavigator1.BindingSource = BindingSourceForm
        GridView1.BestFitColumns(True)
        oConnection.Close()
    End Sub
    Private Function GetSelectCommand(Connection As FbConnection) As FbCommand
        Dim oCommand As New FbCommand() With {
            .Connection = Connection,
            .CommandText = $"SELECT * FROM TBEDM_EMAIL_ACCOUNT"
        }

        Return oCommand
    End Function

    Private Function GetInsertCommand(Connection As FbConnection) As FbCommand
        Dim oSQL = "INSERT INTO TBEDM_EMAIL_ACCOUNT (PROFILE_NAME,POP_IMAP,EMAIL_FROM,FROM_NAME,SERVER_IN,SERVER_OUT,EMAIL_USER,EMAIL_PW,AUTH_TYPE,PORT_IN,PORT_OUT,ACTIVE,CREATEDWHO) VALUES (@PROFILE_NAME,@POP_IMAP,@EMAIL_FROM,@FROM_NAME,@SERVER_IN,@SERVER_OUT,@EMAIL_USER,@EMAIL_PW,@AUTH_TYPE,@PORT_IN,@PORT_OUT,@ACTIVE,@CREATEDWHO)"

        Dim oCommand As New FbCommand With {
            .Connection = Connection,
            .CommandType = CommandType.Text,
            .CommandText = oSQL
        }

        With oCommand.Parameters
            .Add(New FbParameter("@PROFILE_NAME", FbDbType.VarChar, 50, "PROFILE_NAME"))
            .Add(New FbParameter("@POP_IMAP", FbDbType.VarChar, 20, "POP_IMAP"))
            .Add(New FbParameter("@EMAIL_FROM", FbDbType.VarChar, 100, "EMAIL_FROM"))
            .Add(New FbParameter("@FROM_NAME", FbDbType.VarChar, 100, "FROM_NAME"))
            .Add(New FbParameter("@SERVER_IN", FbDbType.VarChar, 100, "SERVER_IN"))
            .Add(New FbParameter("@SERVER_OUT", FbDbType.VarChar, 100, "SERVER_OUT"))
            .Add(New FbParameter("@EMAIL_USER", FbDbType.VarChar, 100, "EMAIL_USER"))
            .Add(New FbParameter("@EMAIL_PW", FbDbType.VarChar, 100, "EMAIL_PW"))
            .Add(New FbParameter("@AUTH_TYPE", FbDbType.VarChar, 100, "AUTH_TYPE"))
            .Add(New FbParameter("@PORT_IN", FbDbType.Integer, 10, "PORT_IN"))
            .Add(New FbParameter("@PORT_OUT", FbDbType.Integer, 10, "PORT_OUT"))
            '.Add(New FbParameter("@ACTIVE", FbDbType.Boolean, 10, "ACTIVE"))
            .Add(New FbParameter("@CREATEDWHO", FbDbType.VarChar, 50, "CREATEDWHO"))

        End With
    End Function
    Private Function GetUpdateCommand(Connection As FbConnection) As FbCommand
        Dim oSQL = "UPDATE TBEDM_EMAIL_ACCOUNT SET PROFILE_NAME = @PROFILE_NAME, POP_IMAP = @POP_IMAP,EMAIL_FROM = @EMAIL_FROM,FROM_NAME = @FROM_NAME,SERVER_IN = @SERVER_IN,SERVER_OUT = @SERVER_OUT,EMAIL_USER = @EMAIL_USER,EMAIL_PW = @EMAIL_PW,AUTH_TYPE = @AUTH_TYPE,PORT_IN = @PORT_IN,PORT_OUT = @PORT_OUT,ACTIVE = @ACTIVE,CHANGEDWHO = @CHANGEDWHO WHERE GUID = @GUID"

        Dim oCommand As New FbCommand With {
            .Connection = Connection,
            .CommandType = CommandType.Text,
            .CommandText = oSQL
        }

        With oCommand.Parameters
            .Add(New FbParameter("@GUID", FbDbType.Integer, 10, "GUID"))
            .Add(New FbParameter("@PROFILE_NAME", FbDbType.VarChar, 50, "PROFILE_NAME"))
            .Add(New FbParameter("@POP_IMAP", FbDbType.VarChar, 20, "POP_IMAP"))
            .Add(New FbParameter("@EMAIL_FROM", FbDbType.VarChar, 100, "EMAIL_FROM"))
            .Add(New FbParameter("@FROM_NAME", FbDbType.VarChar, 100, "FROM_NAME"))
            .Add(New FbParameter("@SERVER_IN", FbDbType.VarChar, 100, "SERVER_IN"))
            .Add(New FbParameter("@SERVER_OUT", FbDbType.VarChar, 100, "SERVER_OUT"))
            .Add(New FbParameter("@EMAIL_USER", FbDbType.VarChar, 100, "EMAIL_USER"))
            .Add(New FbParameter("@EMAIL_PW", FbDbType.VarChar, 100, "EMAIL_PW"))
            .Add(New FbParameter("@AUTH_TYPE", FbDbType.VarChar, 100, "AUTH_TYPE"))
            .Add(New FbParameter("@PORT_IN", FbDbType.Integer, 10, "PORT_IN"))
            .Add(New FbParameter("@PORT_OUT", FbDbType.Integer, 10, "PORT_OUT"))
            '  .Add(New FbParameter("@ACTIVE", FbDbType.Boolean, "ACTIVE"))
            .Add(New FbParameter("@CHANGEDWHO", FbDbType.VarChar, 50, "CHANGEDWHO"))

        End With
    End Function
    Private Function GetConnectionString()
        Dim oConnectionString = New FbConnectionStringBuilder With {
            .DataSource = My.Settings.FB_DATASOURCE,
            .Database = My.Settings.FB_DATABASE,
            .UserID = My.Settings.FB_USER,
            .Password = My.Settings.FB_PW,
            .Dialect = 3
        }.ToString()

        Return oConnectionString
    End Function

    Private Sub btnsendtestmail_Click_1(sender As Object, e As EventArgs) Handles btnsendtestmail.Click
        If txtTestmail.Text <> String.Empty Then
            My.Settings.Save()
            Dim wrapper As New clsEncryption("!35452didalog=")

            Dim PWPlain = wrapper.DecryptData(txtEMAIL_PW.Text)

            If _Email.NewEmail(txtTestmail.Text, "Testmail", "This is the bodytext", txtEMAIL_FROM.Text, txtSERVER_OUT.Text, txtPORT_OUT.Text, txtEMAIL_USER.Text, PWPlain, cmbAUTH_TYPE.Text, "EDMI GUI") = True Then
                MsgBox("Email was send successfully.", MsgBoxStyle.Information)
            Else
                MsgBox("Could not send the testmail. Please check the log.", MsgBoxStyle.Exclamation)
            End If
        End If
    End Sub

    Private Sub tsbtnSaveEmailAccount_Click(sender As Object, e As EventArgs) Handles tsbtnSaveEmailAccount.Click
        Me.Validate()
        Me.BindingSourceForm.EndEdit()
        oTable.AcceptChanges()
        If Not IsNothing(oTable.GetChanges) Then
            _tableAdapter.Update(oTable)
            MsgBox("DONE!")
        Else
            MsgBox("No changes")
        End If
        _tableAdapter.Update(oTable)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim wrapper As New clsEncryption("!35452didalog=")
        Dim pwencrypted = wrapper.EncryptData(txtnewpasswort.Text)
        txtEMAIL_PW.Text = pwencrypted
        Dim upd = $"UPDATE TBEDM_EMAIL_ACCOUNT SET EMAIL_PW = '{pwencrypted}', CHANGEDWHO = '{Environment.UserName}' WHERE GUID = {txtGUID.Text}"
        If _firebird.ExecuteNonQuery(upd) = True Then
            MsgBox("Passwort has been encrypted!")
        End If

    End Sub
End Class