using System;

namespace InABox.Core
{
    public interface IDFLayoutField
    {
        string Name { get; set; }
        DFLayoutFieldProperties GetProperties();
    }

    public abstract class DFLayoutField : DFLayoutControl, IDFLayoutField
    {
        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 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;
            }
        }
    }
}