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

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

namespace BeWo.Service.DCEntityMapper
{
    public class CustomerOrganisationRelationDC_Customer2Organisation : AbstractIDCEntityMapper<Customer2Organisation, CustomerOrganisationRelationDC>
    {
        public override CustomerOrganisationRelationDC MergeWithDC(Customer2Organisation pEntity, CustomerOrganisationRelationDC pDataContract)
        {
            if (pEntity.ValueList.Count > 0)
            {
                pDataContract.RelationRole = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.ValueList[0].Entry);
            }

            pDataContract.Customer2OrganisationNotice = pEntity.Notice;
            pDataContract.Info1 = pEntity.Info1;
            pDataContract.Info2 = pEntity.Info2;
            pDataContract.Info3 = pEntity.Info3;
            pDataContract.Info4 = pEntity.Info4;
            pDataContract.Info5 = pEntity.Info5;
            pDataContract.Customer2OrganisationOid = pEntity.Oid;
            pDataContract.Customer2OrganisationVersion = pEntity.Version;
            pDataContract.Organisation = MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(pEntity.Organisation);

            if (pEntity.Organisation.Address != null)
            {
                pDataContract.Organisation.Street = pEntity.Organisation.Address.Street;
                pDataContract.Organisation.PostalCode = pEntity.Organisation.Address.PostalCode;
                pDataContract.Organisation.Town = pEntity.Organisation.Address.Town;
            }

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

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

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

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

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

                }
            }

            return pDataContract;
        }

        public override Customer2Organisation MergeWithEntity(CustomerOrganisationRelationDC pDataContract, Customer2Organisation pEntity)
        {
            MapperFactory.ValueListEntryDC_ValueListEntry.MapUniqueTypeValueListEntryBack2Entity(pDataContract.RelationRole, pEntity.ValueList, ValueListEntryType.EnvironmentOrganisationType, TableID.Customer2Organisation);

            pEntity.Oid = pDataContract.Customer2OrganisationOid;
            pEntity.Version = pDataContract.Customer2OrganisationVersion;
            pEntity.Notice = pDataContract.Customer2OrganisationNotice;

            pEntity.Info1 = pDataContract.Info1;
            pEntity.Info2 = pDataContract.Info2;
            pEntity.Info3 = pDataContract.Info3;
            pEntity.Info4 = pDataContract.Info4;
            pEntity.Info5 = pDataContract.Info5;

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

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(CustomerOrganisationRelationDC pDC, Customer2Organisation pEntity)
        {
            if (pDC.Customer2OrganisationOid == null)
            {
                return false;
            }

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