﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Runtime.InteropServices;
using Moq;

namespace Kata.Core.TestFactory
{
    public class BaseTestClass<TClass> where TClass: class
    {

        protected TClass Target;
        protected AutoMockContainer MockContainer;

        public BaseTestClass()
        {
            MockRepository factory = new MockRepository(MockBehavior.Loose);
            MockContainer = new AutoMockContainer(factory);

            Type type = typeof (TClass);
            var constructorInfo = type.GetConstructors()[0];
            var paramMockList = new List<object>();

            foreach (var parameterInfo in constructorInfo.GetParameters())
            {
                var paramType = parameterInfo.ParameterType;

                MethodInfo method = typeof (AutoMockContainer).GetMethod("GetMock");
                MethodInfo generic = method.MakeGenericMethod(paramType);
                var methodReturnVal = generic.Invoke(MockContainer, null);

                paramMockList.Add(((Mock)methodReturnVal).Object);
            }

            var instance = constructorInfo.Invoke(paramMockList.ToArray());
            Target = (TClass) instance;
            
        }

        public BaseTestClass(int number) :this()
        {
        } 
    }
}