using System.Collections.Generic;
using System.IO;
using System.Linq;
using CsvHelper.Configuration;
using InventoryTraker.Web.Core;

namespace InventoryTraker.Web.Utilities
{
	public class InventoryTypeParser
	{
		private readonly FileSystemInfo _excelFile;

		private sealed class InventoryTypeMap : CsvClassMap<InventoryType>
		{
			public InventoryTypeMap()
			{
				Map(m => m.Identifier);
				Map(m => m.Name);
				Map(m => m.UnitsPerCase);
				Map(m => m.ContainerType);
				Map(m => m.WeightPerCase);
				Map(m => m.PricePerCase);
			}
		}

		public InventoryTypeParser(FileSystemInfo excelFile)
		{
			_excelFile = excelFile;
		}

		public IList<InventoryType> Parse()
		{
			var csvConfiguration = 
				new CsvConfiguration { IsHeaderCaseSensitive = false, IgnoreReadingExceptions = true};
			csvConfiguration.RegisterClassMap<InventoryTypeMap>();
			using (var reader = ExcelParserBase.OpenExcel(_excelFile, csvConfiguration))
			{
				return reader.GetRecords<InventoryType>().ToList();
			}
		}
	}
}