using System.IO;
using BeWo.Data.Entities;

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class FileAttachmentDC_FileAttachment : AbstractIDCEntityMapper<FileAttachment, FileAttachmentDC>
    {
        public override FileAttachmentDC MergeWithDC(FileAttachment pEntity, FileAttachmentDC pDataContract)
        {
            pDataContract.FileAttachmentOid = pEntity.Oid;
            pDataContract.FileAttachmentVersion = pEntity.Version;
            pDataContract.Description = pEntity.Notice;
            pDataContract.InsertedBy = pEntity.InsUser;
            pDataContract.IsLocked = pEntity.IsLocked;
            pDataContract.LockedBy = pEntity.LockedBy;
            if (pEntity.InsTs.HasValue)
                pDataContract.InsertedOn = pEntity.InsTs.Value;
            pDataContract.Description = pEntity.Notice;
            pDataContract.FileName = Path.GetFileName(pEntity.Path);
            pDataContract.BeWoFolderOid = pEntity.BeWoFolderOid;
            //pDataContract.URL = pEntity.Path;

            pDataContract.URL = "/DocumentDownload.aspx?faoid=" + pEntity.Oid.Value;
            pDataContract.FileAttachmentType = pEntity.Type;
            return pDataContract;
        }

        public override FileAttachment MergeWithEntity(FileAttachmentDC pDataContract, FileAttachment pEntity)
        {
            ConcurrencyCheck(pDataContract.FileAttachmentVersion, pEntity);

            pEntity.Oid = pDataContract.FileAttachmentOid;
            pEntity.Notice = pDataContract.Description;
            pEntity.IsLocked = pDataContract.IsLocked;
            pEntity.LockedBy = pDataContract.LockedBy;
            pEntity.BeWoFolderOid = pDataContract.BeWoFolderOid;

            pEntity.Type = pDataContract.FileAttachmentType;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(FileAttachmentDC pDC, FileAttachment pEntity)
        {
            if (pDC.FileAttachmentOid == null)
            {
                return false;
            }

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