using System;

namespace InABox.Core
{
    public abstract class DFLayoutField : DFLayoutControl
    {
        public DFLayoutField()
        {
        }

        [CodeEditor(Editable = Editable.Disabled)]
        [EditorSequence(-999)]
        public string Name { get; set; }

        protected override string GetDescription()
        {
            return Name;
        }

        protected override void LoadProperties()
        {
            base.LoadProperties();
            Name = GetProperty("Name", "");
        }

        protected override void SaveProperties()
        {
            base.SaveProperties();
            SetProperty("Name", Name);
        }

        public abstract TValue GetPropertyValue<TValue>(string name);

        public abstract object? ParseValue(object? value);

        public abstract DFLayoutFieldProperties GetProperties();
    }

    public class DFLayoutField<T> : DFLayoutField where T : DFLayoutFieldProperties, new()
    {
        public DFLayoutField()
        {
            Properties = new T();
        }

        public T Properties { get; }

        public override DFLayoutFieldProperties GetProperties() => Properties;

        public override TValue GetPropertyValue<TValue>(string name)
        {
            try
            {
                return (TValue)CoreUtils.GetPropertyValue(Properties, name);
            }
            catch (Exception)
            {
                return default;
            }
        }

        public override object? ParseValue(object? value)
        {
            return Properties.ParseValue(value);
        }
    }
}