using System;
using System.Collections.Generic;
using BS.Shared.DataContracts;
using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;

namespace BeWo.Service.DCEntityMapper
{
    public class CompactOrganisationDC_Organisation : AbstractIDCEntityMapper<Organisation, CompactOrganisationDC>
    {
        public override CompactOrganisationDC MergeWithDC(Organisation pEntity, CompactOrganisationDC pDataContract)
        {
            pDataContract.OrganisationOid = pEntity.Oid.Value;
            pDataContract.OrganisationVersion = pEntity.Version.Value;
            pDataContract.Name = pEntity.Name;
			pDataContract.ActivationType = pEntity.IsActive;
			pDataContract.DebitorNumber = pEntity.DebitorNumber;
			
            if (pEntity.CostBearer != null)
            {
                pDataContract.CostBearerID = pEntity.CostBearer.ID;
                pDataContract.CostBearerOid = pEntity.CostBearer.Oid;
                pDataContract.CostBearerVersion = pEntity.CostBearer.Version;

                pDataContract.CostRatePeriods = pEntity.CostBearer.CostRatePeriods.MapToNewDCs();

                pDataContract.ActualHourlyRate = pDataContract.CostRatePeriods.GetCurrentlyValidRateValue(CostRatePeriodType.HourlyRate) ?? 0m;
                pDataContract.ActualRateFactor = pDataContract.CostRatePeriods.GetCurrentlyValidRateValue(CostRatePeriodType.RateFactor) ?? 0m;
                pDataContract.ActualMinuteIntervall = Convert.ToInt32(pDataContract.CostRatePeriods.GetCurrentlyValidRateValue(CostRatePeriodType.MinutesIntervall) ?? 0m);

                pDataContract.IsCalculatingWithFactor = pEntity.CostBearer.IsCalculatingWithFactor;
            }

            return pDataContract;
        }

        // Compact Organisations are readonly
        public override Organisation MergeWithEntity(CompactOrganisationDC pDataContract, Organisation pEntity)
        {
            return DAOFactory.GenericDAO.LoadByID<Organisation>(pDataContract.OrganisationOid);
        }

        protected override bool AreDCAndEntityEqual(CompactOrganisationDC pDC, Organisation pEntity)
        {
            return pDC.OrganisationOid == pEntity.Oid;
        }

        //public override CompactOrganisationDC CreateNewDC()
        //{
        //    CompactOrganisationDC dc = new CompactOrganisationDC();
        //    dc.CostRatePeriods = new List<CostRatePeriodDC>();
        //    return dc;
        //}
        
    }
}