using System;
using System.Linq;

using BeWo.Data.Entities;

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class MailDC_Mail : AbstractIDCEntityMapper<Mail, MailDC>
    {
        public override MailDC MergeWithDC(Mail pEntity, MailDC pDataContract)
        {
            pDataContract.MailOid = pEntity.Oid;
            pDataContract.MailVersion = pEntity.Version;
            pDataContract.InsertedBy = pEntity.InsUser;
            pDataContract.InsertedOn = pEntity.InsTs.Value;
            pDataContract.Description = pEntity.Notice;
            pDataContract.Message = pEntity.Message;
            pDataContract.Subject = pEntity.Subject;

            pDataContract.Receiver = pEntity.ReceiverList.Select(m2r => new MailReceiverDC { EmailAddress = m2r.ReceiverEMailAddress, FirstName = m2r.Receiver == null ? string.Empty : m2r.Receiver.Person.FirstName, LastName = m2r.Receiver == null ? string.Empty : m2r.Receiver.Person.LastName, Mail2ReceiverOid = m2r.Oid.Value, EmployeeOid = m2r.Receiver == null ? null : m2r.Receiver.Oid }).ToList();

            if (pEntity.Sender != null)
            {
                pDataContract.Sender = pEntity.Sender.Oid;

                // pDataContract.Sender = MapperFactory.EmployeeDC_Employee.MapToNewDC(pEntity.Sender);
                pDataContract.SenderName = pEntity.Sender.Person.FirstName + " " + pEntity.Sender.Person.LastName;
            }

            // können hier nicht gesetzt werden
            // pDataContract.Status 
            // pDataContract.Priority
            if (pEntity.AttachmentList != null)
            {
                pDataContract.Attachments = MapperFactory.FileAttachmentDC_FileAttachmentInfo.MapToNewDCs(pEntity.AttachmentList);
            }

            return pDataContract;
        }

        public override Mail MergeWithEntity(MailDC pDataContract, Mail pEntity)
        {
            throw new NotImplementedException();

            // ConcurrencyCheck(pDataContract.MailVersion, pEntity);

            // pEntity.Oid = pDataContract.MailOid;

            // pEntity.Message = pDataContract.Message;
            // pEntity.Sender = MapperFactory.EmployeeDC_Employee.MapToNewEntity(pDataContract.Sender);
            // pEntity.Subject = pDataContract.Subject;
            // pEntity.Version = pDataContract.MailVersion;
            // pEntity.Notice = pDataContract.Description;

            // pEntity.Mail2Receiver = pDataContract.Receiver.Select(receiver => new Mail2Receiver()
            // {
            // IsBlind = false,
            // Mail = pEntity,
            // Oid = -1,
            // Priority = Priority.normal,
            // ysdvgfe iowen geno ie erio
            // })
            // .ToList();

            // return pEntity;
        }

        protected override bool AreDCAndEntityEqual(MailDC pDC, Mail pEntity)
        {
            if (pDC.MailOid == null)
            {
                return false;
            }

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