using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using BS.Shared.Services;

namespace BeWo.Service.Invoicing
{
	[IDSpecificClass(Identifier = "LWLBeWo67")]
	public class BeWo67InvoiceCreation : InvoiceCreation
	{
		private DateTime? accountingPeriodStart = null;
		private DateTime? accountingPeriodEnd = null;

		
		public override void SetInvoiceBaseData(int invoiceCounter, ServiceInvoice invoice, CostBearer costBearer, DateTimeSpan invoicePeriod,
			IList<CompactSupportConceptDC> supportConceptList)
		{
			if (invoicePeriod != null)
			{
				accountingPeriodStart = invoicePeriod.StartDate;
				accountingPeriodEnd = invoicePeriod.EndDate;
			}
			base.SetInvoiceBaseData(invoiceCounter, invoice, costBearer, invoicePeriod, supportConceptList);
		}

		public override void SetInvoiceId(ServiceInvoice invoice)
		{
			invoice.InvoiceBase.InvoiceId = "LWLBeWo67";
		}

		public override IList<InvoiceItem> CreateInvoiceItems(List<ServiceRecordDC> serviceRecordsInPeriod, DateTimeSpan invoicePeriod, SupportConceptApprovalPeriod scap, Calculations calc)
		{
			var list = base.CreateInvoiceItems(serviceRecordsInPeriod, invoicePeriod, scap, calc);

			//IList<InvoiceItem> plist = CreatePauschaleInvoiceItem(serviceRecordsInPeriod, scap, calc);
			//list.AddRange(plist);
			return list;
		}


		//private InvoiceItem CreatePauschaleInvoiceItem(List<ServiceRecordDC> serviceRecordsInPeriod, SupportConceptApprovalPeriod scap, Calculations calc)
		//{
			
		//	if (rf > 0)
		//	{
		//		decimal totalAmount = 0;
		//		DateTime? start = DateTime.MaxValue;
		//		DateTime? end = DateTime.MinValue;

		//		foreach (var iii in iiList)
		//		{
		//			if (iii.ItemPeriodStart < start)
		//				start = iii.ItemPeriodStart;
		//			if (iii.ItemPeriodEnd > end)
		//				end = iii.ItemPeriodEnd;
		//			totalAmount += iii.GrossAmountTotal ?? 0;
		//		}

		//		//zzgl. Fahr+Wegezeiten (20 %)
		//		var ii = new InvoiceItemDC();

		//		int rfFahrzeit = (int)Math.Min(20, rf);
		//		rf -= rfFahrzeit;

		//		ii.ItemPeriodStart = start;
		//		ii.ItemPeriodEnd = end;
		//		ii.RateFactor = rfFahrzeit;
		//		ii.AmountPerUnit = (totalAmount * rfFahrzeit) / 100;
		//		ii.UnitCount = 1;
		//		ii.AmountTotal = Math.Round(ii.AmountPerUnit.Value * ii.UnitCount.Value, 2, MidpointRounding.AwayFromZero);
		//		ii.GrossAmountTotal = ii.AmountTotal;
		//		ii.ItemDescription = "Fahrt- und Wegezeiten";
		//		iiList.Add(ii);

		//		//zzgl. Ausfallzeiten (5 %)	
		//		if (rf > 0)
		//		{
		//			ii = new InvoiceItemDC();

		//			ii.ItemPeriodStart = start;
		//			ii.ItemPeriodEnd = end;
		//			ii.RateFactor = rf;
		//			ii.AmountPerUnit = (totalAmount * rf) / 100;
		//			ii.UnitCount = 1;
		//			ii.AmountTotal = Math.Round(ii.AmountPerUnit.Value * ii.UnitCount.Value, 2, MidpointRounding.AwayFromZero);
		//			ii.GrossAmountTotal = ii.AmountTotal;
		//			ii.ItemDescription = "Ausfallzeiten Fehlkontakte";

		//			iiList.Add(ii);
		//		}
		//	}

		//	var invoiceItem = new InvoiceItem();
		//	int anzahlMonate = 0;
			
		//	if (!accountingPeriodStart.HasValue)
		//		accountingPeriodStart = scap.StartDate;
		//	if (!accountingPeriodEnd.HasValue)
		//		accountingPeriodEnd = scap.EndDate;

		//	if (accountingPeriodStart.HasValue && accountingPeriodEnd.HasValue)
		//	{
		//		DateTime s = accountingPeriodStart.Value;
		//		anzahlMonate = 1;
		//		while ((s = s.AddMonths(1)) < accountingPeriodEnd.Value)
		//		{
		//			anzahlMonate++;
		//		}
				
			
		//	}
			

		//	invoiceItem.AmountPerUnit = AMOUNT_PER_Month;
		//	invoiceItem.UnitCount = anzahlMonate;
		//	invoiceItem.AmountTotal = anzahlMonate * AMOUNT_PER_Month;
		//	invoiceItem.ItemPeriodStart = accountingPeriodStart;
		//	invoiceItem.ItemPeriodEnd = accountingPeriodEnd;
		//	invoiceItem.RateFactor = null;
		//	invoiceItem.GrossAmountTotal = invoiceItem.AmountTotal;
		//	invoiceItem.ItemDescription = "Pauschal monatlich";
		//	if (scap != null)
		//		invoiceItem.SupportConceptApprovalPeriodOid = scap.Oid;



		//	return invoiceItem;
		//}

    }
}
