using BS.Shared.DataContracts.Compact;
using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared;
using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class OrganisationPersonRelDC_Organisation2PersonMapper : AbstractIDCEntityMapper<Organisation2Person, OrganisationPersonRelationDC>
    {
        public override OrganisationPersonRelationDC MergeWithDC(Organisation2Person pEntity, OrganisationPersonRelationDC pDataContract)
        {
			CompactPersonDC personDc = new CompactPersonDC();
	        pDataContract.Person = personDc;

			personDc.DateOfBirth = pEntity.Person.DateOfBirth;
			personDc.FirstName = pEntity.Person.FirstName;
			personDc.LastName = pEntity.Person.LastName;
			personDc.PersonOid = pEntity.Person.Oid.Value;
			personDc.PersonVersion = pEntity.Person.Version.Value;
	        personDc.PersonType = pEntity.Person.Type;

			if (pEntity.Person.Function != null)
			{
				personDc.Function = pEntity.Person.Function.Value;
			}

			if (pEntity.Contacts != null)
			{
				foreach (var contact in pEntity.Contacts)
				{

					if (contact.Type == ContactType.business_Phone)
					{
						personDc.Communication1 = contact.Value;
					}

					if (contact.Type == ContactType.business_Fax)
					{
						personDc.Communication2 = contact.Value;
					}

					if (contact.Type == ContactType.business_Mail)
					{
						personDc.Communication3 = contact.Value;
					}

					if (contact.Type == ContactType.business_Homepage)
					{
						personDc.Communication4 = contact.Value;
					}

				}
			}

			if (string.IsNullOrEmpty(personDc.Communication1))
			{
				if (pEntity.Person.Contacts != null)
				{
					foreach (var contact in pEntity.Person.Contacts)
					{
						if (contact.Type == ContactType.private_Phone)
						{
							personDc.Communication1 = contact.Value;
						}

					}
				}
			}
			if (string.IsNullOrEmpty(personDc.Communication1))
			{
				if (pEntity.Person.Contacts != null)
				{
					foreach (var contact in pEntity.Person.Contacts)
					{
						if (contact.Type == ContactType.private_MobilePhone)
						{
							personDc.Communication1 = contact.Value;
						}

					}
				}
			}
			if (string.IsNullOrEmpty(personDc.Communication2))
			{
				if (pEntity.Person.Contacts != null)
				{
					foreach (var contact in pEntity.Person.Contacts)
					{
						if (contact.Type == ContactType.private_Fax)
						{
							personDc.Communication2 = contact.Value;
						}

					}
				}
			}
			if (string.IsNullOrEmpty(personDc.Communication3))
			{
				if (pEntity.Person.Contacts != null)
				{
					foreach (var contact in pEntity.Person.Contacts)
					{
						if (contact.Type == ContactType.private_Mail)
						{
							personDc.Communication3 = contact.Value;
						}

					}
				}
			}
	        pDataContract.Notice = pEntity.Notice;

            pDataContract.Organisation = MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(pEntity.Organisation);
            pDataContract.Organisation2PersonOid = pEntity.Oid;
            pDataContract.Organisation2PersonVersion = pEntity.Version;

            return pDataContract;
        }

        public override Organisation2Person MergeWithEntity(OrganisationPersonRelationDC pDataContract, Organisation2Person pEntity)
        {
            this.ConcurrencyCheck(pDataContract.Organisation2PersonVersion, pEntity);

            if (pDataContract.Person != null)
                pEntity.Person = DAOFactory.GenericDAO.LoadByID<Person>(pDataContract.Person.PersonOid);

            if (pDataContract.Organisation != null)
                pEntity.Organisation = DAOFactory.GenericDAO.LoadByID<Organisation>(pDataContract.Organisation.OrganisationOid);

            pEntity.Oid = pDataContract.Organisation2PersonOid;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(OrganisationPersonRelationDC pDC, Organisation2Person pEntity)
        {
            if (pDC.Organisation2PersonOid == null)
                return false;

            return pDC.Organisation2PersonOid == pEntity.Oid;
        }
    }
}
