/// <summary>
        /// Save Chat History
        /// </summary>
        /// <param name="chatHistory"></param>
        /// <returns></returns>
        public static void SaveChatHistory(ChatHistoryVM chatHistory)
        {
            StringBuilder traceLog = null;

            using (LinksMediaContext dataContext = new LinksMediaContext())
            {
                traceLog = new StringBuilder();
                try
                {
                    traceLog.AppendLine("Start: SaveChatHistory() for receiver Id");
                    Credentials cred = CommonWebApiBL.GetUserId(Thread.CurrentPrincipal.Identity.Name);
                    if (chatHistory != null && chatHistory.ReceiverCredId > 0 && chatHistory.SenderCredId > 0 && !string.IsNullOrEmpty(chatHistory.Message))
                    {
                        tblChatHistory objChatHistory = new tblChatHistory()
                        {
                            ReceiverCredId     = chatHistory.ReceiverCredId,
                            SenderCredId       = cred.Id,
                            Message            = chatHistory.Message,
                            IsRead             = false,
                            TrasactionDateTime = DateTime.UtcNow,
                        };
                        dataContext.ChatHistory.Add(objChatHistory);
                        dataContext.SaveChanges();
                    }
                    return;
                }
                catch
                {
                    throw;
                }
                finally
                {
                    traceLog.AppendLine("SaveChatHistory  end() : --- " + DateTime.Now.ToLongDateString());
                    LogManager.LogManagerInstance.WriteTraceLog(traceLog);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="credUserId"></param>
        /// <param name="message"></param>
        private static void SendChatNotification(SocketSentChatVM chatHistory)
        {
            byte[] certificate = File.ReadAllBytes(chatHistory.CeritifcatePath);
            new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = false;
                lock (syncLock)
                {
                    StringBuilder traceLog = null;
                    List <UserNotification> allactiveDevices = null;
                    using (LinksMediaContext dataContext = new LinksMediaContext())
                    {
                        try
                        {
                            traceLog = new StringBuilder();
                            traceLog.Append("Start:SendChatNotification()");
                            string notificationType = string.Empty;
                            traceLog.AppendLine("Start: SendChallegesNotificationToUser()");

                            List <tblCredentials> chatsenderReceiverDetails = dataContext.Credentials.Where(em => string.Compare(em.EmailId, chatHistory.ReceiverEmailId, StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(em.EmailId, chatHistory.SenderEmailId, StringComparison.OrdinalIgnoreCase) == 0).ToList();
                            int receiverCredId    = 0;
                            int senderCredId      = 0;
                            string senderUserType = string.Empty;
                            if (chatsenderReceiverDetails != null)
                            {
                                receiverCredId = chatsenderReceiverDetails.Where(em => em.EmailId.Equals(chatHistory.ReceiverEmailId, StringComparison.OrdinalIgnoreCase)).Select(cr => cr.Id).FirstOrDefault();
                                senderCredId   = chatsenderReceiverDetails.Where(em => em.EmailId.Equals(chatHistory.SenderEmailId, StringComparison.OrdinalIgnoreCase)).Select(cr => cr.Id).FirstOrDefault();
                                senderUserType = chatsenderReceiverDetails.Where(em => em.EmailId.Equals(chatHistory.SenderEmailId, StringComparison.OrdinalIgnoreCase)).Select(cr => cr.UserType).FirstOrDefault();
                            }

                            if (chatHistory != null && receiverCredId > 0 && senderCredId > 0 && !string.IsNullOrEmpty(chatHistory.Message))
                            {
                                bool isRead = false;
                                if (!chatHistory.IsOffine)
                                {
                                    isRead = true;
                                }
                                tblChatHistory objChatHistory = new tblChatHistory()
                                {
                                    ReceiverCredId     = receiverCredId,
                                    SenderCredId       = senderCredId,
                                    Message            = chatHistory.Message,
                                    IsRead             = isRead,
                                    TrasactionDateTime = Convert.ToDateTime(chatHistory.TrasactionDateTime),
                                };
                                dataContext.ChatHistory.Add(objChatHistory);
                                dataContext.SaveChanges();
                                if (chatHistory.IsOffine)
                                {
                                    string message         = string.Empty;
                                    allactiveDevices       = PushNotificationBL.GetLastUserDeviceID(receiverCredId);
                                    string senderFirstName = string.Empty;
                                    if (senderUserType.Equals(Message.UserTypeUser, StringComparison.OrdinalIgnoreCase))
                                    {
                                        senderFirstName = (from usr in dataContext.User
                                                           join crd in dataContext.Credentials
                                                           on usr.UserId equals crd.UserId
                                                           where crd.Id == senderCredId && crd.UserType == Message.UserTypeUser
                                                           select usr.FirstName + " " + usr.LastName).FirstOrDefault();
                                    }
                                    else if (senderUserType.Equals(Message.UserTypeTrainer, StringComparison.OrdinalIgnoreCase))
                                    {
                                        senderFirstName = (from usr in dataContext.Trainer
                                                           join crd in dataContext.Credentials
                                                           on usr.TrainerId equals crd.UserId
                                                           where crd.Id == senderCredId && crd.UserType == Message.UserTypeTrainer
                                                           select usr.FirstName + " " + usr.LastName).FirstOrDefault();
                                    }
                                    message = senderFirstName + " sent you a message!";
                                    bool isSaveSuccessfully = false;
                                    foreach (UserNotification objuserNotification in allactiveDevices)
                                    {
                                        // If Deives Token null then by pass to sending push notification
                                        if (!string.IsNullOrEmpty(objuserNotification.DeviceID))
                                        {
                                            if (!isSaveSuccessfully)
                                            {
                                                notificationType = ConstantHelper.constChatNotification;
                                                tblUserNotifications objdeviceNft = new tblUserNotifications
                                                {
                                                    SenderCredlID    = senderCredId,
                                                    ReceiverCredID   = receiverCredId,
                                                    NotificationType = notificationType,
                                                    SenderUserName   = senderFirstName,
                                                    Status           = true,
                                                    IsRead           = false,
                                                    TargetID         = 0,
                                                    CreatedDate      = DateTime.Parse(chatHistory.TrasactionDateTime),
                                                    TokenDevicesID   = objuserNotification.DeviceID
                                                };
                                                dataContext.UserNotifications.Add(objdeviceNft);
                                                dataContext.SaveChanges();
                                                isSaveSuccessfully = true;
                                            }
                                            PushNotificationiOSAndriod objPushNotificationiOSAndriod = new PushNotificationiOSAndriod();
                                            int totalNotificationcount = dataContext.ChatHistory.Count(ch => ch.ReceiverCredId == receiverCredId && ch.IsRead == false);
                                            int totalbudget            = CommonWebApiBL.GetTotalUSerNotification(receiverCredId, objuserNotification.DeviceID);
                                            if (objuserNotification.DeviceType.Equals(DeviceType.IOS.ToString(), StringComparison.OrdinalIgnoreCase))
                                            {
                                                objPushNotificationiOSAndriod.SendPushNotificationForiOS(objuserNotification.DeviceID, message, notificationType, totalNotificationcount, certificate, totalbudget, 0);
                                            }
                                            else if (objuserNotification.DeviceType.Equals(DeviceType.Android.ToString(), StringComparison.OrdinalIgnoreCase))
                                            {
                                                objPushNotificationiOSAndriod.SendPushNotificationForAndriod(objuserNotification.DeviceID, message, notificationType, totalNotificationcount, totalbudget, 0);
                                            }
                                        }
                                    }
                                    message = string.Empty;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.LogManagerInstance.WriteErrorLog(ex);
                        }
                        finally
                        {
                            traceLog.AppendLine("End: SendChatNotification()  --- " + DateTime.Now.ToLongDateString());
                            LogManager.LogManagerInstance.WriteTraceLog(traceLog);
                            traceLog         = null;
                            allactiveDevices = null;
                        }
                    }
                }
            }).Start();
        }