using InABox.Clients;
using System;
using System.Linq;

namespace InABox.Core
{
    internal class PosterTypeLookup : LookupGenerator<object>
    {
        public PosterTypeLookup(object[]? items) : base(items)
        {
            var settings = (items?.FirstOrDefault() as PostableSettings)!;

            var postableType = CoreUtils.GetEntity(settings.PostableType);

            foreach (var entity in PosterUtils.GetPosters())
                if (ClientFactory.IsSupported(entity)
                    && entity.GetInterfaceDefinition(typeof(IPoster<,>))!.GenericTypeArguments[0] == postableType)
                {
                    AddValue(
                        entity.EntityName(),
                        entity.GetCaptionOrNull(true)
                            ?? entity.GetInterfaces()
                                .Where(x => x.HasInterface(typeof(IPoster<,>)))
                                .Select(x => x.GetCaptionOrNull(true)).FirstOrDefault(x => x != null)
                            ?? entity.EntityName().Split('.').Last());
                }
        }
    }
}