using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Evolving.DomainTypes;
using Evolving.TrueView;

using TestApplicationDomainModel;
using TestApplicationDomainModel.Objects;
using TestApplicationDomainModel.BasicTypes;
using TestApplicationDomainModel.Factories;

namespace TestApplication
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTestExample
    {

        public UnitTestExample()
        {

        }


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private void InitialiseTrueView()
        {
            var domainAssembly = typeof(PocoObject).Assembly;
            My.Application.Initialise();
            My.Application.RegisterAssembly(domainAssembly);

            // We've got a Configuration class within this assembly, so make sure it can be found:
            My.Application.RegisterAssembly(typeof(TestApplication.Configuration).Assembly);
        }

        [TestMethod]
        public void ShouldPersistObjectGraph()
        {
            this.InitialiseTrueView();

            var session = Evolving.TrueView.My.Session;

            //-----

            var category = new CategoryFactory().Create();
            category.Name = "Test Category";

            var money = new Money();
            category.Money = money;
            category.Money.Name = "Test Money";


            var subProductA = new SubProductA();
            category.Products.Add(subProductA);

            var subProductB = new SubProductB();
            category.Products.Add(subProductB);

            //-----

            session.Save(category);
            var categoryId = category.ID;
            Assert.AreNotEqual(category.ID, Guid.Empty);

            //-----

            var category2 = session.Load<Category>(category.ID);
            Assert.AreEqual(category.Name, category2.Name);
            this.CheckAuditDetailsMatch(category.Audit, category2.Audit);

            category2.Money = session.Load<Money>(category2, ExpressionUtil.ToPropertyInfo(() => category2.Money));
            Assert.AreEqual(category.Money.Name, category2.Money.Name);
            this.CheckAuditDetailsMatch(category.Money.Audit, category2.Money.Audit);

            var category2Products = session.Load<ObjectList<Product>>(category2, ExpressionUtil.ToPropertyInfo(() => category2.Products));
            Assert.AreEqual(category.Products.Count, category2Products.Count);
        }

        private void CheckAuditDetailsMatch(IAudit a, IAudit b)
        {
            Assert.AreEqual(a.CreatedAt, b.CreatedAt);
            Assert.AreEqual(a.CreatedBy, b.CreatedBy);
            Assert.AreEqual(a.UpdatedAt, b.UpdatedAt);
            Assert.AreEqual(a.UpdatedBy, b.UpdatedBy);
            Assert.AreEqual(a.DeletedAt, b.DeletedAt);
            Assert.AreEqual(a.DeletedBy, b.DeletedBy);
        }

    }
}

