'  
'   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 label2 As System.Windows.Forms.Label
    Private txtPassword As System.Windows.Forms.TextBox
    Private label3 As System.Windows.Forms.Label
    Private txtUsername As System.Windows.Forms.TextBox
    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>
    ''' Gets the typed user name.
    ''' </summary>
    Public ReadOnly Property UserName() As String
        Get
            Return txtUsername.Text
        End Get
    End Property

    ''' <summary>
    ''' Gets the typed password.
    ''' </summary>
    Public ReadOnly Property Password() As String
        Get
            Return txtPassword.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")
        txtUsername.Text = _config.GetString("UserName")
        txtPassword.Text = _config.GetString("Password")
    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.label2 = New System.Windows.Forms.Label()
        Me.txtPassword = New System.Windows.Forms.TextBox()
        Me.label3 = New System.Windows.Forms.Label()
        Me.txtUsername = New System.Windows.Forms.TextBox()
        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:"
        ' 
        ' label2
        ' 
        Me.label2.AutoSize = True
        Me.label2.Location = New System.Drawing.Point(10, 67)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(57, 16)
        Me.label2.TabIndex = 3
        Me.label2.Text = "Password:"
        ' 
        ' txtPassword
        ' 
        Me.txtPassword.Location = New System.Drawing.Point(80, 64)
        Me.txtPassword.Name = "txtPassword"
        Me.txtPassword.PasswordChar = "*"c
        Me.txtPassword.Size = New System.Drawing.Size(125, 20)
        Me.txtPassword.TabIndex = 2
        Me.txtPassword.Text = ""
        ' 
        ' label3
        ' 
        Me.label3.AutoSize = True
        Me.label3.Location = New System.Drawing.Point(10, 41)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(60, 16)
        Me.label3.TabIndex = 5
        Me.label3.Text = "Username:"
        ' 
        ' txtUsername
        ' 
        Me.txtUsername.Location = New System.Drawing.Point(80, 38)
        Me.txtUsername.Name = "txtUsername"
        Me.txtUsername.Size = New System.Drawing.Size(125, 20)
        Me.txtUsername.TabIndex = 1
        Me.txtUsername.Text = ""
        ' 
        ' btnOk
        ' 
        Me.btnOk.Location = New System.Drawing.Point(144, 90)
        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, 125)
        Me.Controls.Add(Me.label3)
        Me.Controls.Add(Me.label2)
        Me.Controls.Add(Me.txtUsername)
        Me.Controls.Add(Me.txtPassword)
        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

        ' check the user name
        If txtUsername.Text = String.Empty Then
            MessageBox.Show("Username cannot be empty.")
            Return
        End If

        ' save typed values
        _config.SetValue("HostName", txtHost.Text)
        _config.SetValue("UserName", txtUsername.Text)
        _config.SetValue("Password", txtPassword.Text)
        _config.Save()

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