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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class FileAttachmentDC_FileAttachmentInfo : AbstractIDCEntityMapper<FileAttachmentInfo, FileAttachmentDC>
    {
        public override FileAttachmentDC MergeWithDC(FileAttachmentInfo 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.FileAttachmentType = pEntity.Type;
            pDataContract.URL = "/DocumentDownload.aspx?faoid=" + pEntity.Oid.Value;

            return pDataContract;
        }

        public override FileAttachmentInfo MergeWithEntity(FileAttachmentDC pDataContract, FileAttachmentInfo pEntity)
        {
            this.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, FileAttachmentInfo pEntity)
        {
            if (pDC.FileAttachmentOid == null)
            {
                return false;
            }

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