using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeWo.Data.Entities;
using BeWo.Service.Invoicing;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Services;

namespace LHHannover.Invoicing
{
	internal class HannoverSettlementInvoiceCreation : SettlementInvoiceCreation
	{
		public override bool CanCreateInvoiceTheOldWay(Settlement2DC si, CostBearer2SupportConcept c2s)
		{
			return false;
		}


		public override IList<InvoiceItemDC> CreateAdditionalInvoiceItems(AccountingPeriod ap)
		{
			var list = new List<InvoiceItemDC>();
			var ii = CreateTravelTimeInvoiceItemForAccountingPeriod(ap);
			if (ii != null)
			{
				list.Add(ii);
			}
			return list;
		}

		public virtual InvoiceItemDC CreateTravelTimeInvoiceItemForAccountingPeriod(AccountingPeriod accountingPeriod)
		{
			if (accountingPeriod.ApprovalUnit.HasValue && accountingPeriod.ApprovalUnit.Value == AccountingIntervalType.Weekly &&
			    accountingPeriod.ApprovedPerUnit.HasValue)
			{
				decimal approvedFlsWeekly = accountingPeriod.ApprovedPerUnit.Value;

				decimal anzahlKontakte = accountingPeriod.ServiceRecordsInPeriod.Count;
				decimal amountPerUnit = 32.22m;

				//Bis 1,5 Stunden max. 2 Kontakte
				//1,5 bis 2 Stunden max. 2 Kontakte
				//Ab 3 Stunden max. 4 Kontakte
				String itemDescription = "";
				if (approvedFlsWeekly <= 1)
				{
					itemDescription = "Fahrtk. ABW (1/Woche)";
				}
				else if (approvedFlsWeekly <= 1.5m)
				{
					itemDescription = "Fahrtk. ABW (1,5/Woche)";
				}
				else if (approvedFlsWeekly <= 2.5m)
				{
					itemDescription = "Fahrtk. ABW (1,5-2,5/Woche)";
				}
				else
				{
					itemDescription = "Fahrtk. ABW (4/Woche)";
				}
				InvoiceItemDC ii = new InvoiceItemDC();
				ii.AccountingInterval = accountingPeriod.AccountingInterval;
				ii.ItemPeriodStart = accountingPeriod.PeriodStart;
				ii.ItemPeriodEnd = accountingPeriod.PeriodEnd;
				ii.AmountPerUnit = amountPerUnit;
				ii.ItemDescription = itemDescription;
				ii.MaxApprovedAmount = accountingPeriod.MaxApprovedAmount;
				ii.MaxApprovedUnitCount = accountingPeriod.MaxApprovedUnitCount;
				ii.ApprovedPerUnit = accountingPeriod.ApprovedPerUnit;
				ii.ApprovalUnit = accountingPeriod.ApprovalUnit;


				ii.UnitCount = anzahlKontakte;

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

				return ii;
			}
			return null;
		}
	}
}
