'  
'   Rebex Sample Code License
' 
'   Copyright 2026, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System
Imports System.IO
Imports System.Windows.Forms

''' <summary>
''' The Connection class provides user interface for setting the ssh connection settings.
''' </summary>
Public Class Connection
    Inherits Form
    ''' <summary>
    ''' Required designer variable.
    ''' </summary>
    Private components As System.ComponentModel.IContainer = Nothing
    Private txtHost As System.Windows.Forms.TextBox
    Private label1 As System.Windows.Forms.Label
    Private btnOk As System.Windows.Forms.Button

    Private _config As Configuration

    ''' <summary>
    ''' Gets the typed host name.
    ''' </summary>
    Public ReadOnly Property HostName() As String
        Get
            Return txtHost.Text
        End Get
    End Property

    ''' <summary>
    ''' Initialize this class.
    ''' </summary>
    ''' <param name="configFile">Config file.</param>
    Public Sub New(ByVal configFile As String)
        InitializeComponent()

        ' initialize the config
        _config = New Configuration(configFile)

        ' set the values from the config
        txtHost.Text = _config.GetString("HostName")
    End Sub

    ''' <summary>
    ''' Clean up any resources being used.
    ''' </summary>
    ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso Not (components Is Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

#Region "Windows Form Designer generated code"

    ''' <summary>
    ''' Required method for Designer support - do not modify
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()
        Me.txtHost = New System.Windows.Forms.TextBox()
        Me.label1 = New System.Windows.Forms.Label
        Me.btnOk = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        ' 
        ' txtHost
        ' 
        Me.txtHost.Location = New System.Drawing.Point(80, 12)
        Me.txtHost.Name = "txtHost"
        Me.txtHost.Size = New System.Drawing.Size(125, 20)
        Me.txtHost.TabIndex = 0
        Me.txtHost.Text = ""
        ' 
        ' label1
        ' 
        Me.label1.AutoSize = True
        Me.label1.Location = New System.Drawing.Point(10, 15)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(59, 16)
        Me.label1.TabIndex = 1
        Me.label1.Text = "Hostname:"
        ' 
        ' btnOk
        ' 
            Me.btnOk.Location = New System.Drawing.Point(144, 42)
        Me.btnOk.Name = "btnOk"
        Me.btnOk.Size = New System.Drawing.Size(62, 23)
        Me.btnOk.TabIndex = 3
        Me.btnOk.Text = "OK"
        AddHandler Me.btnOk.Click, AddressOf btnOk_Click
        ' 
        ' Connection
        ' 
        Me.AcceptButton = Me.btnOk
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(218, 77)
        Me.Controls.Add(Me.txtHost)
        Me.Controls.Add(Me.label1)
        Me.Controls.Add(Me.btnOk)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MinimizeBox = True
        Me.MaximizeBox = False
        Me.ShowInTaskbar = False
        Me.Name = "Connection"
        Me.Text = "Connection"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ''' <summary>
    ''' Handles the button 'Ok' click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' check the host name
        If txtHost.Text = String.Empty Then
            MessageBox.Show("Hostname cannot be empty.")
            Return
        End If

        ' save typed values
        _config.SetValue("HostName", txtHost.Text)
        _config.Save()

        ' close the form
        DialogResult = DialogResult.OK
        Close()
    End Sub
End Class
