using System;
using System.IO;
using System.Linq;
using MileageTraker.Web.DAL;
using NUnit.Framework;

namespace Web.Tests.DAL
{
	[TestFixture]
	public class FuelmanCsvImporterTests
	{
		private const string LogFolder = @"DAL\FuelLogDocs\";
		
		[Test]
		public void Import_Gets_More_Than_One_Result()
		{
			var fuelLogs = FuelmanCsvImporter.Import(new FileInfo(LogFolder + "TRN85E_150837_2015-07-31_1C758D97D46F565.csv"));
			Assert.That(fuelLogs, Has.Count.EqualTo(191));
			foreach (var fuelLog in fuelLogs)
			{
				Console.WriteLine("{0} {1} {2} {3}", fuelLog.Date, fuelLog.DriverFullName, fuelLog.TagNumber, fuelLog.CityName);
			}
		}

		[Test]
		public void Import_Gets_All_Fields()
		{
			var fuelLogs = FuelmanCsvImporter.Import(new FileInfo(LogFolder + "OneLog.csv")).ToList();
			Assert.That(fuelLogs, Has.Count.EqualTo(1));

			var fuelLog = fuelLogs[0];
			Assert.That(fuelLog.Date, Is.EqualTo(new DateTime(2015,7,1, 8,8,0)));
			Assert.That(fuelLog.DriverFullName, Is.EqualTo("Sherry Seal"));
			Assert.That(fuelLog.TagNumber, Is.EqualTo("4584GA"));
			Assert.That(fuelLog.Odometer, Is.EqualTo(129368));
			Assert.That(fuelLog.CityName, Is.EqualTo("Mooresburg"));
			Assert.That(fuelLog.MPG, Is.EqualTo(19.2));
			Assert.That(fuelLog.GasPurchased, Is.EqualTo(2.442));
			Assert.That(fuelLog.TotalPrice, Is.EqualTo(4.99));
		}

		[Test]
		public void Import_Tsv()
		{
			var fuelLogs = FuelmanCsvImporter.Import(new FileInfo(LogFolder + "August2015Fuelman.txt"));
			Assert.That(fuelLogs, Has.Count.EqualTo(197));
			foreach (var fuelLog in fuelLogs)
			{
				Console.WriteLine("{0} {1} {2} {3}", fuelLog.Date, fuelLog.DriverFullName, fuelLog.TagNumber, fuelLog.CityName);
			}
		}

		[Test]
		public void Import_Bad_Format_Gets_No_Results()
		{
			var fuelLogs = FuelmanCsvImporter.Import(new FileInfo(LogFolder + "BadFormat.csv"));
			Assert.That(fuelLogs.Count, Is.EqualTo(0));
		}

		[Test]
		public void Import_NonFuel_Does_Not_Import()
		{
			var fuelLogs = FuelmanCsvImporter.Import(new FileInfo(LogFolder + "OneCarWashOneFuel.csv"));
			Assert.That(fuelLogs.Count, Is.EqualTo(1));
		}

		[Test]
		[ExpectedException(typeof(ImportException))]
		public void Import_BadDate()
		{
			FuelmanCsvImporter.Import(new FileInfo(LogFolder + "BadDate.csv"));
		}

		[Test]
		[ExpectedException(typeof(ImportException))]
		public void Import_MissingField()
		{
			FuelmanCsvImporter.Import(new FileInfo(LogFolder + "MissingField.csv"));
		}

		[Test]
		[ExpectedException(typeof(ImportException))]
		public void Import_No_File()
		{
			FuelmanCsvImporter.Import(new FileInfo(LogFolder + "No File Here.csv"));
		}

		[Test]
		[ExpectedException(typeof(ImportException))]
		public void Import_Xls()
		{
			FuelmanCsvImporter.Import(new FileInfo(@"DAL\LogDocs\" + "Logs.xls"));
		}
	}
}
