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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class Organisation2PersonDC_Organisation2Person : AbstractIDCEntityMapper<Organisation2Person, Organisation2PersonDC>
    {
        public override Organisation2PersonDC MergeWithDC(Organisation2Person pEntity, Organisation2PersonDC pDataContract)
        {
            //pDataContract.OrganisationOid = pEntity.Organisation.Oid;
            //pDataContract.OrganisationVersion = pEntity.Organisation.Version;
            //pDataContract.OrganisationName = pEntity.Organisation.Name;
			pDataContract.Organisation = MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(pEntity.Organisation);
            pDataContract.Organisation2PersonOid = pEntity.Oid;
            pDataContract.Organisation2PersonVersion = pEntity.Version;

            pDataContract.Notice = pEntity.Notice;

			foreach (var contact in pEntity.Contacts)
			{
				if (contact.Type == ContactType.business_Mail)
				{
					pDataContract.Mail = contact.Value;
				}

				if (contact.Type == ContactType.business_Phone)
				{
					pDataContract.Phone = contact.Value;
				}

				if (contact.Type == ContactType.business_Fax)
				{
					pDataContract.Fax = contact.Value;
				}
			}

            //pDataContract.ContactInformations = MapperFactory.ContactDC_Contact.MapToNewDCs(pEntity.Contacts);

            return pDataContract;
        }

        public override Organisation2Person MergeWithEntity(Organisation2PersonDC pDataContract, Organisation2Person pEntity)
        {
            pEntity.Notice = pDataContract.Notice;

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

            //MapperFactory.ContactDC_Contact.MergeWithEntitys(pDataContract.ContactInformations, pEntity.Contacts);

			MergeContact(pEntity.Contacts, pDataContract.Fax, ContactType.business_Fax);
			MergeContact(pEntity.Contacts, pDataContract.Mail, ContactType.business_Mail);
			MergeContact(pEntity.Contacts, pDataContract.Phone, ContactType.business_Phone);

            return pEntity;
        }

		private void MergeContact(IList<Contact> contacts, string pValue, ContactType pContactType)
		{

			Contact existingContact = contacts.SingleOrDefault(con => con.Type == pContactType);

			if (string.IsNullOrEmpty(pValue) && existingContact != null)
			{
				contacts.Remove(existingContact);
			}
			else if (!string.IsNullOrEmpty(pValue))
			{
				if (existingContact == null)
				{
					existingContact = new Contact
						{
							Type = pContactType
						};
					contacts.Add(existingContact);
				}

				existingContact.Value = pValue;
			}

		}

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

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