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

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

namespace BeWo.Service.DCEntityMapper
{
    public class PersonDC_Person : AbstractIDCEntityMapper<Person, PersonDC>
    {
        public override PersonDC MergeWithDC(Person pEntity, PersonDC pDataContract)
        {
            pDataContract.PersonOid = pEntity.Oid;
            pDataContract.DateOfBirth = pEntity.DateOfBirth;
            pDataContract.FirstName = pEntity.FirstName;
	        pDataContract.Sex = pEntity.Sex;
            pDataContract.FamilyStatus = pEntity.FamilyStatus;
            pDataContract.LastName = pEntity.LastName;
            pDataContract.Profession = pEntity.Profession;
            pDataContract.PersonVersion = pEntity.Version;
            pDataContract.PersonNotice = pEntity.Notice;
            pDataContract.Abbreviation = pEntity.Abbreviation;
            pDataContract.ActivationType = pEntity.IsActive;
			
            if (pEntity.Title != null)
            {
                pDataContract.Title = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.Title);
            }

            if (pEntity.Function != null)
            {
                pDataContract.Function = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.Function);
            }
			
            if (pEntity.Address != null)
            {
                pDataContract.AddressOid = pEntity.Address.Oid;
				pDataContract.AddressLine1 = pEntity.Address.AddressLine1;
                pDataContract.Street = pEntity.Address.Street;
                pDataContract.Town = pEntity.Address.Town;
                pDataContract.PostalCode = pEntity.Address.PostalCode;
                pDataContract.AddressVersion = pEntity.Address.Version;
            }

            if (pEntity.InvoiceAddress != null)
            {
                pDataContract.InvoiceAddressOid = pEntity.InvoiceAddress.Oid;
                pDataContract.InvoiceAddressStreet = pEntity.InvoiceAddress.Street;
                pDataContract.InvoiceAddressTown = pEntity.InvoiceAddress.Town;
                pDataContract.InvoiceAddressPostalCode = pEntity.InvoiceAddress.PostalCode;
                pDataContract.InvoiceAddressVersion = pEntity.InvoiceAddress.Version;
                pDataContract.InvoiceAddressLine1 = pEntity.InvoiceAddress.AddressLine1;
                pDataContract.InvoiceAddressLine2 = pEntity.InvoiceAddress.AddressLine2;
            }

            if (pEntity.BankAccount != null)
            {
                pDataContract.BankAccountOid = pEntity.BankAccount.Oid;
                pDataContract.BankAccountVersion = pEntity.BankAccount.Version;
                pDataContract.AccountNumber = pEntity.BankAccount.AccountNumber;
                pDataContract.BankName = pEntity.BankAccount.BankName;
                pDataContract.BankCode = pEntity.BankAccount.BankCode;
                pDataContract.IBAN = pEntity.BankAccount.IBAN;
                pDataContract.Bic = pEntity.BankAccount.Bic;
            }

            pDataContract.ContactInformations = MapperFactory.ContactDC_Contact.MapToNewDCs(pEntity.Contacts);
            pDataContract.OrganisationRelations = MapperFactory.Organisation2PersonDC_Organisation2Person.MapToNewDCs(pEntity.Organisation2Persons);

            var defs = DAOFactory.SearchDAO.GetVarFieldDefs(TableID.Person);
            pDataContract.VarFields = MapperFactory.VarFieldDC_VarField.VarFieldMapToDCs(defs, pEntity.VarFieldValueList);

            return pDataContract;
        }

        public override Person MergeWithEntity(PersonDC pDataContract, Person pEntity)
        {
            this.ConcurrencyCheck(pDataContract.PersonVersion, pEntity);

            pEntity.Oid = pDataContract.PersonOid;

            pEntity.DateOfBirth = pDataContract.DateOfBirth;
            pEntity.FirstName = pDataContract.FirstName;
            pEntity.Sex = pDataContract.Sex;
            pEntity.FamilyStatus = pDataContract.FamilyStatus;
            pEntity.LastName = pDataContract.LastName;
            pEntity.Profession = pDataContract.Profession;
            pEntity.Notice = pDataContract.PersonNotice;
            pEntity.Abbreviation = pDataContract.Abbreviation;
            pEntity.IsActive = pDataContract.ActivationType;

            if (pDataContract.Title == null)
            {
                pEntity.Title = null;
            }
            else if (pEntity.Title == null || pEntity.Title.Oid != pDataContract.Title.ValueListEntryOid)
            {
                pEntity.Title = DAOFactory.GenericDAO.LoadByID<ValueListEntry>(pDataContract.Title.ValueListEntryOid.Value);
            }

            if (pDataContract.Function == null)
            {
                pEntity.Function = null;
            }
            else if (pEntity.Function == null || pEntity.Function.Oid != pDataContract.Function.ValueListEntryOid)
            {
                pEntity.Function = DAOFactory.GenericDAO.LoadByID<ValueListEntry>(pDataContract.Function.ValueListEntryOid.Value);
            }

            if (pDataContract.ContainsAddressData())
            {
                if (pEntity.Address == null)
                {
                    pEntity.Address = new Address();
                }

                this.ConcurrencyCheck(pDataContract.AddressVersion, pEntity.Address);

                pEntity.Address.Oid = pDataContract.AddressOid;
				pEntity.Address.AddressLine1 = pDataContract.AddressLine1;
                pEntity.Address.Street = pDataContract.Street;
                pEntity.Address.Town = pDataContract.Town;
                pEntity.Address.PostalCode = pDataContract.PostalCode;
            }
            else if (pEntity.Address != null)
            {
                // all-delete-orphan wird von NHibernate (noch) nicht unterstützt für many-to-one
                Address lAdress = pEntity.Address;
                pEntity.Address = null;
                DAOFactory.GenericDAO.Delete(lAdress);
            }

            if (pDataContract.ContainsInvoiceAddressData())
            {
                if (pEntity.InvoiceAddress == null)
                {
                    pEntity.InvoiceAddress = new Address();
                }

                this.ConcurrencyCheck(pDataContract.InvoiceAddressVersion, pEntity.InvoiceAddress);

                pEntity.InvoiceAddress.Oid = pDataContract.InvoiceAddressOid;
                pEntity.InvoiceAddress.Street = pDataContract.InvoiceAddressStreet;
                pEntity.InvoiceAddress.Town = pDataContract.InvoiceAddressTown;
                pEntity.InvoiceAddress.PostalCode = pDataContract.InvoiceAddressPostalCode;
                pEntity.InvoiceAddress.AddressLine1 = pDataContract.InvoiceAddressLine1;
                pEntity.InvoiceAddress.AddressLine2 = pDataContract.InvoiceAddressLine2;
            }
            else if (pEntity.InvoiceAddress != null)
            {
                // all-delete-orphan wird von NHibernate (noch) nicht unterstützt für many-to-one
                Address lInvoiceAddress = pEntity.InvoiceAddress;
                pEntity.InvoiceAddress = null;
                DAOFactory.GenericDAO.Delete(lInvoiceAddress);
            }

            if (pDataContract.ContainsBankAccountData())
            {
                if (pEntity.BankAccount == null)
                {
                    pEntity.BankAccount = new BankAccount();
                }

                this.ConcurrencyCheck(pDataContract.BankAccountVersion, pEntity.BankAccount);

                pEntity.BankAccount.Oid = pDataContract.BankAccountOid;
                pEntity.BankAccount.Version = pDataContract.BankAccountVersion;
                pEntity.BankAccount.IBAN = pDataContract.IBAN;
                pEntity.BankAccount.Bic = pDataContract.Bic;
                pEntity.BankAccount.BankCode = pDataContract.BankCode;
                pEntity.BankAccount.BankName = pDataContract.BankName;
                pEntity.BankAccount.AccountNumber = pDataContract.AccountNumber;
            }
            else if (pEntity.BankAccount != null)
            {
                // all-delete-orphan wird von NHibernate (noch) nicht unterstützt für many-to-one)
                BankAccount lBankAccount = pEntity.BankAccount;
                pEntity.BankAccount = null;
                DAOFactory.GenericDAO.Delete(lBankAccount);
            }

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

            MapperFactory.Organisation2PersonDC_Organisation2Person.MergeWithEntitys(pDataContract.OrganisationRelations, pEntity.Organisation2Persons);

            pEntity.VarFieldValueList = MapperFactory.VarFieldDC_VarField.VarFieldMergeToEntity(pEntity.VarFieldValueList, pDataContract.VarFields, pEntity);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(PersonDC pDC, Person pEntity)
        {
            if (pDC.PersonOid == null)
            {
                return false;
            }

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