using System;
using System.Collections.Generic;
using NUnit.Framework;
using SmartStore.ComponentModel;

namespace SmartStore.Core.Tests
{
	[TestFixture]
	public class MiniMapperTest
	{
		[Test]
		public void CanMap1()
		{
			var from = new MapClass1
			{
				Prop1 = "Prop1",
				Prop2 = "Prop2",
				Prop3 = "Prop3"
			};
			from.Address.FirstName = "John";
			from.Address.LastName = "Doe";
			from.Address.Age = 24;

			var to = MiniMapper.Map<MapClass1, MapClass2>(from);

			Assert.AreEqual(from.Prop1, to.Prop1);
			Assert.AreEqual(from.Prop2, to.Prop2);
			Assert.AreEqual(from.Prop3, to.Prop3);

			var dict = to.Address;
			Assert.AreEqual(dict.Count, 3);
			Assert.AreEqual(dict["FirstName"], from.Address.FirstName);
			Assert.AreEqual(dict["LastName"], from.Address.LastName);
			Assert.AreEqual(dict["Age"], from.Address.Age);
		}

		[Test]
		public void CanMap2()
		{
			var from = new MapClass2
			{
				Prop1 = "Prop1",
				Prop2 = "Prop2",
				Prop3 = "Prop3"
			};
			from.Address["FirstName"] = "John";
			from.Address["LastName"] = "Doe";
			from.Address["Age"] = 24;

			var to = MiniMapper.Map<MapClass2, MapClass1>(from);

			Assert.AreEqual(from.Prop1, to.Prop1);
			Assert.AreEqual(from.Prop2, to.Prop2);
			Assert.AreEqual(from.Prop3, to.Prop3);

			var dict = from.Address;
			Assert.AreEqual(dict.Count, 3);
			Assert.AreEqual(dict["FirstName"], to.Address.FirstName);
			Assert.AreEqual(dict["LastName"], to.Address.LastName);
			Assert.AreEqual(dict["Age"], to.Address.Age);
		}

		[Test]
		public void CanMapAnonymousType()
		{
			var from = new
			{
				Prop1 = "Prop1",
				Prop2 = "Prop2",
				Prop3 = "Prop3",
				Address = new
				{
					FirstName = "John",
					LastName = "Doe",
					Age = 24
				}
			};

			var to = new MapClass2();
			MiniMapper.Map(from, to);

			Assert.AreEqual(from.Prop1, to.Prop1);
			Assert.AreEqual(from.Prop2, to.Prop2);
			Assert.AreEqual(from.Prop3, to.Prop3);

			var dict = to.Address;
			Assert.AreEqual(dict.Count, 3);
			Assert.AreEqual(dict["FirstName"], from.Address.FirstName);
			Assert.AreEqual(dict["LastName"], from.Address.LastName);
			Assert.AreEqual(dict["Age"], from.Address.Age);
		}
	}

	public class MapClass1
	{
		public string Prop1 { get; set; }
		public string Prop2 { get; set; }
		public string Prop3 { get; set; }
		public MapNestedClass Address { get; set; } = new MapNestedClass();
	}

	public class MapClass2
	{
		public string Prop1 { get; set; }
		public string Prop2 { get; set; }
		public string Prop3 { get; set; }
		public IDictionary<string, object> Address { get; set; } = new Dictionary<string, object>();
	}

	public class MapNestedClass
	{
		public string FirstName { get; set; }
		public string LastName { get; set; }
		public int Age { get; set; }
	}
}
