using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BeWo.Data.Entities;
using BeWo.Service.Invoicing;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Services;

namespace SPZLeverkusen.Invoicing
{
	public class SpzLeverkusenInvoiceCreation : InvoiceCreation
	{
	    public override ServiceInvoice CreateSingleInvoice(int invoiceCounter, CostBearer costBearer, DateTimeSpan invoicePeriod,
	        CompactSupportConceptDC supportConcept, List<ServiceRecordDC> serviceRecords)
	    {
            var invoice = base.CreateSingleInvoice(invoiceCounter, costBearer, invoicePeriod, supportConcept, serviceRecords);
            SetAmountAdvancePayments(invoice);
            return invoice;
        }
        
        public override IList<InvoiceItem> CreateSummaryInvoiceItems(IList<InvoiceItem> itemList, DateTimeSpan invoicePeriod, SupportConceptApprovalPeriod scap,
			Calculations calc)
		{
			//Fasse InvoiceItems mit gleichem Stundensatz und Monat zusammen:

			var newlist = new List<InvoiceItem>();

			var monthToItem = new Dictionary<String, InvoiceItem>();
			foreach (InvoiceItem iitem in itemList)
			{
				InvoiceItem item;
				String key = String.Format("{0}_{1}_{2:MM.yyyy}", iitem.AmountPerUnit, iitem.RateFactor, iitem.ItemPeriodStart);

				if (monthToItem.ContainsKey(key))
				{
					item = monthToItem[key];
				}
				else
				{
					item = new InvoiceItem();
					newlist.Add(item);

					item.AmountPerUnit = iitem.AmountPerUnit;
					if (invoicePeriod != null)
					{
						item.ItemPeriodStart = invoicePeriod.StartDate;
						item.ItemPeriodEnd = invoicePeriod.EndDate;
					}
					else
					{
						item.ItemPeriodStart = scap.StartDate;
						item.ItemPeriodEnd = scap.EndDate;
					}
					item.RateFactor = iitem.RateFactor;

					item.AccountingInterval = iitem.AccountingInterval;
					item.ItemDescription = String.Format("Fachleistungsstunden {0:MMMM yy}", iitem.ItemPeriodStart);
					item.SupportConceptApprovalPeriodOid = scap.Oid;
					item.UnitDescription = "Std.";

					monthToItem[key] = item;
				}


				if (!item.UnitCount.HasValue)
				{
					item.UnitCount = 0;
				}
				item.UnitCount += iitem.UnitCount ?? 0;
			}


			foreach (InvoiceItem invoiceItem in newlist)
			{
				if (invoiceItem.AmountPerUnit.HasValue)
				{
					invoiceItem.AmountPerUnit = Math.Round(invoiceItem.AmountPerUnit.Value, 2, MidpointRounding.AwayFromZero);
				}
				else
				{
					invoiceItem.AmountPerUnit = 0;
				}

				if (invoiceItem.UnitCount.HasValue)
				{
					invoiceItem.UnitCount = Math.Round(invoiceItem.UnitCount.Value * (((invoiceItem.RateFactor ?? 0) + 100) / 100), 2, MidpointRounding.AwayFromZero);
				}
				else
				{
					invoiceItem.UnitCount = 0;
				}

				invoiceItem.AmountTotal = invoiceItem.AmountPerUnit * invoiceItem.UnitCount;
				invoiceItem.AmountTotal = Math.Round(invoiceItem.AmountTotal.Value, 2, MidpointRounding.AwayFromZero);

				invoiceItem.GrossAmountTotal = invoiceItem.AmountTotal;
				
			}
			return newlist;
		}
	}
}
