using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using InventoryTraker.Web.Core;
using InventoryTraker.Web.Data;
using InventoryTraker.Web.Identity;
using InventoryTraker.Web.Utilities;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace InventoryTraker.Web
{
	public static class SeedData
	{
		public static void Init()
		{
			using (var context = new AppDbContext())
			{
				if (!context.Users.Any())
				{
					var manager = new ApplicationUserManager(new UserStore<User>(context));
					manager.Create(new User
					{
						Email = "james.kolpack@gmail.com",
						UserName = "James Kolpack",
					}, "hgBdFiJTK");

					manager.Create(new User
					{
						Email = "bbanegas@ethra.org",
						UserName = "Brandi Banegas",
					}, "3v9qxe");

					manager.Create(new User
					{
						Email = "ccecil@ethra.org",
						UserName = "Cyndie Cecil",
					}, "95kdsxa");

					manager.Create(new User
					{
						Email = "knorton@ethra.org",
						UserName = "Kay Norton",
					}, "rt9pmz1");
				}

				if (!context.Inventories.Any())
				{
					AddInventoryTypes(context);

					context.SaveChanges();

					AddInventory(context);

					context.SaveChanges();
				}
			}
		}

		private static void AddInventoryTypes(AppDbContext context)
		{
			var folder = HttpContext.Current.Server.MapPath("~/App_Data");
			var inventoryTypeFile = Path.Combine(folder, "InventoryTypeSeedData.xlsx");
			var parser = new InventoryTypeParser(new FileInfo(inventoryTypeFile));
			foreach (var inventoryType in parser.Parse())
			{
				context.InventoryTypes.Add(inventoryType);
			}
		}

		private static void AddInventory(AppDbContext context)
		{
			var r = new Random(1);
			var inventoryTypes = context.InventoryTypes.ToList();
			var cityNames = new List<string> {"Maryville", "Wartburg", "Clinton", "Oak Ridge", "Alcoa", "Jellico"};
			var colorNames = new List<string> {"Red", "Purple", "Blue", "Yellow", "White"};

			for (int i = 0; i < 200; i++)
			{
				var inventoryType = inventoryTypes.ElementAt(r.Next(0, context.InventoryTypes.Count()));
				var addedDate = DateTime.Today.AddMonths(-r.Next(1, 24));
				var expiration = addedDate.AddMonths(r.Next(2, 48));
				var memo = 
					r.Next(0,3) > 0 
					? colorNames.ElementAt(r.Next(0, colorNames.Count)) + $" {inventoryType.ContainerType}"
					: string.Empty;
				var quantity = r.Next(5, 112);

				var previousTransaction = new Transaction
				{
					TransactionType = TransactionType.Added,
					AddedQuantity = quantity,
					Memo = "Arrival",
					CurrentQuantity = quantity,
					TransactionDate = addedDate,
					Timestamp = addedDate
				};
				var inventory = new Inventory
				{
					InventoryType = inventoryType,
					ExpirationDate = expiration,
					AddedDate = addedDate,
					Memo = memo,
					Quantity = quantity,
					Transactions = new List<Transaction> { previousTransaction}
				};
				context.Inventories.Add(inventory);

				for (int j = 0; j < 5 && previousTransaction.CurrentQuantity > 0; j++)
				{
					var transactionDate = previousTransaction.TransactionDate.AddDays(r.Next(1, 100));
					if (transactionDate >= DateTime.Today)
						break;

					Transaction transaction;
					if (transactionDate >= expiration)
					{
						if (r.Next(1, 3) == 1)
							break;

						transaction = new Transaction
						{
							TransactionType = TransactionType.Expired,
							RemovedQuantity = previousTransaction.CurrentQuantity,
							CurrentQuantity = 0,
							Inventory = inventory,
							Memo = $"Expired on {expiration.ToShortDateString()}",
							TransactionDate = transactionDate,
							Timestamp = transactionDate
						};
					}
					else
					{
						var quantityRemoved = r.Next(1, 100);
						if (quantityRemoved > previousTransaction.CurrentQuantity)
							quantityRemoved = previousTransaction.CurrentQuantity;

						var transMemo = $"Distributed to {cityNames.ElementAt(r.Next(0, cityNames.Count))}";
						var transType = TransactionType.Distributed;

						if (r.Next(1, 15) == 1)
						{
							transMemo = "Loss";
							transType = TransactionType.Loss;
						}

						transaction = new Transaction
						{
							TransactionType = transType,
							RemovedQuantity = quantityRemoved,
							CurrentQuantity = previousTransaction.CurrentQuantity - quantityRemoved,
							Inventory = inventory,
							Memo = transMemo,
							TransactionDate = transactionDate,
							Timestamp = transactionDate
						};
					}

					inventory.Quantity = transaction.CurrentQuantity;

					inventory.Transactions.Add(transaction);

					previousTransaction = transaction;
				}
			}
		}
	}
}