Imports System.Net
Imports VERAG_PROG_ALLGEMEIN

Public Class frmAPIEinstellungen

    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 frmAPIEinstellungen_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 [api_active],[api_productive], [api_program],[api_id],[api_url] ,[api_user],[api_password] ,[api_UseToken],[api_token] ,[api_description] FROM " & table, table)
            Else

                APIBind.initBinding("SELECT [api_active],[api_productive], [api_program], [api_id],[api_url] ,[api_user],[api_password] ,[api_useToken],[api_token] ,[api_description]  FROM " & table & " WHERE " & sqlwhere, table)

            End If

            DataGridView.DataSource = APIBind.bindingdataTable

            With DataGridView

                If .Columns.Count > 0 Then

                    .Columns("api_id").Visible = False
                    .Columns("api_active").HeaderText = "Aktiv"
                    .Columns("api_active").Width = 50
                    .Columns("api_productive").HeaderText = "Produktiv"
                    .Columns("api_productive").Width = 70
                    .Columns("api_program").HeaderText = "Programm"
                    .Columns("api_program").Width = 80
                    .Columns("api_url").HeaderText = "URL"
                    .Columns("api_url").Width = 150
                    .Columns("api_user").HeaderText = "User"
                    .Columns("api_user").Width = 100
                    .Columns("api_password").HeaderText = "PW"
                    .Columns("api_password").Width = 100
                    .Columns("api_useToken").HeaderText = "Token"
                    .Columns("api_useToken").Width = 40
                    .Columns("api_Token").HeaderText = "Token-Key"
                    .Columns("api_Token").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    .Columns("api_description").HeaderText = "Beschreibung"
                    .Columns("api_description").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

                    Dim c As New DataGridViewImageColumn
                    c.Name = "isRunning" : c.HeaderText = "Online"
                    c.Image = My.Resources.wait : c.ImageLayout = DataGridViewImageCellLayout.Zoom
                    c.Width = 40 : c.DefaultCellStyle.Padding = New Padding(2) : c.DefaultCellStyle.BackColor = Color.White
                    c.DefaultCellStyle.Tag = "Online"
                    .Columns.Add(c)



                    For Each r As DataGridViewRow In .Rows
                        If r.Cells("api_url").Value Is DBNull.Value Then
                            r.DefaultCellStyle.ForeColor = Color.Gray
                        ElseIf CBool(r.Cells("api_active").Value) = False Then
                            r.DefaultCellStyle.ForeColor = Color.Gray
                        End If

                        If r.Cells("api_url").Value IsNot DBNull.Value AndAlso CheckAddress(r.Cells("api_url").Value) Then
                            DirectCast(r.Cells("isRunning"), DataGridViewImageCell).Value = My.Resources.ok
                        Else
                            DirectCast(r.Cells("isRunning"), DataGridViewImageCell).Value = My.Resources.del
                        End If
                    Next
                End If


            End With



        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

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

            e.Row.Cells("api_active").Value = True
            e.Row.Cells("api_productive").Value = False
            e.Row.Cells("api_token").Value = False

        Catch ex As Exception
            MsgBox(ex.Message)
        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("api_url").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("api_user").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("api_password").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 Function CheckAddress(url As String) As Boolean
        Try
            If url = "" Then Return False
            Dim request As WebRequest = WebRequest.Create(url)
            Dim response As WebResponse = request.GetResponse()
        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function

    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 &= " [api_program] like '%" & txtSuche.Text & "%' or  [api_url] like '%" & txtSuche.Text & "%'"
            End If
            loadDGV(sqladdon)
        End If

    End Sub
End Class