using StyletIoC.Creation;
using System;
using System.Linq.Expressions;

namespace StyletIoC.Internal.Creators
{
    /// <summary>
    /// Knows how to create an instance of an abstract factory (generated by Container.GetFactoryForType)
    /// </summary>
    internal class AbstractFactoryCreator : ICreator
    {
        private readonly Type abstractFactoryType;
        public Type Type
        {
            get { return this.abstractFactoryType; }
        }

        public AbstractFactoryCreator(Type abstractFactoryType)
        {
            this.abstractFactoryType = abstractFactoryType;
        }

        public Expression GetInstanceExpression(ParameterExpression registrationContext)
        {
            var ctor = this.abstractFactoryType.GetConstructor(new[] { typeof(IRegistrationContext) });
            var construction = Expression.New(ctor, registrationContext);
            return construction;
        }
    }
}
