'  
'   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
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Imports Rebex.TerminalEmulation

''' <summary>
''' The Options class provides user interface for setting the terminal options.
''' </summary>
Public Class Options
    Inherits Form
    ''' <summary>
    ''' Helper class for working with Encoding combo box.
    ''' </summary>
    Private Class EncodingElement
        Public ReadOnly Property CodePage() As Integer
            Get
                Return _encoding.CodePage
            End Get
        End Property

        Public ReadOnly Property Encoding() As Encoding
            Get
                Return _encoding
            End Get
        End Property

        Private _encoding As Encoding

        Public Sub New(ByVal encodingName As String)
            _encoding = EncodingTools.GetEncoding(encodingName)
        End Sub

        Public Overloads Overrides Function ToString() As String
            Return String.Format("{0} {1}", _encoding.HeaderName, _encoding.EncodingName)
        End Function
    End Class

    ''' <summary>
    ''' Required designer variable.
    ''' </summary>
    Private components As System.ComponentModel.IContainer = Nothing
    Private cbEncoding As System.Windows.Forms.ComboBox
    Private label2 As System.Windows.Forms.Label
    Private btnOk As System.Windows.Forms.Button
    Private txtTerminalName As System.Windows.Forms.TextBox
    Private label1 As System.Windows.Forms.Label
    Private cbForceNonDestructiveBackspace As System.Windows.Forms.CheckBox
    Private cbIgnoreSubstituteCharacter As System.Windows.Forms.CheckBox
    Private cbImplicitCarriageReturnInLinefeed As System.Windows.Forms.CheckBox
    Private label3 As System.Windows.Forms.Label
    Private cbAutoWrapMode As System.Windows.Forms.ComboBox
    Private label4 As System.Windows.Forms.Label
    Private cbBackspaceWrapMode As System.Windows.Forms.ComboBox

    ''' <summary>
    ''' The TerminalOptions object.
    ''' </summary>
    Private _options As TerminalOptions
    Private btnCancel As System.Windows.Forms.Button

    ''' <summary>
    ''' Encoding names shown in combo box.
    ''' </summary>
    Private _encodingNames As String() = EncodingTools.GetEncodingNames()

    ''' <summary>
    ''' Initializes this class.
    ''' </summary>
    ''' <param name="options">Terminal options to be configured.</param>
    Public Sub New(ByVal options As TerminalOptions)
        _options = options

        InitializeComponent()

        FillComboBoxes()

        SetFormValues(options)
    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.cbEncoding = New System.Windows.Forms.ComboBox()
        Me.label2 = New System.Windows.Forms.Label()
        Me.btnOk = New System.Windows.Forms.Button()
        Me.txtTerminalName = New System.Windows.Forms.TextBox()
        Me.label1 = New System.Windows.Forms.Label()
        Me.cbForceNonDestructiveBackspace = New System.Windows.Forms.CheckBox()
        Me.cbIgnoreSubstituteCharacter = New System.Windows.Forms.CheckBox()
        Me.cbImplicitCarriageReturnInLinefeed = New System.Windows.Forms.CheckBox()
        Me.label3 = New System.Windows.Forms.Label()
        Me.cbAutoWrapMode = New System.Windows.Forms.ComboBox()
        Me.label4 = New System.Windows.Forms.Label()
        Me.cbBackspaceWrapMode = New System.Windows.Forms.ComboBox()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        ' 
        ' cbEncoding
        ' 
        Me.cbEncoding.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbEncoding.Location = New System.Drawing.Point(120, 32)
        Me.cbEncoding.Name = "cbEncoding"
        Me.cbEncoding.Size = New System.Drawing.Size(248, 21)
        Me.cbEncoding.TabIndex = 1
        ' 
        ' label2
        ' 
        Me.label2.AutoSize = True
        Me.label2.Location = New System.Drawing.Point(5, 35)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(55, 16)
        Me.label2.TabIndex = 23
        Me.label2.Text = "Encoding:"
        ' 
        ' btnOk
        ' 
        Me.btnOk.Location = New System.Drawing.Point(232, 152)
        Me.btnOk.Name = "btnOk"
        Me.btnOk.Size = New System.Drawing.Size(63, 23)
        Me.btnOk.TabIndex = 7
        Me.btnOk.Text = "OK"
        AddHandler Me.btnOk.Click, AddressOf btnOk_Click
        ' 
        ' txtTerminalName
        ' 
        Me.txtTerminalName.Location = New System.Drawing.Point(120, 6)
        Me.txtTerminalName.Name = "txtTerminalName"
        Me.txtTerminalName.Size = New System.Drawing.Size(248, 20)
        Me.txtTerminalName.TabIndex = 0
        Me.txtTerminalName.Text = ""
        ' 
        ' label1
        ' 
        Me.label1.AutoSize = True
        Me.label1.Location = New System.Drawing.Point(5, 9)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(83, 16)
        Me.label1.TabIndex = 26
        Me.label1.Text = "Terminal name:"
        ' 
        ' cbForceNonDestructiveBackspace
        ' 
        Me.cbForceNonDestructiveBackspace.Location = New System.Drawing.Point(10, 115)
        Me.cbForceNonDestructiveBackspace.Name = "cbForceNonDestructiveBackspace"
        Me.cbForceNonDestructiveBackspace.Size = New System.Drawing.Size(214, 17)
        Me.cbForceNonDestructiveBackspace.TabIndex = 4
        Me.cbForceNonDestructiveBackspace.Text = "Force Non-destructive backspace"
        ' 
        ' cbIgnoreSubstituteCharacter
        ' 
        Me.cbIgnoreSubstituteCharacter.Location = New System.Drawing.Point(10, 138)
        Me.cbIgnoreSubstituteCharacter.Name = "cbIgnoreSubstituteCharacter"
        Me.cbIgnoreSubstituteCharacter.Size = New System.Drawing.Size(214, 17)
        Me.cbIgnoreSubstituteCharacter.TabIndex = 5
        Me.cbIgnoreSubstituteCharacter.Text = "Ignore Substitute character"
        ' 
        ' cbImplicitCarriageReturnInLinefeed
        ' 
        Me.cbImplicitCarriageReturnInLinefeed.Location = New System.Drawing.Point(10, 161)
        Me.cbImplicitCarriageReturnInLinefeed.Name = "cbImplicitCarriageReturnInLinefeed"
        Me.cbImplicitCarriageReturnInLinefeed.Size = New System.Drawing.Size(214, 17)
        Me.cbImplicitCarriageReturnInLinefeed.TabIndex = 6
        Me.cbImplicitCarriageReturnInLinefeed.Text = "Implicit Carriage return in Linefeed"
        ' 
        ' label3
        ' 
        Me.label3.AutoSize = True
        Me.label3.Location = New System.Drawing.Point(5, 62)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(58, 16)
        Me.label3.TabIndex = 33
        Me.label3.Text = "Auto wrap:"
        ' 
        ' cbAutoWrapMode
        ' 
        Me.cbAutoWrapMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbAutoWrapMode.Location = New System.Drawing.Point(120, 59)
        Me.cbAutoWrapMode.Name = "cbAutoWrapMode"
        Me.cbAutoWrapMode.Size = New System.Drawing.Size(109, 21)
        Me.cbAutoWrapMode.TabIndex = 2
        ' 
        ' label4
        ' 
        Me.label4.AutoSize = True
        Me.label4.Location = New System.Drawing.Point(5, 89)
        Me.label4.Name = "label4"
        Me.label4.Size = New System.Drawing.Size(90, 16)
        Me.label4.TabIndex = 35
        Me.label4.Text = "Backspace wrap:"
        ' 
        ' cbBackspaceWrapMode
        ' 
        Me.cbBackspaceWrapMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbBackspaceWrapMode.Location = New System.Drawing.Point(120, 86)
        Me.cbBackspaceWrapMode.Name = "cbBackspaceWrapMode"
        Me.cbBackspaceWrapMode.Size = New System.Drawing.Size(109, 21)
        Me.cbBackspaceWrapMode.TabIndex = 3
        ' 
        ' btnCancel
        ' 
        Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnCancel.Location = New System.Drawing.Point(304, 152)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(63, 23)
        Me.btnCancel.TabIndex = 36
        Me.btnCancel.Text = "Cancel"
        AddHandler Me.btnCancel.Click, AddressOf btnCancel_Click
        ' 
        ' Options
        ' 
        Me.AcceptButton = Me.btnOk
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.CancelButton = Me.btnCancel
        Me.ClientSize = New System.Drawing.Size(378, 187)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.label4)
        Me.Controls.Add(Me.label3)
        Me.Controls.Add(Me.label1)
        Me.Controls.Add(Me.label2)
        Me.Controls.Add(Me.cbBackspaceWrapMode)
        Me.Controls.Add(Me.cbAutoWrapMode)
        Me.Controls.Add(Me.cbImplicitCarriageReturnInLinefeed)
        Me.Controls.Add(Me.cbIgnoreSubstituteCharacter)
        Me.Controls.Add(Me.cbForceNonDestructiveBackspace)
        Me.Controls.Add(Me.txtTerminalName)
        Me.Controls.Add(Me.btnOk)
        Me.Controls.Add(Me.cbEncoding)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MinimizeBox = False
        Me.MaximizeBox = False
        Me.ShowInTaskbar = False
        Me.KeyPreview = True
        Me.Name = "Options"
        Me.Text = "Properties"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ''' <summary>
    ''' Fills the form's combo boxes.
    ''' </summary>
    Private Sub FillComboBoxes()
        Dim i As Integer
        For i = 0 To _encodingNames.Length - 1
            ' fill the cbEncoding.Items collection
            Dim encoding As New EncodingElement(_encodingNames(i))
            cbEncoding.Items.Add(encoding)
        Next

        cbAutoWrapMode.DataSource = [Enum].GetValues(GetType(AutoWrapMode))
        cbBackspaceWrapMode.DataSource = [Enum].GetValues(GetType(AutoWrapMode))
    End Sub

    ''' <summary>
    ''' Sets the form values according to the specified terminal options.
    ''' </summary>
    ''' <param name="options">Terminal options.</param>
    Private Sub SetFormValues(ByVal options As TerminalOptions)
        txtTerminalName.Text = options.TerminalName
        Me.Encoding = options.Encoding
        cbAutoWrapMode.SelectedItem = options.AutoWrapMode
        cbBackspaceWrapMode.SelectedItem = options.BackspaceWrapMode
        cbForceNonDestructiveBackspace.Checked = options.ForceNonDestructiveBackspace
        cbIgnoreSubstituteCharacter.Checked = options.IgnoreSubstituteCharacter
        cbImplicitCarriageReturnInLinefeed.Checked = options.ImplicitCarriageReturnInLinefeed
    End Sub

    ''' <summary>
    ''' Gets or sets the selected Encoding in the combo box.
    ''' </summary>
    Private Property Encoding() As Encoding
        Get
            Return CType(cbEncoding.SelectedItem, EncodingElement).Encoding
        End Get
        Set(ByVal Value As Encoding)
            cbEncoding.SelectedIndex = -1

            If Value Is Nothing Then
                Return
            End If

            Dim encoding As EncodingElement
            For Each encoding In cbEncoding.Items
                If encoding.CodePage = Value.CodePage Then
                    cbEncoding.SelectedItem = encoding
                    Return
                End If
            Next
        End Set
    End Property

    ''' <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)
        ' set the values from form to the terminal options
        _options.TerminalName = txtTerminalName.Text
        _options.Encoding = Me.Encoding
        _options.AutoWrapMode = CType(cbAutoWrapMode.SelectedValue, AutoWrapMode)
        _options.BackspaceWrapMode = CType(cbBackspaceWrapMode.SelectedValue, AutoWrapMode)
        _options.ForceNonDestructiveBackspace = cbForceNonDestructiveBackspace.Checked
        _options.IgnoreSubstituteCharacter = cbIgnoreSubstituteCharacter.Checked
        _options.ImplicitCarriageReturnInLinefeed = cbImplicitCarriageReturnInLinefeed.Checked

        Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Close()
    End Sub
End Class
