/// <summary>
 /// This function adds the history
 /// </summary>
 /// <param name="history"></param>
 /// <returns></returns>
 public int AddHistory(History history)
 {
     var context = new dbDataContext();
     var objHistory = new tbl_History
     {
         RefId = history.RefId,
         RefType = history.RefType,
         SubRefId = history.SubRefId,
         SubRefType = history.SubRefType,
         TypeId = history.TypeId,
         CreatedDate = DateTime.Now,
         Title = SetHistoryTitle(history),
         ClientUserId = history.ClientUserId
     };
     context.tbl_Histories.InsertOnSubmit(objHistory);
     context.SubmitChanges();
     return objHistory.HistoryId;
 }
        /// <summary>
        /// This function gets the history title
        /// </summary>
        /// <param name="history"></param>
        /// <returns></returns>
        private string SetHistoryTitle(History history)
        {
            var title = "";
            var context = new dbDataContext();
            if (history.RefType.Equals("Contact"))
            {
                //  var candidate = (context.tbl_Candidates.FirstOrDefault(t => t.CandidateId == history.RefId));
                if (string.IsNullOrEmpty(history.SubRefType))
                    return null;
                if (history.SubRefType.Equals("JobApplication"))
                {
                    var jobTitle = (from t in context.tbl_JobApplications
                                    join j in context.tbl_Jobs on t.JobId equals j.JobId
                                    where t.JobApplicationId == history.SubRefId
                                    select j.JobTitle);

                    title = "Applied for " + jobTitle + ".";
                    if (history.ClientUserId <= 0) return title;
                    // applied by user
                    var user = (context.tbl_Users.FirstOrDefault(t => t.UserId == history.ClientUserId));
                    title = "Applied for " + jobTitle + ((user != null) ? " via " + (user.Forename + " " + user.Surname) : "") + ".";
                }
                else if (history.SubRefType.Equals("SMS"))
                {
                    if (history.ClientUserId <= 0) return title;
                    var user = (context.tbl_Users.FirstOrDefault(t => t.UserId == history.ClientUserId));
                    title = "Sms has been sent";
                    title += (user != null)
                        ? (" by " + user.Forename + " " + user.Surname + ".")
                        : ".";
                }
                else if (history.SubRefType.Equals("Email"))
                {
                    if (history.ClientUserId <= 0) return title;
                    var user = (context.tbl_Users.FirstOrDefault(t => t.UserId == history.ClientUserId));
                    title = "Email has been sent";
                    title += (user != null)
                        ? (" by " + user.Forename + " " + user.Surname + ".")
                        : ".";
                }
                else if (history.SubRefType.Equals("User"))
                {
                    var user = (context.tbl_Users.FirstOrDefault(t => t.UserId == history.ClientUserId));
                    switch (history.TypeId)
                    {
                        case 1:
                            title = "Contact has been added" +
                                     ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                            break;
                        case 2:
                            title = "Contact has been updated" +
                                    ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                            break;
                        case 3:
                            title = "Contact has been deleted" +
                                    ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                            break;
                    }

                }
            }
            else if (history.RefType.Equals("Job"))
            {
                var vacancy = (context.tbl_Jobs.FirstOrDefault(t => t.JobId == history.RefId));
                if (vacancy == null) { return title; }

                var user = (context.tbl_Users.FirstOrDefault(t => t.UserId == history.ClientUserId));
                switch (history.TypeId)
                {
                    case 5:
                        title = vacancy.JobTitle + " vacancy has been added" +
                                                 ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                    case 6:
                        title = vacancy.JobTitle + " vacancy has been updated" +
                                ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                    case 7:
                        title = vacancy.JobTitle + " vacancy has been deleted" +
                                ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                }

            }
            else if (history.RefType.Equals("User"))
            {
                var user = (context.tbl_Users.FirstOrDefault(t => t.UserId == history.ClientUserId));
                var updatedUser = (context.tbl_Users.FirstOrDefault(t => t.UserId == history.RefId));
                if (updatedUser == null)
                    return title;
                switch (history.TypeId)
                {
                    case 12:
                        title = (updatedUser.Forename + " " + updatedUser.Surname) + " user has been added." +
                                                 ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                    case 13:
                        title = (updatedUser.Forename + " " + updatedUser.Surname) + " user has been updated" +
                                ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                    case 14:
                        title = (updatedUser.Forename + " " + updatedUser.Surname) + " user has been deleted" +
                                ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                }
            }
            else if (history.RefType.Equals("Client"))
            {
                var user = (context.tbl_Users.FirstOrDefault(t => t.UserId == history.ClientUserId));
                switch (history.TypeId)
                {
                    case 15:
                        title = "Client has been added." + ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                    case 16:
                        title = "Client has been updated" + ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                    case 17:
                        title = "Client has been deleted" + ((user != null) ? " by " + (user.Forename + " " + user.Surname) : "") + ".";
                        break;
                }
            }
            return title;
        }
        /// <summary>
        /// This function set the custom titles
        /// </summary>
        /// <param name="history"></param>
        /// <param name="dr"></param>
        /// <param name="isRefIdPassed"></param>
        // ReSharper disable once FunctionComplexityOverflow
        static void GetCustomTitle(ref History history, DataRow dr, bool isRefIdPassed)
        {
            var line1 = "";
            var line2 = "";
            var jobTitle = (dr["JobTitle"] ?? "").ToString();
            var jobId = dr["JobId"] == DBNull.Value ? 0 : (int)dr["JobId"];
            var refName = (dr["RefName"] ?? "").ToString();
            var refId = dr["RefId"] == DBNull.Value ? 0 : (int)dr["RefId"];
            var refType = (dr["RefType"] ?? "").ToString();
            var userName = (dr["UserName"] ?? "").ToString();
            var subRefId = dr["SubRefId"] == DBNull.Value ? 0 : (int)dr["SubRefId"];
            var emailSubject = (dr["EmailSubject"] ?? "").ToString();
            if (string.IsNullOrEmpty(emailSubject)) emailSubject = "<i>No Subject</i>";
            const string vacancyResponseLink = "/vacancies/vacancy-management.aspx?jobid=";
            const string updateJobLink = "/vacancies/vacancy-edit.aspx?jobid=";

            switch (history.TypeId)
            {
                case 1:
                    if (history.ClientUserId > 0)
                        line1 = "<strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed) + "</strong>  has been registered as a new candidate" + (string.IsNullOrEmpty(userName) ? (".") : (" by <strong>" + userName + "</strong>."));
                    else
                        line1 = "<strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed) + "</strong>  registered as a new candidate";
                    break;
                case 2:
                    if (history.ClientUserId > 0)
                        line1 = "<strong>" + userName + "</strong> updated <strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed) + "'s</strong> profile.";
                    else
                        line1 = "<strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed) + "</strong> updated his profile.";
                    break;
                case 3:
                    if (history.ClientUserId > 0)
                        line1 = "<strong>" + userName + "</strong> deleted <strong>" + refName + "'s</strong> profile.";
                    else
                        line1 = "<strong>" + refName + "</strong> deleted his profile.";
                    break;
                case 4:
                    if (history.SubRefType.Equals("JobApplication"))
                    {
                        line1 = "<strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed) + "</strong> applied for vacancy <strong>" + jobTitle + "</strong>.";
                        // applied by user
                        if (history.ClientUserId > 0)
                        {
                            line1 = "<strong>" + userName + "</strong> applied for vacancy on behalf of <strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed) + "</strong>.";
                        }
                        line2 = "<a href='" + vacancyResponseLink + jobId + "'>" + jobTitle + "</a>";
                    }
                    break;
                case 5:
                    line1 = "<strong>" + userName + "</strong> added a new vacancy";
                    line2 = "<a href='" + updateJobLink + jobId + "' target='_blank'>" + jobTitle + "</a>";
                    break;
                case 6:
                    line1 = "<strong>" + userName + "</strong> updated a vacancy.";
                    line2 = "<a href='" + updateJobLink + jobId + "' target='_blank'>" + jobTitle + "</a>";
                    break;
                case 7:
                    line1 = "<strong>" + userName + "</strong> deleted  the vacancy '<strong>" + jobTitle + "</strong>'.";
                    break;
                case 8:
                    line1 = "<strong>" + userName + "</strong> sent an email to <strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed, !refType.ToLower().Equals("contact")) + "</strong>.";
                    line2 = "<a href='javascript:void(0);' data-id='" + history.SubRefId + "' data-toggle='email-view-popup'>" + emailSubject + "</a>";
                    break;
                case 10:
                    line1 = "<strong>" + userName + "</strong> sent a text message to <strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed, !refType.ToLower().Equals("contact")) + "</strong>.";
                    line2 = "<a href='javascript:void(0);' data-id='" + history.SubRefId + "' data-toggle='sms-popover'>View Message</a>";
                    break;
                case 12:
                    line1 = "<strong>" + refName + "</strong> has been registered as a new user" + (string.IsNullOrEmpty(userName) ? (".") : (" by <strong>" + userName + "</strong>."));
                    break;
                case 13:
                    line1 = "<strong>" + userName + "</strong> updated <strong> " + refName + "'s</strong>  profile.";
                    break;
                case 14:
                    line1 = "<strong>" + userName + "</strong> deleted <strong>" + refName + "'s</strong>  profile.";
                    break;
                case 15:
                    line1 = "<strong>" + userName + "</strong> added <strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed, true) + "</strong>  as a Client.";
                    break;
                case 16:
                    line1 = "<strong>" + userName + "</strong> updated <strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed, true) + "'s</strong>  details.";
                    break;
                case 17:
                    line1 = "<strong>" + userName + "</strong> deleted <strong>" + refName + "'s</strong>  profile.";
                    break;
                case 18:
                    line1 = "<strong>" + userName + "</strong> recived an email from <strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed, !refType.ToLower().Equals("contact")) + "</strong>.";
                    line2 = "<a href='javascript:void(0);' data-id='" + history.SubRefId + "' data-toggle='email-view-popup'>" + emailSubject + "</a>";
                    break;
                case 19:
                    line1 = "<strong>" + userName + "</strong> deleted the job application for <strong>" + GetRefNamePopupLink(refId, refName, isRefIdPassed, !refType.ToLower().Equals("contact")) + "</strong>.";
                    line2 = "<a href='" + vacancyResponseLink + jobId + "'>" + jobTitle + "</a>";
                    break;
                case 20:
                    line1 = "<strong>" + userName + "</strong> created an interview.";
                    var interviewId = refId;
                    line2 = GetInterviewTitleLink(interviewId);
                    break;
                case 21:
                    line1 = "<strong>" + userName + "</strong> updated an interview.";
                    interviewId = refId;
                    line2 = GetInterviewTitleLink(interviewId);
                    break;
                case 22:

                    if (!string.IsNullOrEmpty(userName))
                    {
                        line1 = "<strong>" + userName + "</strong> allocated a timeslot for <strong>" +
                                GetRefNamePopupLink(refId, refName, isRefIdPassed, !refType.ToLower().Equals("contact")) + "</strong>";
                    }
                    else
                    {
                        line1 = "<strong>" +
                                GetRefNamePopupLink(refId, refName, isRefIdPassed, !refType.ToLower().Equals("contact")) +
                                "</strong> confirmed an interview.";
                    }
                    var tSlotId = subRefId;
                    line2 = GetInterviewLinkFromTimeSlotId(tSlotId);
                    break;
            }
            if (string.IsNullOrEmpty(userName))
            {
                line1 = line1.Replace("<strong></strong>", "");
                line1 = Utils.FirstCharToUpper(line1.Trim());
            }

            history.CustomTitle1 = line1;
            history.CustomTitle2 = line2;
        }
 /// <summary>
 /// This function extracts the histories
 /// </summary>
 /// <param name="dr"></param>
 /// <param name="isRefIdPassed"></param>
 /// <returns></returns>
 // ReSharper disable once FunctionComplexityOverflow
 private static History ExtractHistoryItem(DataRow dr, bool isRefIdPassed)
 {
     var obj = new History
     {
         TotalCount = (int)dr["RECORDCOUNT"],
         HistoryId = (int)dr["HistoryId"],
         Title = (dr["Title"] ?? "").ToString(),
         Type = (dr["HistoryType"] ?? "").ToString(),
         TypeId = (int)dr["TypeId"],
         RefId = (int)dr["RefId"],
         RefType = (dr["RefType"] ?? "").ToString(),
         RefProfileImage = (dr["DocumentPath"] ?? "").ToString(),
         SubRefId = (int)dr["SubRefId"],
         SubRefType = (dr["SubRefType"] ?? "").ToString(),
         CreatedDate = (DateTime)dr["CreatedDate"],
         ClientUserId = (int)dr["ClientUserId"]
     };
     GetCustomTitle(ref obj, dr, isRefIdPassed);
     return obj;
 }