using System;
using System.Linq;

namespace InABox.Core
{
    public class EnumLookupEditor : StaticLookupEditor
    {
        public EnumLookupEditor(Type type) : base(type)
        {
            if (!Type.IsEnum)
                throw new ArgumentException("T must be an enumerated type");
            LookupWidth = 120;
        }

        public override CoreTable Values(string columnname, BaseObject[]? items)
        {
            var result = new CoreTable();
            result.Columns.Add(new CoreColumn { ColumnName = columnname.Split('.').Last(), DataType = Type });
            result.Columns.Add(new CoreColumn { ColumnName = "Display", DataType = typeof(string) });
            foreach (var item in Enum.GetValues(Type))
            {
                var row = result.NewRow();
                row[columnname.Split('.').Last()] = item;
                row["Display"] = item.ToString().SplitCamelCase();
                result.Rows.Add(row);
            }

            return result;
        }

        public override void Clear()
        {
        }

        protected override BaseEditor DoClone()
        {
            return CloneStaticLookupEditor();
        }
    }
}