'  
'   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 Rebex.TerminalEmulation
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Reflection
Imports System.Text

''' <summary>
''' A list box item that represents an executable script (from the Scripts folder)
''' </summary>
Public Class ListBoxItem
    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Private Set
            _name = value
        End Set
    End Property

    Private _typeName As String
    Public Property TypeName() As String
        Get
            Return _typeName
        End Get
        Private Set
            _typeName = value
        End Set
    End Property

    Private _type As Type
    Private _method As MethodInfo

    Public Sub New(type As Type)
        _type = type

        _method = type.GetMethod("Execute", BindingFlags.[Public] Or BindingFlags.[Static], Nothing, New Type() {GetType(Scripting)}, Nothing)

        TypeName = type.Name

        Dim attributes As Object() = type.GetCustomAttributes(GetType(DescriptionAttribute), False)
        If attributes.Length > 0 Then
            Name = DirectCast(attributes(0), DescriptionAttribute).Description
        Else
            Name = TypeName
        End If
    End Sub

    Public Sub Execute(scripting As Scripting)
        If _method IsNot Nothing Then
            _method.Invoke(Nothing, New Object() {scripting})
        End If
    End Sub

    Public Overrides Function ToString() As String
        Return Name
    End Function
End Class
