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

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

		// ReSharper disable once ClassNeverInstantiated.Local
		private sealed class InventoryAddFormMap : CsvClassMap<InventoryAddForm>
		{
			public InventoryAddFormMap()
			{
				Map(m => m.Id).Name("Box ID");
				Map(m => m.ProgramName).Name("Program Name");
				Map(m => m.ProgramSubtype).Name("Program Subtype");
				Map(m => m.Description);
				Map(m => m.ShredReadyDate).Name("Shread Ready Date");
				Map(m => m.AddedDate).Name("Added Date");
				Map(m => m.Memo);
			}
		}

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

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