using Gremlin.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Gremlin.GremlinUI.ViewModels
{
    internal class QuoteVM : BaseVM
    {
        public List<LineItemVM> LineItemsVM { get; private set; }
        public ContactVM Recipient { get; set; }
        public ContactVM SalesRep { get; private set; }
        public string QuoteNumber { get; set; }
        public string QuoteType { get; set; }
        public string QuotePath { get; set; }
        public int Warranty { get; set; }
        public int Validity { get; set; }
        public decimal TotalListprice { get; private set; }
        public decimal AverageDiscount { get; private set; }
        public decimal TotalNet { get; private set; }
        public float VAT { get; private set; }
        public bool? Brutto { get; set; }
        public bool? SinglePrices { get; set; }
        public bool? Brochures { get; set; }
        public bool? DataSheets { get; set; }
        public bool? MailTemplate { get; set; }
        public bool QuoteContains3PP { get; private set; }
        public bool QuoteContainsRB { get; private set; }

        internal QuoteVM() { }

        private QuoteVM(string quoteNumber, string quoteType, ContactVM recipient, ContactVM salesRep, bool? brutto, float vAT, int warranty, int validity, string quotePath, bool? singlePrices, bool? brochures, bool? dataSheets, bool? mailTemplate)
        {
            QuoteNumber = quoteNumber;
            QuoteType = quoteType;
            Recipient = recipient;
            SalesRep = salesRep;
            Brutto = brutto;
            VAT = vAT;
            Warranty = warranty;
            Validity = validity;
            QuotePath = quotePath;
            SinglePrices = singlePrices;
            Brochures = brochures;
            DataSheets = dataSheets;
            MailTemplate = mailTemplate;
        }

        internal static QuoteVM CreateQuote(string quoteNumber, ContactVM recipient, ContactVM salesRep, float vAT = 19f, string quoteType = "ein Analysegerät", bool? brutto = true, int warranty = 12, bool? singlePrices = true, bool? brochures = true, bool? dataSheets = true, bool? mailTemplate = true, string quotePath = "", int validity = 60)
        {
            decimal totalListprice = 0, totalDiscount = 0, totalNet = 0;
            bool quoteContains3PP = false, quoteContainsRB = false;

            QuoteVM quoteVM = new(quoteNumber, quoteType, recipient, salesRep, brutto, vAT, warranty, validity, quotePath, singlePrices, brochures, dataSheets, mailTemplate);
            quoteVM.LineItemsVM = new();

            ObservableCollection<LineItemVM> lineItemsVM = LineItemVM.ReadLineItemsFromClipboard();
            foreach (LineItemVM lineItemVM in lineItemsVM)
            {
                totalListprice += lineItemVM.ListPrice;
                totalDiscount += lineItemVM.TotalDiscount;
                totalNet += lineItemVM.TotalNet;

                switch (lineItemVM.ProductLine)
                {
                    case "RB":
                        if (lineItemVM.ProductNumber != "R2005A") //kein wirkliches RB-Produkt
                            quoteContainsRB = true;
                        break;
                    case "3PP":
                        quoteContains3PP = true; break;
                }

                if (lineItemVM.OptionNumber.StartsWith("8D"))
                {
                    quoteVM.Warranty = int.Parse(lineItemVM.OptionNumber.Last().ToString()) * 12;
                }

                quoteVM.LineItemsVM.Add(lineItemVM);
            }

            quoteVM.TotalListprice = totalListprice;
            if (lineItemsVM.Count != 0)
            {
                quoteVM.AverageDiscount = (decimal)MathF.Round((float)(totalDiscount / lineItemsVM.Count), 3);
            }
            quoteVM.TotalNet = totalNet;
            quoteVM.QuoteContainsRB = quoteContainsRB;
            quoteVM.QuoteContains3PP = quoteContains3PP;

            return quoteVM;
        }
    }
}
