using System;
using System.Collections.Generic;
using System.Linq;
using Humanizer;
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())
				{
					AddInventory(context);

					context.SaveChanges();
				}

				if (!context.Customers.Any())
				{
					AddNewCustomers(context);

					AddExistingCustomers(context);

					AddTerminatedCustomers(context);

					context.SaveChanges();
				}
			}
		}

		private static void AddInventory(AppDbContext context)
		{
			var peanutButterSmooth = new InventoryType
			{
				ContainerType = "18 oz jars",
				Name = "Peanut Butter Smth 18",
				Id = "100395",
				UnitsPerCase = 12,
				PricePerCase = 14.55M,
				WeightPerCase = 13.5
			};
			context.InventoryTypes.Add(peanutButterSmooth);

			var beanInventoryType = new InventoryType
			{
				ContainerType = "#300 cans",
				Name = "Beans, Veg 300",
				Id = "100363",
				UnitsPerCase = 24,
				PricePerCase = 10.18M,
				WeightPerCase = 24
			};
			context.InventoryTypes.Add(beanInventoryType);

			var corn = new InventoryType
			{
				ContainerType = "#300 cans",
				Name = "Corn Kernel 300",
				Id = "100311",
				UnitsPerCase = 24,
				PricePerCase = 10.03M,
				WeightPerCase = 22.9
			};
			context.InventoryTypes.Add(corn);

			context.Inventories.Add(new Inventory
			{
				InventoryType = beanInventoryType,
				ExpirationDate = DateTime.Now.AddYears(1).AtMidnight(),
				Memo = "French cut",
				Quantity = 10,
				Transactions = new List<Transaction> { new Transaction { AddedQuantity = 10, Memo = "arrival", TransactionDate = DateTime.Now} }
			});
		}

		private static void AddTerminatedCustomers(AppDbContext context)
		{
			context.Customers.Add(new Customer
			{
				Name = "Arlo Seymour",
				HomeEmail = "Arlo@home.com",
				WorkEmail = "Arlo@work.com",
				WorkAddress = "123 Arlo Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Seymour Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(-90),
				TerminationDate = DateTime.Today.ToStartOfMonth().AddDays(5),
			});

			context.Customers.Add(new Customer
			{
				Name = "Porter Jakeman",
				HomeEmail = "Porter@home.com",
				WorkEmail = "Porter@work.com",
				WorkAddress = "123 Porter Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Jakeman Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(-75),
				TerminationDate = DateTime.Today.ToStartOfMonth().AddDays(10),
			});

			context.Customers.Add(new Customer
			{
				Name = "Edwyn Perry",
				HomeEmail = "Edwyn@home.com",
				WorkEmail = "Edwyn@work.com",
				WorkAddress = "123 Edwyn Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Perry Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(-45),
				TerminationDate = DateTime.Today.ToStartOfMonth().AddDays(15),
			});
		}

		private static void AddExistingCustomers(AppDbContext context)
		{
			context.Customers.Add(new Customer
			{
				Name = "Gosse Greene",
				HomeEmail = "Gosse@home.com",
				WorkEmail = "Gosse@work.com",
				WorkAddress = "123 Gosse Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Greene Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(-20),
				Risks = new List<Risk>()
				{
					new Risk{Title = "Considering vendor switch", Description = "His contract is expiring next month, and he's evaluating other vendors.  He likes the services we provide, but feels he is paying too much."}
				}
			});

			context.Customers.Add(new Customer
			{
				Name = "Warwick Rye",
				HomeEmail = "Warwick@home.com",
				WorkEmail = "Warwick@work.com",
				WorkAddress = "123 Warwick Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Rye Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(-15),
				Opportunities = new List<Opportunity>
				{
					new Opportunity{Title = "Expanding business", Description = "Warwick's business is booming.  He's considering acquiring a competitor.  If that happens, he'll need a *lot* of custom development to integrate the two systems."}
				}
			});

			context.Customers.Add(new Customer
			{
				Name = "Odell Dennel",
				HomeEmail = "Odell@home.com",
				WorkEmail = "Odell@work.com",
				WorkAddress = "123 Odell Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Dennel Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(-10),
				Risks = new List<Risk>
				{
					new Risk{Title = "Customer may not pay", Description = "Odell is not pleased with the solution we developed.  He has threatened to stop all future payments, including outstanding invoices for work that has already been performed."}
				}
			});
		}

		private static void AddNewCustomers(AppDbContext context)
		{
			context.Customers.Add(new Customer
			{
				Name = "John Doe",
				HomeEmail = "john@home.com",
				WorkEmail = "john@work.com",
				WorkAddress = "123 Main Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Second Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth()
			});

			context.Customers.Add(new Customer
			{
				Name = "Roy Irvine",
				HomeEmail = "roy@home.com",
				WorkEmail = "roy@work.com",
				WorkAddress = "123 Roy Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Irvine Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(5),
			});

			context.Customers.Add(new Customer
			{
				Name = "Vere Rowland",
				HomeEmail = "vere@home.com",
				WorkEmail = "vere@work.com",
				WorkAddress = "123 Vere Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Roland Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(10),
			});

			context.Customers.Add(new Customer
			{
				Name = "Zack Beasley",
				HomeEmail = "zack@home.com",
				WorkEmail = "zack@work.com",
				WorkAddress = "123 Zack Street\r\nSuite B\r\nNew York, NY 55555",
				HomeAddress = "321 Beasley Street\r\nApt 1205\r\nNew York, NY 55555",
				HomePhone = "(555) 123-4555",
				WorkPhone = "(555) 321-5444",
				CreateDate = DateTime.Today.ToStartOfMonth().AddDays(15),
				Opportunities = new List<Opportunity>
				{
					new Opportunity{Title = "Interested in on-site support", Description = "Zack likes the solution we developed for his business.  He's interested in our on-site support services, too."}
				}
			});
		}
	}
}