//  
//   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.
// 

using Rebex.TerminalEmulation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace Rebex.Samples
{
    /// <summary>
    /// A list box item that represents an executable script (from the Scripts folder)
    /// </summary>
    public class ListBoxItem
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            private set { _name = value; }
        }

        private string _typeName;
        public string TypeName
        {
            get { return _typeName; }
            private set { _typeName = value; }
        }

        private Type _type;
        private MethodInfo _method;

        public ListBoxItem(Type type)
        {
            _type = type;

            _method = type.GetMethod("Execute", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(Scripting) }, null);

            TypeName = type.Name;

            object[] attributes = type.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
                Name = ((DescriptionAttribute)attributes[0]).Description;
            else
                Name = TypeName;
        }

        public void Execute(Scripting scripting)
        {
            if (_method != null)
                _method.Invoke(null, new object[] { scripting });
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
