using Gremlin.GremlinData.DBClasses;
using Gremlin.GremlinData.EntityClasses;
using Gremlin.MVVM.ViewModels;
using System;

namespace Gremlin.MVVM.Models
{
	public class QuoteModel
	{
		public static bool WriteToDB(QuoteViewModel quoteVM)
		{
			Quote quote = new()
			{
				ContactId = quoteVM.Recipient.ContactID,
				QuotationNumber = quoteVM.QuoteNumber,
				QuotationDate = DateTime.Now,
				ValidUntil = DateTime.Now.AddDays(quoteVM.Validity),
				ValidFor = (byte)quoteVM.Validity,
				TotalListprice = quoteVM.TotalListprice,
				//quote.TotalDiscount = 
				TotalNet = quoteVM.TotalNet,
				VAT = Convert.ToSingle(quoteVM.VAT),
				TotalGross = quoteVM.TotalGross,
				QuoteContains3PP = quoteVM.QuoteContains3PP,
				QuoteContainsRB = quoteVM.QuoteContainsRB,
				DataModificationByUser = "Gremlin",
				LineItems = LineItemModel.ConvertToLineItems(quoteVM.LineItemsVM),
				DataStatus = "Active"
			};

			try
			{
				using (GremlinContext gremlinContext = new())
				{
					_ = gremlinContext.Add(quote);
					_ = gremlinContext.SaveChanges();
				}
				return true;
			}
			catch (Exception ex)
			{
				ErrorHandler.ShowErrorInMessageBox(ex);
				return false;
			}
		}
	}
}