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

namespace BeWo.WegweiserBetreuungsdienstReports
{
	public class DefaultInvoiceCreation : 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);

			if (!invoice.AmountAdvancePayments.HasValue)
			{
				SetAmountAdvancePayments(invoice);
			}
			
			return invoice;
		}

		public override void CheckMaxApprovedAmount(ServiceInvoice invoice, ServiceInvoicePeriod sip, SupportConceptApprovalPeriodDC scap, CostBearer2SupportConcept costBearer2SupportConcept, DateTimeSpan invoicePeriod, List<ServiceRecordDC> serviceRecords)
		{
			var calc = PluginLoader.FindClass<Calculations>(_SpecificID) ?? Calculations.GetInstance(_SpecificID);

			
			if (!sip.ApprovedFixedAmount.HasValue)
			{
				var costBearer = costBearer2SupportConcept.CostBearer;
                var costRates = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(costBearer.CostRatePeriods);

				sip.ApprovedHours = calc.GetApprovedBETotal(scap, costRates);
				
			}

			base.CheckMaxApprovedAmount(invoice, sip, scap, costBearer2SupportConcept, invoicePeriod, serviceRecords);
		}

	}
}
