using System.Data.Entity;
using System.Linq;
using log4net;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Parsers;
using LeafWeb.Core.Utility;

namespace LeafWeb.Core.DAL
{
	public class LeafWebInitializer : CreateDatabaseIfNotExists<LeafWebContext>
	{
		private const string ContentDirectory = @"DAL\InitialData\";

		protected override void Seed(LeafWebContext context)
		{
			LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Debug("Seed");

			// get fluxnet sites from file
			var fileInfo = FileUtility.GetContentFile(ContentDirectory, "fluxnet_site_list_all_October2015_with_joins_corrected.csv", true);
			var fluxnetSiteCsvParser = new FluxnetSiteCsvParser(fileInfo);
			var fluxnetSites = fluxnetSiteCsvParser.Parse().ToList();

			// add to context
			fluxnetSites.ForEach(s => context.FluxnetSites.Add(s));
			
			var photosynthesisTypes = new []
			{
				new PhotosynthesisType {Id = "C3_photosynthesis_leafweb", Name = "C3 Photosynthesis", SortOrder = 1},
				new PhotosynthesisType {Id = "C4_photosynthesis_leafweb", Name = "C4 Photosynthesis", SortOrder = 2},
				new PhotosynthesisType {Id = "CAM_photosynthesis_leafweb", Name = "CAM Photosynthesis", SortOrder = 3}
			};

			context.PhotosynthesisTypes.AddRange(photosynthesisTypes);

			context.SaveChanges();

			base.Seed(context);
		}
	}
}