'  
'   Rebex Sample Code License
' 
'   Copyright 2025, 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.IO
Imports System.Windows.Forms
Imports System.Xml
Imports System.Collections.Generic
Imports System.ComponentModel

Public Partial Class ConnectionForm
    Inherits Form
    Private ReadOnly _data As ConnectionFormData

    Public ReadOnly Property Data() As ConnectionFormData
        Get
            Return _data
        End Get
    End Property

    Public Sub New()
        Me.New(New ConnectionFormData(FileTransferComponent.FileTransfer))
        If LicenseManager.UsageMode <> LicenseUsageMode.Designtime Then
            Throw New InvalidOperationException("Don't use the parameter-less constructor. It is intended for Visual Studio design mode only.")
        End If
    End Sub

    Public Sub New(data As ConnectionFormData)
        If data Is Nothing Then
            Throw New ArgumentNullException()
        End If

        _data = data

        InitializeComponent()

        ucConnectionEditor.HideFtpGui = (_data.SupportedComponents And FileTransferComponent.Ftp) = 0
        ucConnectionEditor.HideSftpGui = (_data.SupportedComponents And FileTransferComponent.Sftp) = 0

        ucConnectionEditor.AdaptToCurrentVisibility()

        ConnectionEditorUtils.BindComboWithEnum(Of LogLevel)(cmbLogLevel, Nothing)

        SetData()
    End Sub

    ''' <summary>
    ''' Propagate current form fields values to the <see cref="Data"/> object.
    ''' </summary>
    Private Sub GetData()
        ucConnectionEditor.GetData(_data)
        _data.LogLevel = CType(cmbLogLevel.SelectedValue, LogLevel)
    End Sub

    ''' <summary>
    ''' Propagate current <see cref="Data"/> values to form fields.
    ''' </summary>
    Private Sub SetData()
        ucConnectionEditor.SetData(_data)
        cmbLogLevel.SelectedValue = _data.LogLevel
    End Sub

    Private Sub btnConnect_Click(sender As Object, e As EventArgs)
        Dim errors As List(Of String) = ucConnectionEditor.GetValidationErrors()
        If errors.Count > 0 Then
            Dim msg As String = String.Join(vbCr & vbLf, errors.ToArray())
            ConnectionEditorUtils.ShowWarning(msg, "Invalid data")
            Return
        End If

        If Not ucConnectionEditor.EnsurePrivateKey() Then
            Return
        End If
        If Not ucConnectionEditor.EnsureClientCertificate() Then
            Return
        End If

        DialogResult = DialogResult.OK
        Close()
    End Sub

    Private Sub ConnectionForm_KeyPress(sender As Object, e As KeyPressEventArgs)
        ' when 'esc' is pressed the form is closed
        If (e.KeyChar) = Convert.ToChar(Keys.Escape) Then
            DialogResult = DialogResult.Cancel
            Close()
        End If
    End Sub

    Private Sub ConnectionForm_FormClosing(sender As Object, e As FormClosingEventArgs)
        ' we gather values from form-fields to the Data object regarles the closing reason
        GetData()
    End Sub

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

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

    Private ucConnectionEditor As UcConnectionEditor
    Private label1 As System.Windows.Forms.Label
    Private cmbLogLevel As System.Windows.Forms.ComboBox
    Private btnConnect As System.Windows.Forms.Button

#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.label1 = New System.Windows.Forms.Label()
        Me.cmbLogLevel = New System.Windows.Forms.ComboBox()
        Me.btnConnect = New System.Windows.Forms.Button()
        Me.ucConnectionEditor = New UcConnectionEditor()
        Me.SuspendLayout()
        ' 
        ' label1
        ' 
        Me.label1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.label1.AutoSize = True
        Me.label1.Location = New System.Drawing.Point(9, 355)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(73, 13)
        Me.label1.TabIndex = 1
        Me.label1.Text = "Logging level:"
        ' 
        ' cmbLogLevel
        ' 
        Me.cmbLogLevel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.cmbLogLevel.FormattingEnabled = True
        Me.cmbLogLevel.DropDownStyle = ComboBoxStyle.DropDownList
        Me.cmbLogLevel.Location = New System.Drawing.Point(119, 352)
        Me.cmbLogLevel.Name = "cmbLogLevel"
        Me.cmbLogLevel.Size = New System.Drawing.Size(121, 21)
        Me.cmbLogLevel.TabIndex = 2
        ' 
        ' btnConnect
        ' 
        Me.btnConnect.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnConnect.Location = New System.Drawing.Point(557, 350)
        Me.btnConnect.Name = "btnConnect"
        Me.btnConnect.Size = New System.Drawing.Size(75, 23)
        Me.btnConnect.TabIndex = 3
        Me.btnConnect.Text = "Connect"
        Me.btnConnect.UseVisualStyleBackColor = True
        AddHandler Me.btnConnect.Click, New System.EventHandler(AddressOf Me.btnConnect_Click)
        ' 
        ' ucConnectionEditor
        ' 
        Me.ucConnectionEditor.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ucConnectionEditor.ClearCommandChannel = False
        Me.ucConnectionEditor.ClientCertificate = Nothing
        Me.ucConnectionEditor.ClientCertificateFilename = Nothing
        Me.ucConnectionEditor.FileTransferMode = Net.FileTransferMode.Ftp
        Me.ucConnectionEditor.Location = New System.Drawing.Point(12, 12)
        Me.ucConnectionEditor.MinimumSize = New System.Drawing.Size(620, 0)
        Me.ucConnectionEditor.Name = "ucConnectionEditor"
        Me.ucConnectionEditor.PassiveMode = False
        Me.ucConnectionEditor.PrivateKey = Nothing
        Me.ucConnectionEditor.PrivateKeyFilename = ""
        Me.ucConnectionEditor.Protocol = CType(((Rebex.Net.TlsVersion.TLS13 Or Rebex.Net.TlsVersion.TLS12)), Rebex.Net.TlsVersion)
        Me.ucConnectionEditor.ProxyHost = ""
        Me.ucConnectionEditor.ProxyPassword = ""
        Me.ucConnectionEditor.ProxyPort = 0
        Me.ucConnectionEditor.ProxyType = FileTransferProxyType.None
        Me.ucConnectionEditor.ProxyUser = ""
        Me.ucConnectionEditor.ServerCertificateThumbprint = ""
        Me.ucConnectionEditor.ServerCertificateVerifyingMode = CertificateVerifyingMode.LocalyStoredThumbprint
        Me.ucConnectionEditor.ServerDomain = ""
        Me.ucConnectionEditor.ServerHost = ""
        Me.ucConnectionEditor.ServerKeyAlgorithm = Net.SshHostKeyAlgorithm.RSA
        Me.ucConnectionEditor.ServerKeyFingerprint = ""
        Me.ucConnectionEditor.ServerKeyHashAlgorithm = Security.Certificates.SignatureHashAlgorithm.MD5
        Me.ucConnectionEditor.ServerKeyVerifyingMode = KeyVerifyingMode.LocalyStoredFingerprint
        Me.ucConnectionEditor.ServerPassword = ""
        Me.ucConnectionEditor.ServerPort = 0
        Me.ucConnectionEditor.ServerUser = ""
        Me.ucConnectionEditor.Size = New System.Drawing.Size(620, 334)
        Me.ucConnectionEditor.StorePassword = False
        Me.ucConnectionEditor.TabIndex = 0
        Me.ucConnectionEditor.UseGssApi = False
        ' 
        ' ConnectionForm
        ' 
        Me.AcceptButton = Me.btnConnect
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(644, 382)
        Me.Controls.Add(Me.btnConnect)
        Me.Controls.Add(Me.cmbLogLevel)
        Me.Controls.Add(Me.label1)
        Me.Controls.Add(Me.ucConnectionEditor)
        Me.KeyPreview = True
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.MinimumSize = New System.Drawing.Size(660, 39)
        Me.Name = "ConnectionForm"
        Me.ShowIcon = False
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Connection"
        AddHandler Me.FormClosing, New System.Windows.Forms.FormClosingEventHandler(AddressOf Me.ConnectionForm_FormClosing)
        AddHandler Me.KeyPress, New System.Windows.Forms.KeyPressEventHandler(AddressOf Me.ConnectionForm_KeyPress)
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region
End Class
