'  
'   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
Imports Rebex.TerminalEmulation

''' <summary>
''' The main form of the application. It cantains the ssh terminal control.
''' </summary>
Public Class MainForm
    Inherits Form
    ''' <summary>
    ''' Path to the config file at Local application data storage on the File system.
    ''' </summary>
    Private Shared ReadOnly _configFile As String = Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), String.Format("Rebex{0}Telnet{0}SimpleWinFormClient.xml", Path.DirectorySeparatorChar))

    ''' <summary>
    ''' Required designer variable.
    ''' </summary>
    Private components As System.ComponentModel.IContainer = Nothing
    Private stcConsole As TelnetTerminalControl

    ''' <summary>
    ''' Initialize this class.
    ''' </summary>
    Public Sub New()
        InitializeComponent()

        CheckForIllegalCrossThreadCalls = True
    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()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MainForm))
        Me.stcConsole = New TelnetTerminalControl
        Me.SuspendLayout()
        '
        'stcConsole
        '
        Me.stcConsole.BackColor = System.Drawing.Color.Magenta
        Me.stcConsole.Location = New System.Drawing.Point(0, 0)
        Me.stcConsole.Name = "stcConsole"
        Me.stcConsole.Recorder = Nothing
        Me.stcConsole.ServerName = ""
        Me.stcConsole.Size = New System.Drawing.Size(737, 400)
        Me.stcConsole.TabIndex = 0
        Me.stcConsole.ServerPort = 23
        '
        'MainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(737, 400)
        Me.Controls.Add(Me.stcConsole)
        Me.Name = "MainForm"
        Me.Text = "Rebex Simple Telnet Winform Client"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ''' <summary>
    ''' Handles the form load event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' show connection dialog
        Dim connection As New Connection(_configFile)

        ' if not confirmed, exit the application
        If connection.ShowDialog() <> DialogResult.OK Then
            Close()
            Return
        End If

        ' initialize connection information with the values from the connection dialog
        stcConsole.ServerName = connection.HostName

        ' try connecting
        Try
            stcConsole.Connect()
        Catch ex As Exception
            MessageBox.Show(String.Format("Exception occured: {0}", ex))
            Close()
            Return
        End Try
    End Sub
End Class
