using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using HRM.BO;
using HRM.DA;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
using Ease.Core;


namespace HRM.DA
{
    public class BookmarkService : ServiceTemplate, IBookmarkService
    {
        public BookmarkService()
        {
        }

        private void MapObject(Bookmark oBookmark, DataReader oReader)
        {
            base.SetObjectID(oBookmark, oReader.GetInt32("BookmarkID").Value);
            oBookmark.EmployeeId = oReader.GetInt32("EmployeeId");
            oBookmark.UserID = oReader.GetInt32("UserID", 0);
            oBookmark.MenuCode = oReader.GetString("MenuCode", string.Empty);
            oBookmark.Link = oReader.GetString("Link", string.Empty);
            oBookmark.Description = oReader.GetString("Description", string.Empty);
            oBookmark.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oBookmark.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oBookmark.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oBookmark.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oBookmark, ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader oReader)
        {
            Bookmark oBookmark = new Bookmark();
            MapObject(oBookmark, oReader);
            return oBookmark as T;
        }

        protected Bookmark CreateObject(DataReader oReader)
        {
            Bookmark oBookmark = new Bookmark();
            MapObject(oBookmark, oReader);
            return oBookmark;
        }

        public List<Bookmark> Get(int UserID)
        {
            List<Bookmark> bookmarks = new List<Bookmark>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BookmarkDA.Get(tc, UserID));
                bookmarks = this.CreateObjects<Bookmark>(dr);
                dr.Close();
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return bookmarks;
        }


        public int Save(Bookmark item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("Bookmark", "BookmarkID");
                    base.SetObjectID(item, id);
                    BookmarkDA.Insert(tc, item);
                }
                else
                {
                    BookmarkDA.Update(tc, item);
                }

                tc.End();
                return item.ID;
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                BookmarkDA.Delete(tc, id);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }
        }
        public void Delete(int userID, string menuKey)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                BookmarkDA.Delete(tc, userID, menuKey);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

    }
}