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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class TextbausteinDC_Textbaustein : AbstractIDCEntityMapper<Textbaustein, TextbausteinDC>
    {
        public override TextbausteinDC MergeWithDC(Textbaustein pEntity, TextbausteinDC pDataContract)
        {
            pDataContract.Position = pEntity.Position;
            pDataContract.TextbausteinVersion = pEntity.Version.Value;
            pDataContract.TextbausteinOid = pEntity.Oid.Value;
            pDataContract.Text = pEntity.Text;
            pDataContract.Name = pEntity.Name;
            pDataContract.IsOnlyForEmployee = pEntity.IsOnlyForEmployee;

            if(pEntity.ServiceCategory != null)
                pDataContract.ServiceCategory = MapperFactory.ServiceCategoryDC_ServiceCategory.MapToNewDC(pEntity.ServiceCategory);

            if (pEntity.Employee != null)
                pDataContract.Employee = MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Employee);

            return pDataContract;
        }

        public override Textbaustein MergeWithEntity(TextbausteinDC pDataContract, Textbaustein pEntity)
        {
            ConcurrencyCheck(pDataContract.TextbausteinVersion, pEntity);

            pEntity.Position = pDataContract.Position;
            pEntity.Oid = pDataContract.TextbausteinOid;
            pEntity.Text = pDataContract.Text;
            pEntity.Name = pDataContract.Name;
            pEntity.IsOnlyForEmployee = pDataContract.IsOnlyForEmployee;

            if (pDataContract.ServiceCategory != null)
            {
                pEntity.ServiceCategory = pDataContract.ServiceCategory.ServiceCategoryOid.HasValue ?
                    DAOFactory.GenericDAO.LoadByID<ServiceCategory>(pDataContract.ServiceCategory.ServiceCategoryOid.Value) :
                    MapperFactory.ServiceCategoryDC_ServiceCategory.MapToNewEntity(pDataContract.ServiceCategory);
            }

            if (pDataContract.Employee != null)
            {
                pEntity.Employee = DAOFactory.GenericDAO.LoadByID<Employee>(pDataContract.Employee.EmployeeOid);
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(TextbausteinDC pDC, Textbaustein pEntity)
        {
            return pDC.TextbausteinOid == pEntity.Oid;
        }
    }
}
