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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class TaskDC_Task : AbstractIDCEntityMapper<Task, TaskDC>
    {
        public override TaskDC MergeWithDC(Task pEntity, TaskDC pDataContract)
        {
            pDataContract.TaskOid = pEntity.Oid;
            pDataContract.TaskVersion = pEntity.Version;
            pDataContract.DueDate = pEntity.DueDate;
            pDataContract.CompletedNotice = pEntity.CompletedNotice;
            pDataContract.CompletedDate = pEntity.CompletedDate;
            pDataContract.Title = pEntity.Title;
            pDataContract.Description = pEntity.Description;

            pDataContract.Employees = MapperFactory.CompactEmployeeDC_Employee.MapToNewDCs(pEntity.EmployeeList);

            if (pEntity.SupportConcept != null)
            {
                pDataContract.SupportConceptOid = pEntity.SupportConcept.Oid;
            }

            return pDataContract;
        }

        public override Task MergeWithEntity(TaskDC pDataContract, Task pEntity)
        {
            this.ConcurrencyCheck(pDataContract.TaskVersion, pEntity);

            pEntity.Oid = pDataContract.TaskOid;
            pEntity.DueDate = pDataContract.DueDate;
            pEntity.CompletedNotice = pDataContract.CompletedNotice;
            pEntity.CompletedDate = pDataContract.CompletedDate;
            pEntity.Title = pDataContract.Title;
            pEntity.Description = pDataContract.Description;

            pEntity.SupportConcept = null;
            if (pDataContract.SupportConceptOid.HasValue)
            {
                pEntity.SupportConcept = DAOFactory.GenericDAO.LoadByID<SupportConcept>(pDataContract.SupportConceptOid.Value);
            }

            MapperFactory.CompactEmployeeDC_Employee.MergeWithEntitys(pDataContract.Employees, pEntity.EmployeeList);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(TaskDC pDC, Task pEntity)
        {
            if (pDC.TaskOid == null)
            {
                return false;
            }

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