using System;
using System.IO;
using AutoMapper;
using InventoryTraker.Web.Services;
using NUnit.Framework;

namespace InventoryTraker.Web.Tests.Services
{
	[TestFixture]
	public class InventoryImporterTests
	{
		private readonly string _documentFolder =
			AppDomain.CurrentDomain.BaseDirectory + @"\Utilities\Documents\";

		private IMapper _mapper;

		[OneTimeSetUp]
		public void StartUp()
		{
			_mapper = AutoMapperConfig.Config.CreateMapper();
		}

		[Test]
		public void Import()
		{
			var fileInfo = new FileInfo(Path.Combine(_documentFolder, "InventoryData.xlsx"));

			var importer = new InventoryImporter(_mapper);

			var inventories = importer.Import(fileInfo);

			foreach (var inventory in inventories)
			{
				Console.WriteLine($"{inventory.Id} {inventory.ProgramName} {inventory.ShredReadyDate}");

			}
		}

		[Test]
		public void Import_BadDate()
		{
			var fileInfo = new FileInfo(Path.Combine(_documentFolder, "InventoryData-BadDate.xlsx"));

			var importer = new InventoryImporter(_mapper);

			var importException = Assert.Throws<ImportException>(() => importer.Import(fileInfo));

			Assert.That(importException.Message, Does.Contain("Date"));
		}

		[Test]
		public void Import_BadHeader()
		{
			var fileInfo = new FileInfo(Path.Combine(_documentFolder, "InventoryData-BadHeader.xlsx"));

			var importer = new InventoryImporter(_mapper);

			var importException = Assert.Throws<ImportException>(() => importer.Import(fileInfo));

			Assert.That(importException.Message, Does.Contain("Program Name"));
		}
	}
}
