Imports DD_Rights
Imports DD_LIB_Standards
Public Class frmRecOrgConfig_Basic
    Dim loaded As Boolean = False
    Dim changes_ModuleKonfig As Boolean = False
    Dim changes_RightService As Boolean = False
    Dim changes_PMOKonfig As Boolean = False
    Private Sub frmConfiguration_Basic_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Dim sql As String = "select * from TBDD_MODULES where UPPER(NAME) = UPPER('Record-Organizer')"
            Dim dt As DataTable = MYDB_ECM.GetDatatable(sql)
            If Not dt Is Nothing Then
                If dt.Rows.Count = 1 Then
                    Me.txtDelimiterVersion.Text = dt.Rows(0).Item("VERSION_DELIMITER")
                    Me.txtDelimiterFile.Text = dt.Rows(0).Item("FILE_DELIMITER")
                    Me.txtwddriveletter.Text = dt.Rows(0).Item("STRING1")
                    Me.txtProductName.Text = dt.Rows(0).Item("PRODUCT_NAME1")
                    Me.txtAutoLogoutUser.Text = dt.Rows(0).Item("AUTO_LOGOUT_USER")

                    ' Hintergrundbild unwandeln und anzeigen
                    If Not IsDBNull(dt.Rows(0).Item("BACKGROUND_IMAGE")) Then
                        Dim bimage() As Byte = dt.Rows(0).Item("BACKGROUND_IMAGE")
                        Dim image = ByteArrayToBitmap(bimage)
                        Me.pbBackground.Image = image
                    End If
                End If
            End If
            sql = "select * from TBPMO_SERVICE_RIGHT_CONFIG where GUID = 1"
            dt = MYDB_ECM.GetDatatable(sql)
            If Not dt Is Nothing Then
                If dt.Rows.Count = 1 Then
                    Me.txtDomäne.Text = dt.Rows(0).Item("AD_DOMAIN")
                    Me.txtUser.Text = dt.Rows(0).Item("AD_USER")
                    Dim PWplainText As String
                    Dim wrapper As New ClassEncryption("!35452didalog=")
                    ' DecryptData throws if the wrong password is used.
                    Try
                        PWplainText = wrapper.DecryptData(dt.Rows(0).Item("AD_USER_PW"))
                        Me.txtPW.Text = PWplainText
                    Catch ex As Exception
                        LOGGER.Warn("- the Password '" & dt.Rows(0).Item("AD_USER_PW") & "' could not be decrypted")
                        Me.txtPW.Text = ""
                    End Try
                    Me.txtServername.Text = dt.Rows(0).Item("AD_SERVER")
                End If
            End If
            sql = "select * from TBPMO_KONFIGURATION where GUID = 1"
            dt = MYDB_ECM.GetDatatable(sql)
            If Not dt Is Nothing Then
                If dt.Rows.Count = 1 Then
                    '  Me.chkUnicode.Checked = dt.Rows(0).Item("WD_UNICODE")
                    Me.txtWiki.Text = dt.Rows(0).Item("WIKI_STRING")
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Form Load: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        loaded = True
    End Sub

    Private Sub TextChanged_ModuleKonfig(sender As Object, e As EventArgs) Handles txtDelimiterFile.TextChanged, txtDelimiterVersion.TextChanged, txtwddriveletter.TextChanged, txtProductName.TextChanged
        If loaded = True Then
            changes_ModuleKonfig = True
            btnsave.Enabled = True
        End If
    End Sub

    Private Sub chkUnicode_CheckedChanged(sender As Object, e As EventArgs)
        If loaded = True Then
            changes_PMOKonfig = True
            btnsave.Enabled = True
        End If
    End Sub

    Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click
        If changes_ModuleKonfig = True Then
            Try
                ' Hintergrund speichern
                Dim image As Bitmap = Nothing
                Dim bimage() As Byte = Nothing

                Dim sql As String = "UPDATE TBDD_MODULES SET AUTO_LOGOUT_USER = @AUTO_LOGOUT_USER, VERSION_DELIMITER = @VERSION_DELIMITER, FILE_DELIMITER = @FILE_DELIMITER, STRING1 = @STRING1, BACKGROUND_IMAGE = @BACKGROUND_IMAGE, PRODUCT_NAME1 = @PRODUCT_NAME1, CHANGED_WHO = @CHANGED_WHO WHERE NAME = 'Record-Organizer'"
                Dim conn As New SqlClient.SqlConnection(MyConnectionString)
                Dim cmd As New SqlClient.SqlCommand(sql, conn)

                If pbBackground.ImageLocation IsNot Nothing Then
                    image = CType(Drawing.Image.FromFile(pbBackground.ImageLocation, True), Bitmap)
                    bimage = BitmapToByteArray(image)
                    cmd.Parameters.Add("@BACKGROUND_IMAGE", SqlDbType.VarBinary).Value = bimage
                Else
                    cmd.Parameters.Add("@BACKGROUND_IMAGE", SqlDbType.VarBinary).Value = DBNull.Value
                End If

                cmd.Parameters.Add("@VERSION_DELIMITER", SqlDbType.Text).Value = txtDelimiterVersion.Text
                cmd.Parameters.Add("@AUTO_LOGOUT_USER", SqlDbType.Int).Value = txtAutoLogoutUser.Text
                cmd.Parameters.Add("@FILE_DELIMITER", SqlDbType.Text).Value = txtDelimiterFile.Text
                cmd.Parameters.Add("@STRING1", SqlDbType.Text).Value = txtwddriveletter.Text
                cmd.Parameters.Add("@CHANGED_WHO", SqlDbType.Text).Value = USER_USERNAME
                cmd.Parameters.Add("@PRODUCT_NAME1", SqlDbType.Text).Value = txtProductName.Text

                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()

                lblSaveresult.Visible = True
            Catch ex As Exception
                MsgBox("Error in Save ModuleConfig: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
        If changes_PMOKonfig = True Then
            Try
                Dim wduni As Integer = 0
                'If chkUnicode.Checked Then
                '    wduni = 1
                'End If
                Dim upd = String.Format("UPDATE TBPMO_KONFIGURATION SET WIKI_STRING = '{0}', CHANGED_WHO = '{1}', WD_UNICODE = {2} WHERE GUID = 1", Me.txtWiki.Text, USER_USERNAME, wduni)

                If MYDB_ECM.ExecuteNonQuery(upd) = True Then
                    lblSaveresult.Visible = True
                Else
                    lblSaveresult.Visible = False
                End If
            Catch ex As Exception
                MsgBox("Error in Save Config: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
        If changes_RightService = True Then
            Dim wrapper As New ClassEncryption("!35452didalog=")
            Dim cipherText As String = wrapper.EncryptData(Me.txtPW.Text)
            Dim pw As String = cipherText

            Dim upd = String.Format("UPDATE TBPMO_SERVICE_RIGHT_CONFIG SET AD_DOMAIN = '{0}', AD_USER = '{1}', AD_USER_PW = '{2}', AD_SERVER = '{3}', CHANGED_WHO = '{4}' WHERE GUID = 1", Me.txtDomäne.Text, Me.txtUser.Text, pw, Me.txtServername.Text, USER_USERNAME)
            MYDB_ECM.ExecuteNonQuery(upd)
            btnsave.Enabled = False

        End If

    End Sub

    Private Sub chkChildWindow_CheckedChanged(sender As Object, e As EventArgs) Handles chkChildWindow.CheckedChanged
        My.Settings.Save()
    End Sub

    Private Sub btnSelectBackground_Click(sender As Object, e As EventArgs) Handles btnSelectBackground.Click
        OpenFileDialog1.Filter = "PNG Bilder|*.png"

        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Dim path As String = OpenFileDialog1.FileName
            pbBackground.ImageLocation = path
            btnsave.Enabled = True
            changes_ModuleKonfig = True
        End If
    End Sub

    Private Sub btnResetBackground_Click(sender As Object, e As EventArgs) Handles btnResetBackground.Click
        If pbBackground.Image IsNot Nothing Then
            btnsave.Enabled = True
            changes_ModuleKonfig = True
        End If

        pbBackground.ImageLocation = Nothing
        pbBackground.Image = Nothing
    End Sub

    Private Sub btnTest_Anmeldung_Click(sender As Object, e As EventArgs) Handles btnTest_Anmeldung.Click
        Try
            DD_Rights.clsLogger.Init(Application.UserAppDataPath() & "\Log", USER_USERNAME)
            Dim session = DD_Rights.ClassRights.GetWMSessionAsUser(Me.txtDomäne.Text, Me.txtServername.Text, Me.txtUser.Text, Me.txtPW.Text)
            If Not IsNothing(session) Then
                MsgBox("The persionalized connection to windream was created successfully!", MsgBoxStyle.Information)
                Dim wrapper As New ClassEncryption("!35452didalog=")
                Dim cipherText As String = wrapper.EncryptData(Me.txtPW.Text)
                Dim pw As String = cipherText

                Dim upd = String.Format("UPDATE TBPMO_SERVICE_RIGHT_CONFIG SET AD_DOMAIN = '{0}', AD_USER = '{1}', AD_USER_PW = '{2}', AD_SERVER = '{3}', CHANGED_WHO = '{4}' WHERE GUID = 1", Me.txtDomäne.Text, Me.txtUser.Text, pw, Me.txtServername.Text, USER_USERNAME)
                MYDB_ECM.ExecuteNonQuery(upd)
                btnsave.Enabled = False
            Else
                MsgBox("The persionalized connection to windream could NOT be created." & vbNewLine & "Please check the User-Data!", MsgBoxStyle.Critical)
            End If
        Catch ex As Exception
            MsgBox("Error in Check windream login: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub txtDomäne_TextChanged(sender As Object, e As EventArgs) Handles txtDomäne.TextChanged
        If loaded = True Then
            changes_RightService = True
            btnsave.Enabled = True
        End If
    End Sub

    Private Sub txtUser_TextChanged(sender As Object, e As EventArgs) Handles txtUser.TextChanged
        If loaded = True Then
            changes_RightService = True
            btnsave.Enabled = True
        End If
    End Sub

    Private Sub txtPW_TextChanged(sender As Object, e As EventArgs) Handles txtPW.TextChanged
        If loaded = True Then
            changes_RightService = True
            btnsave.Enabled = True
        End If
    End Sub

    Private Sub txtServername_TextChanged(sender As Object, e As EventArgs) Handles txtServername.TextChanged
        If loaded = True Then
            changes_RightService = True
            btnsave.Enabled = True
        End If
    End Sub

    Private Sub txtWiki_TextChanged(sender As Object, e As EventArgs) Handles txtWiki.TextChanged
        If loaded = True Then
            changes_PMOKonfig = True
            btnsave.Enabled = True
        End If
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
        Select Case TabControl1.SelectedIndex
            Case 1
                Dim sql = String.Format("SELECT * FROM TBDD_MODULES WHERE UPPER(NAME) = UPPER('Record-Organizer')")
                Dim KONFIG_DT As DataTable = MYDB_ECM.GetDatatable(sql)
                If KONFIG_DT.Rows.Count = 1 Then
                    txtBoxUpdatePath.Text = KONFIG_DT.Rows(0).Item("VERSION_UPATE_PATH")
                End If
        End Select
    End Sub

    Private Sub btnSaveUpdatePath_Click(sender As Object, e As EventArgs) Handles btnSaveUpdatePath.Click
        Dim upd = String.Format("UPDATE TBDD_MODULES SET VERSION_UPATE_PATH = '{0}' WHERE UPPER(NAME) = UPPER('Record-Organizer')", txtBoxUpdatePath.Text)
        If MYDB_ECM.ExecuteNonQuery(upd) = True Then
            lblSave.Visible = True
        Else
            lblSave.Visible = False
        End If
    End Sub

    Private Sub txtWiki_VisibleChanged(sender As Object, e As EventArgs) Handles txtWiki.VisibleChanged

    End Sub

    Private Sub txtAutoLogoutUser_TextChanged(sender As Object, e As EventArgs) Handles txtAutoLogoutUser.TextChanged
        If loaded = True Then
            changes_ModuleKonfig = True
            btnsave.Enabled = True
        End If
    End Sub
End Class