using System;
using System.Collections.Generic;
using System.Linq;

namespace InABox.Core
{
    public class DigitalFormCategoryLookups : LookupGenerator<object>
    {
        private static Type[]? _formTypes;
        public static IEnumerable<Type> FormTypes
        {
            get
            {
                _formTypes ??= CoreUtils.Entities.Where(x => x.HasInterface<IDigitalForm>()).ToArray();
                return _formTypes;
            }
        }

        public Dictionary<string, string> Lookups()
        {
            var result = new Dictionary<string, string>(); // { { "", "Select Category" } };

            foreach (var type in FormTypes)
            {
                var forms = type.GetInterfaces().Where(x => x.GetInterfaces().Contains(typeof(IDigitalForm)) && x.IsGenericType);
                foreach (var form in forms)
                    foreach (var generic in form.GenericTypeArguments)
                        result[generic.EntityName().Split('.').Last()] = generic.GetCaption();
            }

            return result;
        }
        
        public DigitalFormCategoryLookups(object[]? items) : base(items)
        {
            var lookups = Lookups();
            foreach (var (key, value) in lookups)
                AddValue(key, value);
        }
    }
}