Imports System.Net
Imports Org.BouncyCastle.Crypto
Imports Org.BouncyCastle.Crypto.Generators
Imports VERAG_PROG_ALLGEMEIN

Public Class frmAPIZugaenge

    Dim APIBind As cEasyBinding
    Public table
    Public BIND_DB
    Private EditRow As Integer = -1
    Sub New(table, BIND_DB)
        Me.table = table
        Me.BIND_DB = BIND_DB
        InitializeComponent()
    End Sub

    Private Sub frmAPIZugaengen_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        loadDGV()
        btnOK.Visible = True


    End Sub

    Private Sub loadDGV(Optional sqlwhere As String = "")

        Try
            APIBind = New cEasyBinding(BIND_DB)


            If sqlwhere = "" Then

                APIBind.initBinding("SELECT [userId] ,[username] ,[password] ,[customerId] ,[type] ,[active], [description], hashedPassword FROM " & table & " WHERE [active] = " & IIf(cbxProduktivsystem.Checked, "1", "0"), table)
            Else

                APIBind.initBinding("Select [userId] ,[username] ,[password] ,[customerId] ,[type] ,[active], [description], hashedPassword  FROM " & table & " WHERE [active] = " & IIf(cbxProduktivsystem.Checked, "1", "0") & sqlwhere, table)

            End If

            DataGridView.DataSource = APIBind.bindingdataTable

            With DataGridView

                If .Columns.Count > 0 Then

                    .Columns("userId").Visible = False
                    .Columns("username").HeaderText = "Username"
                    .Columns("username").Width = 100
                    .Columns("password").HeaderText = "PW"
                    .Columns("password").Width = 100
                    .Columns("hashedPassword").HeaderText = "Hashed PW"
                    .Columns("hashedPassword").Width = 250
                    .Columns("customerId").HeaderText = "CustomerID"
                    .Columns("customerId").Width = 100
                    .Columns("type").HeaderText = "Type"
                    .Columns("type").Width = 100
                    .Columns("description").HeaderText = "Beschreibung"
                    .Columns("description").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    .Columns("active").HeaderText = "Aktiv"
                    .Columns("active").Width = 25

                End If

            End With



        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try

    End Sub

    Private Sub DataGridView_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles DataGridView.DefaultValuesNeeded
        Try

            e.Row.Cells("active").Value = False
            e.Row.Cells("type").Value = "REST"

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
    End Sub


    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click


        If DataGridView.Columns.Count = 0 Or DataGridView.SelectedRows.Count = 0 Then 'alles gelöscht oder markierte Zeile gelöscht
            APIBind.updateBinding()
            Exit Sub
        End If

        If DataGridView.CurrentRow.Cells("username").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("password").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("type").ToString <> "" AndAlso EditRow >= 0 Then
            APIBind.updateBinding()
        End If

    End Sub


    Private Sub DataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView.EditingControlShowing
        EditRow = DataGridView.CurrentRow.Index
    End Sub

    Private Sub DataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView.SelectionChanged
        If EditRow >= 0 Then
            Dim new_row As Integer = EditRow
            EditRow = -1
            DataGridView.CurrentCell = DataGridView.Rows(new_row).Cells(
                DataGridView.CurrentCell.ColumnIndex)
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)
        If DataGridView.SelectedRows.Count = 0 Then Exit Sub
    End Sub

    Private Sub txtSuche_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSuche.KeyDown

        Dim sqladdon As String = ""


        If e.KeyCode = Keys.Enter Then
            If txtSuche.Text <> "" Then
                sqladdon &= " and [username] Like '%" & txtSuche.Text & "%' or  [type] like '%" & txtSuche.Text & "%'"
            End If
            loadDGV(sqladdon)
        End If

    End Sub

    Private Sub cbxProduktivsystem_CheckedChanged(sender As Object, e As EventArgs) Handles cbxProduktivsystem.CheckedChanged
        Dim sqladdon As String = ""
        If txtSuche.Text <> "" Then
            sqladdon &= " and [username] Like '%" & txtSuche.Text & "%' or  [type] like '%" & txtSuche.Text & "%'"
        End If
        loadDGV(sqladdon)

    End Sub

    Private Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
        Dim webAddress As String = "https://wiki.verag.ag/de/software/Offene_Punkte#aviso-rest-api"
        Process.Start(webAddress)
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        If DataGridView.SelectedRows.Count = 0 Then Exit Sub

        For Each r As DataGridViewRow In DataGridView.SelectedRows
            If r.Cells("password").Value IsNot Nothing AndAlso r.Cells("password").Value <> "" Then
                If IsDBNull(r.Cells("hashedPassword").Value) OrElse r.Cells("hashedPassword").Value = "" Then
                    If Not vbYes = MsgBox("Möchten Sie die Passwörter der markierten Datensätze hashen?" & vbNewLine & "Achtung, das Klartextpassowrt wird nicht in der Datenbank gespeichert!", vbYesNoCancel) Then
                        Exit Sub
                    End If

                    Dim hashedPW = BCrypt.Net.BCrypt.HashPassword(r.Cells("password").Value).ToString
                    r.Cells("hashedPassword").Value = hashedPW
                    r.Cells("password").Value = ""
                        APIBind.updateBinding()
                    End If

                End If
        Next

        Dim sqladdon As String = ""
        If txtSuche.Text <> "" Then
            sqladdon &= " and [username] Like '%" & txtSuche.Text & "%' or  [type] like '%" & txtSuche.Text & "%'"
        End If
        loadDGV(sqladdon)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim testpw = InputBox("zu testendes Passwort eingeben!", "PW-TESTER - Funktion des Hashing/Validation")

        If testpw = "" Then Exit Sub

        Dim passwordHash1 As String = BCrypt.Net.BCrypt.HashPassword(testpw)
        Dim passwordHash2 As String = BCrypt.Net.BCrypt.HashPassword(testpw)
        Dim passwordHash3 As String = BCrypt.Net.BCrypt.HashPassword(testpw)

        Dim verified1 As Boolean = BCrypt.Net.BCrypt.Verify(testpw, passwordHash1)
        Dim verified2 As Boolean = BCrypt.Net.BCrypt.Verify(testpw, passwordHash2)
        Dim verified3 As Boolean = BCrypt.Net.BCrypt.Verify(testpw, passwordHash3)

        MsgBox(passwordHash1 & vbNewLine & passwordHash2 & vbNewLine & passwordHash3 & vbNewLine & verified1 & vbNewLine & verified2 & vbNewLine & verified3)

    End Sub


End Class