using System;
using System.Linq;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MiscUtil.Collections;
using NUnit.Framework;
using ImportLogViewModel = MileageTraker.Web.ViewModels.Log.ImportLogViewModel;

namespace Web.Tests.Utility
{
	[TestFixture]
	public class CustomExtensionsTests
	{
		[Test]
		public void TimeSpan_ToVerboseStringHistoric_Yesterday()
		{
			var s = (DateTime.Today - DateTime.Today.AddDays(-1)).ToVerboseStringHistoric();

			Assert.That(s, Is.EqualTo("Yesterday"));
		}
	
		[Test]
		public void TimeSpan_ToVerboseStringHistoric_Today()
		{
			var s = (DateTime.Today - DateTime.Today).ToVerboseStringHistoric();

			Assert.That(s, Is.EqualTo("Today"));
		}
	
		[Test]
		public void TimeSpan_ToVerboseStringHistoric_TwoDaysAgo()
		{
			var s = (DateTime.Today - DateTime.Today.AddDays(-2)).ToVerboseStringHistoric();

			Assert.That(s, Is.EqualTo("2 days ago"));
		}

		[Test]
		public void TimeSpan_ToVerboseStringHistoric_OneWeekAgo()
		{
			var s = (DateTime.Today - DateTime.Today.AddDays(-7)).ToVerboseStringHistoric();

			Assert.That(s, Is.EqualTo("1 week ago"));
		}

		[Test]
		public void TimeSpan_ToVerboseStringHistoric_OneWeekOneDayAgo()
		{
			var s = (DateTime.Today - DateTime.Today.AddDays(-8)).ToVerboseStringHistoric();

			Assert.That(s, Is.EqualTo("1 week, 1 day ago"));
		}

		[Test]
		public void TimeSpan_ToVerboseStringHistoric_OneWeekTwoDaysAgo()
		{
			var s = (DateTime.Today - DateTime.Today.AddDays(-9)).ToVerboseStringHistoric();

			Assert.That(s, Is.EqualTo("1 week, 2 days ago"));
		}

		[Test]
		public void TimeSpan_ToVerboseStringHistoric_TwoWeeksAgo()
		{
			var s = (DateTime.Today - DateTime.Today.AddDays(-7)).ToVerboseStringHistoric();

			Assert.That(s, Is.EqualTo("1 week ago"));
		}

		[Test]
		public void Type_GetPropertyNames()
		{
			var propertyNames = (typeof (ImportLogViewModel).GetPropertyNames()).ToList();

			Assert.That(propertyNames, Has.Member("Vehicle ID"));
			Assert.That(propertyNames, Has.Count.GreaterThan(5));
		}

		[Test]
		public void Enum_ParseWithDisplayNames_Short()
		{
			var result = typeof (MileageLogType).ParseWithDisplayNames("Comm");

			Assert.That(result, Is.EqualTo(MileageLogType.Commuting));
		}

		[Test]
		public void Enum_ParseWithDisplayNames_Long()
		{
			var result = typeof (MileageLogType).ParseWithDisplayNames("Gas Purchase");

			Assert.That(result, Is.EqualTo(MileageLogType.GasPurchase));
		}

		[Test]
		public void Enum_ParseWithDisplayNames_Default()
		{
			var result = typeof (MileageLogType).ParseWithDisplayNames("GasPurchase");

			Assert.That(result, Is.EqualTo(MileageLogType.GasPurchase));
		}

		[Test]
		[ExpectedException(typeof(InvalidOperationException))]
		public void Enum_ParseWithDisplayNames_Invalid()
		{
			typeof (MileageLogType).ParseWithDisplayNames("None");
		}

		[Test]
		public void YearMonthGroupTest()
		{
			var range = new Range<DateTime>(new DateTime(2011, 2, 4), new DateTime(2013, 6, 23));
			var monthsBetween = range.FromStart(d => d.AddMonths(1));
			var yearMonthList = CustomExtensions.YearMonthList(monthsBetween);
			Assert.That(yearMonthList, Has.Count.EqualTo(3));
			Assert.That(yearMonthList["2013"], Has.Count.EqualTo(6));
		}
	}
}
