using Gremlin.GremlinData.DBClasses;
using Gremlin.GremlinData.EntityClasses;
using System.Diagnostics;
using System.Linq;

namespace Gremlin.GremlinUI.ViewModels
{
    internal class CustomDescriptionVM
    {
        public string Heading { get; set; }
        public string DescriptionText { get; set; }
        public string CoverletterText { get; set; }
        public string Notes { get; set; }    //Hinweise, Tipps, Caveats, etc. für Konfiguration, Verwendung, Best Practice usw.

        public override string ToString() => $"{Heading}";

        internal static CustomDescriptionVM GetCustomDescription(LineItemVM lineItemVM)
        {
            using (GremlinContext gremlinContext = new())
            {
                CustomDescriptionVM customDescriptionVM = new();
                CustomDescription customDescription = gremlinContext.CustomDescriptions.Where(cD => cD.ProductNumber == lineItemVM.ProductNumber && cD.OptionNumber == lineItemVM.OptionNumber)
                                                                                       .FirstOrDefault();

                if (customDescription != null)
                {
                    customDescriptionVM.Heading = customDescription.Heading;
                    customDescriptionVM.DescriptionText = customDescription.DescriptionText;
                    customDescriptionVM.CoverletterText = customDescription.CoverletterText;
                    customDescriptionVM.Notes = customDescription.Notes;

                    return customDescriptionVM;
                }
                else
                {
                    Debug.WriteLine($"CustomDescription für {lineItemVM.ProductNumber}#{lineItemVM.OptionNumber} nicht vorhanden! Verwende (vorläufig) Standardbeschreibung {lineItemVM.SapShortDescription}!");
                    customDescriptionVM.DescriptionText = lineItemVM.SapShortDescription;
                    customDescriptionVM.Heading = lineItemVM.SapShortDescription;
                }

                return customDescriptionVM;
            }
        }
    }
}
