Imports System.ComponentModel
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.Logging

Public Class frmServiceConfig
    Private Client As Client
    Private Logger As Logger

    Private Const STATUS_CONNECTED = "Connection Established"
    Private Const STATUS_CONNECTING = "Trying to create connection..."
    Private Const STATUS_FAILED = "Connection Failed!"

    Private ConnectionChanged As Boolean = False

    Public Property ServiceAddress As String = ""
    Public Property ServiceOnline As Boolean = False

    Private Sub frmServiceConfig_Load(sender As Object, e As EventArgs) Handles Me.Load
        Logger = My.LogConfig.GetLogger()

        If ServiceAddress <> String.Empty Then
            Dim oAddress = Client.ParseServiceAddress(ServiceAddress)
            txtServiceAddress.Text = oAddress.Item1
            txtServicePort.Text = oAddress.Item2
        End If
        If ServiceOnline Then
            txtStatus.Text = STATUS_CONNECTED
        End If

        txtServiceAddress.Focus()
    End Sub

    Private Sub btnOK_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnOK.ItemClick
        Try
            Dim oIPAddress = txtServiceAddress.Text
            Dim oPort = Integer.Parse(txtServicePort.Text)

            Client = New Client(My.LogConfig, oIPAddress, oPort)

            txtStatus.Text = STATUS_CONNECTING
            Dim oResult = Client.Connect()

            If oResult = True Then
                ServiceAddress = $"{oIPAddress}:{oPort}"
                ServiceOnline = True
                txtStatus.Text = STATUS_CONNECTED

            Else
                ServiceAddress = ""
                ServiceOnline = True
                txtStatus.Text = STATUS_FAILED
                ' TODO: Make a connection test that is as elaborate as this one  :D
                'Select Case oResult
                '    Case ClassService.ConnectionTestResult.NotFound
                '        lblStatus.Text = "Dienst konnte nicht gefunden werden. Bitte überprüfen sie Addresse und Port."
                '    Case ClassService.ConnectionTestResult.EmptyURI
                '        lblStatus.Text = "Bitte tragen Sie eine gültige Dienst Adresse ein."
                '    Case ClassService.ConnectionTestResult.Authentication
                '        lblStatus.Text = "Authentifizierungsfehler. Prüfen Sie, ob sich Ihr Gerät in der korrekten Domäne befindet."
                '    Case Else
                '        lblStatus.Text = "Unbekannter Fehler."
                'End Select
            End If
        Catch ex As Exception
            Logger.Error(ex)
            MsgBox("Fehler beim Verbindungsaufbau", MsgBoxStyle.Critical, Text)
        End Try
    End Sub

    Private Sub frmServiceConfig_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        If ServiceAddress <> "" And ServiceOnline = True Then
            DialogResult = DialogResult.OK
        Else
            DialogResult = DialogResult.Cancel
        End If
    End Sub
End Class