using Gremlin.GremlinData.DBClasses;
using Gremlin.GremlinData.EntityClasses;
using Gremlin.MVVM.ViewModels;
using Gremlin.MVVM.Views;
using System;
using System.Collections.Generic;

namespace Gremlin.MVVM.Models
{
	public class CustomDescriptionModel
	{
		public static CustomDescription SearchForProducts(LineItem angebot, List<CustomDescription> customDescriptions)
		{
			List<CustomDescription> resultCustomDescriptions = customDescriptions.FindAll(customDescription => customDescription.ProductNumber == angebot.ProductNumber);
			return resultCustomDescriptions.Find(customDescription => customDescription.OptionNumber == angebot.OptionNumber);
		}

		public static bool CreateNewCustomDescription(CustomDescriptionViewModel customDescriptionVM, LineItemViewModel lineItemVM)
		{
			if (lineItemVM.OptionNumber == "")
			{
				lineItemVM.OptionNumber = null;
			}

			CustomDescription customDescription = new()
			{
				AccountId = 1,
				ProductNumber = lineItemVM.ProductNumber,
				OptionNumber = lineItemVM.OptionNumber,
				DescriptionText = customDescriptionVM.DescriptionText,
				Heading = customDescriptionVM.Heading,
				CoverletterText = customDescriptionVM.Heading,
				DataCreationDate = DateTime.Now,
				DataModificationDate = DateTime.Now,
				DataValidFrom = DateTime.Now,
				DataValidUntil = DateTime.MaxValue,
				DataVersionNumber = 1,
				DataModificationByUser = "Gremlin",
				DataStatus = "Active",
				Notes = "Description has to be edited manually!"
			};

			CheckNewCustomDescription checkNewCustomDescription = new(lineItemVM, customDescriptionVM);
			if (checkNewCustomDescription.ShowDialog() == true)
			{
				customDescription.Heading = checkNewCustomDescription.CustomDescriptionVM.Heading;
				customDescription.CoverletterText = checkNewCustomDescription.CustomDescriptionVM.CoverletterText;
				customDescription.DescriptionText = checkNewCustomDescription.CustomDescriptionVM.DescriptionText;

				using (GremlinContext gremlinContext = new())
				{
					_ = gremlinContext.Add(customDescription);
					_ = gremlinContext.SaveChanges();
					return true;
				}
			}

			return false;
		}

	}
}