using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BS.Shared.Core;
using BS.Shared.DataContracts;

namespace BeWo.Service.Invoicing
{
	[IDSpecificClass(Identifier = "LWLMaxAmount")]
	class KuKLWLSettlementInvoiceCreation : SettlementInvoiceCreation
	{
		public override decimal? CalculateMaxAmount(Settlement2DC si)
		{
			decimal approvedCurrentHourlyRate = 0;
			decimal approvedLastHourlyRate = 0;
			decimal approved3RdHourlyRate = 0;
			
			if (si.DifferentHourlyRateCount == 1)
			{
				if (si.ApprovedFLS != null)
				{
					approvedCurrentHourlyRate = si.ApprovedFLS.Value;
				}
			}
			else
			{
				approvedCurrentHourlyRate = (si.ApprovedFLSWeekly ?? 0) * si.WeeksWithNewHourlyRate;
			}

			approvedLastHourlyRate = (si.ApprovedFLSWeekly ?? 0) * si.WeeksWithOldHourlyRate;
			approved3RdHourlyRate = (si.ApprovedFLSWeekly ?? 0) * si.WeeksWithHourlyRate3;

			approvedCurrentHourlyRate = Math.Round(approvedCurrentHourlyRate, 2, MidpointRounding.AwayFromZero);
			approvedLastHourlyRate = Math.Round(approvedLastHourlyRate, 2, MidpointRounding.AwayFromZero);
			approved3RdHourlyRate = Math.Round(approved3RdHourlyRate, 2, MidpointRounding.AwayFromZero);

			decimal rf = 1;
			if (si.RateFactor.HasValue && si.RateFactor.Value != 0)
			{
				rf = si.RateFactor.Value;
				rf = (100 + rf) / 100;
			}

			decimal maxAmount = Math.Round(approved3RdHourlyRate * si.HourlyRate3 * rf, 2, MidpointRounding.AwayFromZero);
			maxAmount += Math.Round(approvedLastHourlyRate * (si.HourlyRateOld ?? 0) * rf, 2, MidpointRounding.AwayFromZero);
			maxAmount += Math.Round(approvedCurrentHourlyRate * (si.HourlyRateNew ?? 0) * rf, 2, MidpointRounding.AwayFromZero);
			maxAmount = Math.Round(maxAmount, 2, MidpointRounding.AwayFromZero);

			return maxAmount;
		}
	}
}
