using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using HRM.BO;
using HRM.BO.Assets;
using HRM.DA.DA.Assets;
using Payroll.BO;
using Payroll.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static iTextSharp.text.pdf.AcroFields;

namespace HRM.DA.Service.Assets
{
    public class AssetSerialTranAttachmentService : ServiceTemplate
    {
        #region Constructor
        public AssetSerialTranAttachmentService() { }

        protected override T CreateObject<T>(DataReader dr)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Mapping
        private void MapObjectAssetSerialTranAttachment(AssetSerialTranAttachment oAssetSerialTranAttachment, DataReader oReader)
        {
            base.SetObjectID(oAssetSerialTranAttachment, oReader.GetInt32("AssetSerailTranID").Value);
            oAssetSerialTranAttachment.AssetID = oReader.GetInt32("AssetID").Value;
            oAssetSerialTranAttachment.AssetSerialID = oReader.GetInt32("AssetSerialID").Value;
            oAssetSerialTranAttachment.AssetSerailTranID = oReader.GetInt32("AssetSerailTranID").Value;
            oAssetSerialTranAttachment.FileName = oReader.GetString("FileName");
            oAssetSerialTranAttachment.FilePath = oReader.GetString("FilePath");
            //oAssetSerialTranAttachment.FileType = (EnumFileType)Enum.Parse(typeof(EnumFileType), oReader.GetString("FileType"));
            oAssetSerialTranAttachment.FileAsByteArray = oReader.GetLob("FileAsByteArray");
            oAssetSerialTranAttachment.PreviousFileTobase64 = oReader.GetString("PreviousFileTobase64");
            oAssetSerialTranAttachment.Extension = oReader.GetString("Extension");
            this.SetObjectState(oAssetSerialTranAttachment, Ease.Core.ObjectState.Saved);
        }


        public void SaveAttachmnet(AssetSerialTranAttachment oAssetSerialAttachment)
        {
            try
            {

                if (oAssetSerialAttachment != null)
                {
                    if (oAssetSerialAttachment.ID > 0 && oAssetSerialAttachment.PreviousFileTobase64 != null)
                    {
                        byte[] newBytes = Convert.FromBase64String(oAssetSerialAttachment.PreviousFileTobase64);
                        oAssetSerialAttachment.FileAsByteArray = newBytes;
                        oAssetSerialAttachment.AssetSerialTranAttachmentID = oAssetSerialAttachment.ID;
                        oAssetSerialAttachment.AssetID = oAssetSerialAttachment.AssetID;
                        oAssetSerialAttachment.AssetSerialID = oAssetSerialAttachment.AssetSerialID;
                        oAssetSerialAttachment.AssetSerailTranID = oAssetSerialAttachment.AssetSerailTranID;
                        oAssetSerialAttachment.FileName = oAssetSerialAttachment.FileName;
                        oAssetSerialAttachment.FilePath = oAssetSerialAttachment.FilePath;
                        oAssetSerialAttachment.FileAsByteArray = oAssetSerialAttachment.FileAsByteArray;
                        oAssetSerialAttachment.Extension = oAssetSerialAttachment.Extension;
                        AssetSerialTranAttachmentDA.Insert(oAssetSerialAttachment, oAssetSerialAttachment.ConnectionString);
                    }
                    else
                    {
                        oAssetSerialAttachment.AssetSerailTranID = oAssetSerialAttachment.ID;
                        oAssetSerialAttachment.AssetID = oAssetSerialAttachment.AssetID;
                        oAssetSerialAttachment.AssetSerialID = oAssetSerialAttachment.AssetSerialID;
                        oAssetSerialAttachment.AssetSerailTranID = oAssetSerialAttachment.AssetSerailTranID;
                        oAssetSerialAttachment.FileName = oAssetSerialAttachment.FileName;
                        oAssetSerialAttachment.FilePath = oAssetSerialAttachment.FilePath;
                        oAssetSerialAttachment.FileAsByteArray = oAssetSerialAttachment.FileAsByteArray;
                        oAssetSerialAttachment.Extension = oAssetSerialAttachment.Extension;
                        //item.claimRequisitionFileAttacment.ReferenceID = oClaimRequisition.ID;
                        //item.claimRequisitionFileAttacment.RefchildID = itemid;
                        //FileAttachmentDA.Insert(item.claimRequisitionFileAttacment, oClaimRequisition.ConnectionString);
                        AssetSerialTranAttachmentDA.Insert(oAssetSerialAttachment, oAssetSerialAttachment.ConnectionString);
                    }
                }
               
            }
            catch (Exception e)
            {
                #region Handle Exception
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
        }

        #endregion
    }
}