using System;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using MileageTraker.Web.Context;
using MileageTraker.Web.Models;

namespace MileageTraker.Web.DAL
{
	public class MileageTrakerInitializer : 
//#if DEBUG
//		DropCreateDatabaseIfModelChanges<MileageTrakerContext>
//#else
		CreateDatabaseIfNotExists<MileageTrakerContext>
//#endif
	{
		private const string CreateIndex = "CREATE NONCLUSTERED INDEX [IDX_{1}] ON [{0}] ([{1}])";

		protected override void Seed(MileageTrakerContext context)
		{
			Action<string, string> createIndex =
				(table, column) => context.Database.ExecuteSqlCommand(string.Format(CreateIndex, table, column));

			createIndex("Log", "VehicleId");
			createIndex("Log", "EndOdometer");
			//createIndex("Log", "EmployeeName");
			//createIndex("Log", "CityName");
			createIndex("Log", "Date");
			createIndex("Log", "Created");
			//createIndex("Vehicle", "Assigned");

			var folder = HttpContext.Current.Server.MapPath("~/App_Data");
			var vehicles = VehicleImporter.Import(Path.Combine(folder, "2011ETHRAvehicles.xls"));
			foreach (var vehicle in vehicles)
			{
				context.Vehicles.Add(vehicle);
			}
			
			var cities = VehicleImporter.ImportCities(Path.Combine(folder, "Cities.xls"));
			foreach (var city in cities)
			{
				context.Cities.Add(city);
			}

			/* // make some random logs
			var random = new Random(10);
			foreach (var vehicle in vehicles.Where(v => v.Assigned != null))
			{
				var odom = 1000 + random.Next(20000);
				var purDateSplit = vehicle.PurDate.Split('/');
				var date = new DateTime(int.Parse("20" + purDateSplit[1]), int.Parse(purDateSplit[0]), 1);
				var commonLogType = random.Next(1)%2 == 0 ? MileageLogType.NonCommuting : MileageLogType.Commuting;
				var commonCity = cities.OrderBy(c => Guid.NewGuid()).First();

				while (date < DateTime.Today)
				{
					var gas = random.Next(4)%4 == 0 ? Math.Round(random.NextDouble()*15, 3) : 0;

					var logType = commonLogType;
					if (gas > 0 && random.Next(3) % 3 == 0)
						logType = MileageLogType.GasPurchase;
					else if (random.Next(6) % 6 == 0)
						logType = commonLogType == MileageLogType.NonCommuting ? MileageLogType.Commuting : MileageLogType.NonCommuting;

					var driver = vehicle.Assigned;
					if (random.Next(10)%10 == 0)
						driver = vehicles.Where(v => v.Assigned != null).OrderBy(x => Guid.NewGuid()).First().Assigned;

					var city = commonCity;
					if (random.Next(10)%10 == 0)
						city = cities.OrderBy(c => Guid.NewGuid()).First();

					var log = new Log
								{
									CityName = city.Name,
									Date = date,
									EndOdometer = odom,
									GasPurchased = gas,
									LogType = logType,
									VehicleId = vehicle.VehicleId,
									Source = "Autogenerated",
									UserAgent = "IE",
									UserHostAddress = "127.0.0.1",
									Created = date
								};
					context.Logs.Add(log);

					date += new TimeSpan(random.Next(15), 0, 0, 0);
					if (random.Next(10) % 10 == 0)
						date += new TimeSpan(random.Next(360) + 90, 0, 0, 0);
					odom += random.Next(25) + 25;
				}
			}
			context.SaveChanges(); */
		} 
	}
}