using System;
using MileageTraker.Web.Models;
using MileageTraker.Web.ViewModels.Log;
using NUnit.Framework;

namespace Web.Tests.ViewModels.Log
{
	[TestFixture]
	public class LogViewModelTests
	{
		[Test]
		public void GetLog_Converts_To_Log()
		{
			var logId = 1;
			var cityName = "My Town";
			var date = DateTime.Today;
			var endOdometer = "1234";
			var driver = "name";
			var ethraId = "4567";
			string gasPurchased = "2.546";
			var mileageLogType = new MileageLogTypeWrapper { Enum = MileageLogType.Commuting };

			var viewModel =
				new LogViewModel
				{
					LogId = logId,
					CityName = cityName,
					Date = date,
					EndOdometer = endOdometer,
					UserFullName = driver,
					VehicleId = ethraId,
					GasPurchased = gasPurchased,
					LogType = mileageLogType
				};

			var log = viewModel.GetLog();

			Assert.That(log.LogId, Is.EqualTo(logId));
			Assert.That(log.CityName, Is.EqualTo(cityName));
			Assert.That(log.Date, Is.EqualTo(date));
			Assert.That(log.EndOdometer.ToString(), Is.EqualTo(endOdometer));
			Assert.That(log.VehicleId, Is.EqualTo(ethraId));
			Assert.That(log.GasPurchased.ToString(), Is.EqualTo(gasPurchased));
			Assert.That(log.LogType.Enum, Is.EqualTo(mileageLogType.Enum));
		}

		[Test]
		public void UpdateLog_Converts_To_Log()
		{
			var viewModelLogId = 1;
			var viewModelCityName = "My Town";
			var viewModelDate = DateTime.Today;
			var viewModelEndOdometer = "1234";
			var viewModelDriver = "name";
			var viewModelVehicleId = "4567";
			string viewModelGasPurchased = "2.546";
			var viewModelLogType = new MileageLogTypeWrapper { Enum = MileageLogType.Commuting };

			var viewModel =
				new LogViewModel
				{
					LogId = viewModelLogId,
					CityName = viewModelCityName,
					Date = viewModelDate,
					EndOdometer = viewModelEndOdometer,
					UserFullName = viewModelDriver,
					VehicleId = viewModelVehicleId,
					GasPurchased = viewModelGasPurchased,
					LogType = viewModelLogType
				};

			var logUser = new User{Username = "log"};
			var logSource = "here";
			var log =
				new MileageTraker.Web.Models.Log
				{
					LogId = 2,
					CityName = "Different Town",
					Date = DateTime.MinValue,
					EndOdometer = 1,
					VehicleId = "1",
					GasPurchased = 1.0,
					LogType = new MileageLogTypeWrapper { Enum = MileageLogType.GasPurchase },
					User = logUser,
					Source = logSource
				};

			viewModel.SetProperties(log);

			Assert.That(log.LogId, Is.EqualTo(viewModelLogId));
			Assert.That(log.CityName, Is.EqualTo(viewModelCityName));
			Assert.That(log.Date, Is.EqualTo(viewModelDate));
			Assert.That(log.EndOdometer.ToString(), Is.EqualTo(viewModelEndOdometer));
			Assert.That(log.VehicleId, Is.EqualTo(viewModelVehicleId));
			Assert.That(log.GasPurchased.ToString(), Is.EqualTo(viewModelGasPurchased));
			Assert.That(log.LogType.Enum, Is.EqualTo(viewModelLogType.Enum));

			Assert.That(log.User, Is.EqualTo(logUser));
			Assert.That(log.Source, Is.EqualTo(logSource));
		}

		[Test]
		public void Constructor_FromLog()
		{
			var logId = 1;
			var cityName = "My Town";
			var date = DateTime.Today;
			var endOdometer = 1234;
			var driver = "name";
			var ethraId = "4567";
			var gasPurchased = 2.546;
			var mileageLogType = new MileageLogTypeWrapper { Enum = MileageLogType.Commuting };

			var log =
				new MileageTraker.Web.Models.Log
				{
					LogId = logId,
					CityName = cityName,
					Date = date,
					EndOdometer = endOdometer,
					VehicleId = ethraId,
					GasPurchased = gasPurchased,
					LogType = mileageLogType
				};

			var viewModel = new LogViewModel(log);

			Assert.That(viewModel.LogId, Is.EqualTo(logId));
			Assert.That(viewModel.CityName, Is.EqualTo(cityName));
			Assert.That(viewModel.Date, Is.EqualTo(date));
			Assert.That(viewModel.EndOdometer, Is.EqualTo(endOdometer.ToString()));
			Assert.That(viewModel.VehicleId, Is.EqualTo(ethraId));
			Assert.That(viewModel.GasPurchased, Is.EqualTo(gasPurchased.ToString()));
			Assert.That(viewModel.LogType.Enum, Is.EqualTo(mileageLogType.Enum));
		}
	}
}
