using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using ExcelLibrary.SpreadSheet;
using MileageTraker.Web.Models;

namespace MileageTraker.Web.DAL
{
	public static class UserImporter
	{
		public static List<User> Import(string filename)
		{
			var fileInfo = new FileInfo(filename);
			return Import(Workbook.Load(fileInfo.FullName)).ToList();
		}

		private static IEnumerable<User> Import(Workbook workbook)
		{
			var sheet = workbook.Worksheets[0];
			var first = true;
			for (var r = sheet.Cells.FirstRowIndex; r <= sheet.Cells.LastRowIndex; r++)
			{
				var row = sheet.Cells.GetRow(r);

				if (first)
				{
					first = false;
					continue;
				}

				var user = new User();
				user.FullName = row.GetCell(0).StringValue.Trim();
				user.Username = row.GetCell(1).StringValue.Trim();
				user.Email = row.GetCell(2).StringValue.Trim();
				user.IsApproved = string.IsNullOrEmpty(row.GetCell(3).StringValue);

				yield return user;
			}
		}
	}
}