using System.Collections.Generic;
using System.Linq;

using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared;
using BS.Shared.Extensions;

using NHibernate;

namespace BeWo.Service.Core
{
	/// <summary>
	///     Used by more than one Service... ? Businesslogic schicht?
	/// </summary>
	public static class ServiceLogic
	{
		public static bool ConcurrencyCheck(long? pDataContractVersion, BeWoEntityBase pEntity)
		{
			if (pDataContractVersion != null && pDataContractVersion != pEntity.Version)
			{
				throw new StaleObjectStateException(pEntity.GetType().ToString(), pEntity.Oid);
			}

			return true;
		}

		//public static ICostBearerCalc GetCalcualtionsObjectForCostBearer(long pCostBearerOid)
		//{
		//    var cb = DAOFactory.GenericDAO.LoadByID<CostBearer>(pCostBearerOid);

		//    if (!string.IsNullOrEmpty(cb.CalculationsDllName))
		//    {
		//        Type[] calcTypes = new Type[0];

		//        string customReportDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\" + cb.CalculationsDllName);
		//        if (File.Exists(customReportDllPath))
		//        {
		//            calcTypes = Assembly.LoadFrom(customReportDllPath).GetTypes();

		//            foreach (var iType in calcTypes)
		//            {
		//                if (typeof(ICostBearerCalc).IsAssignableFrom(iType))
		//                {
		//                    return Activator.CreateInstance(iType) as ICostBearerCalc;
		//                }
		//            }
		//        }
		//    }

		//    return new InvoiceCreation();
		//}

		//public static List<CostRate> GetCostRateListForCostBearer(CostBearer costBearer)
		//{
		//    List<CostRate> list = new List<CostRate>();

		//    if (costBearer != null && costBearer.CostRatePeriods != null)
		//    {
		//        foreach (CostRatePeriod crp in costBearer.CostRatePeriods)
		//        {
		//            CostRate cr = new CostRate();
		//            cr.Amount = crp.CostRateValue;
		//            switch (crp.CostRateType)
		//            {
		//                case CostRatePeriodType.HourlyRate:
		//                    cr.CostRateType = CostRateType.HourlyRate;
		//                    break;
		//                case CostRatePeriodType.MinutesIntervall:
		//                    cr.CostRateType = CostRateType.MinutesIntervall;
		//                    break;
		//                case CostRatePeriodType.RateFactor:
		//                    cr.CostRateType = CostRateType.RateFactor;
		//                    break;
		//            }

		//            cr.StartDate = crp.StartDate;
		//            cr.EndDate = crp.EndDate;

		//            list.Add(cr);
		//        }
		//    }

		//    return list;
		//}


		//public static string GetNextInvoiceNumber(int increment, InvoiceBase invoiceBase)
		//{
		//	string next = String.Empty;
		//	InvoiceNumberDC invoiceNumber = new AccountingServiceImp().LoadInvoiceNumber();

		//	if (invoiceNumber != null && invoiceNumber.Use)
		//	{
		//		next = ApplyPattern(invoiceNumber.Pattern, invoiceNumber.CurrentNumber + increment, invoiceBase);
		//	}
		//	return next;
		//}

		
		public static void SetActivationType<T>(Dictionary<long, long> pOid2Version, ActivationTypeId activationType)
			where T : BeWoEntityBase, new()
		{
			var lOriginals = DAOFactory.GenericDAO.LoadByIDs<T>(pOid2Version.Select(e => e.Key)).Where(e => e.SystemEntryID == null).ToList();

			pOid2Version.Keys.DoForEach(SettingsLogic.RemoveFromAllHistories<T>);

			lOriginals.DoForEach(or => ConcurrencyCheck(pOid2Version[or.Oid.Value], or));
			lOriginals.DoForEach(or => or.IsActive = activationType);

			DAOFactory.GenericDAO.Update(lOriginals);
		}

		public static void SetActivationType<T>(long pOid, long pVersion, ActivationTypeId activationType)
			where T : BeWoEntityBase, new()
		{
			SetActivationType<T>(
				new Dictionary<long, long>
				{
					{
						pOid, pVersion
					}
				},
				activationType);
		}
	}
}