public void GetMessageList(string username)
        {
            business = new ConversationBusiness(_db);
            var id = Context.ConnectionId;
            var fromUserDetail = ConnectedUsers.Where(x => x.ConnectionId == id).FirstOrDefault();
            var fromUser = _db.Users.Where(x => x.Username.Equals(fromUserDetail.Username)).FirstOrDefault();
            var toUser = _db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault();
            var toUserDetail = ConnectedUsers.Where(x => x.Username.Equals(username)).FirstOrDefault();

            //Check if toUserDetail is not online yet
            if (toUserDetail == null)
            {
                toUserDetail = new UserDetail
                {
                    FullName = toUser.FullName,
                    Username = toUser.Username,
                    IsOnline = false,
                    ProfilePicture = toUser.ProfilePicture
                };
            }

            var doctor = _db.Doctors.Where(u => u.Username.Equals(fromUser.Username)).FirstOrDefault();
            var patient = new Patient();
            if (doctor == null)
            {
                doctor = _db.Doctors.Where(u => u.Username.Equals(toUser.Username)).FirstOrDefault();
                patient = _db.Patients.Where(u => u.Username.Equals(fromUser.Username)).FirstOrDefault();
            }
            else
            {
                patient = _db.Patients.Where(u => u.Username.Equals(toUser.Username)).FirstOrDefault();
            }
            if (doctor != null && patient != null)
            {
                Debug.WriteLine("Patient: " + patient.Username + "  " + "Doctor: " + doctor.Username);
                DoctorDetail doctorDetail = Doctors.Where(u => u.Username.Equals(doctor.Username)).FirstOrDefault();
                var newestConversation = _db.Conversations.Where(
                    con => (con.PatientId == patient.UserId) && (con.DoctorId == doctor.UserId))
                    .OrderByDescending(con => con.DateConsulted).FirstOrDefault();

                if (newestConversation != null)
                {
                    var conversationDetails = _db.ConversationDetails.Where(cd => cd.Conversation.ConversationId == newestConversation.ConversationId).
                    OrderBy(cd => cd.CreatedDate).ToList();

                    //Convert conversationDetails to MessageDetails
                    var messageDetails = new List<MessageDetail>();
                    foreach (var conversationDetail in conversationDetails)
                    {
                        String date = (DateTime.Now.Subtract(conversationDetail.CreatedDate).Days) > 1 ?
                            String.Format("{0:HH:mm:ss}", conversationDetail.CreatedDate) :
                            String.Format("{0:dd/mm/yyyy HH:mm:ss}", conversationDetail.CreatedDate);
                        MessageDetail messageDetail = new MessageDetail
                        {
                            Content = conversationDetail.Content,
                            CreatedDate = date,
                            Username = conversationDetail.User.Username,
                            IsRead = conversationDetail.IsRead
                        };
                        messageDetails.Add(messageDetail);
                    }
                    Debug.WriteLine(messageDetails.Count);
                    if (fromUserDetail.Username.Equals(doctor.Username))
                    {
                        business.MarkConversationAsRead(newestConversation);
                        this.ConnectDoctor(doctor.Username);
                    }
                    Clients.Caller.onGetMessageList(fromUserDetail, toUserDetail, messageDetails);
                }
                else
                {
                    Clients.Caller.onGetMessageList(fromUserDetail, toUserDetail, "");
                }
            }
        }
        public void SendMessageTo(string toUsername, string message)
        {
            business = new ConversationBusiness(_db);
            Debug.WriteLine(toUsername + " " + message);
            var id = Context.ConnectionId;
            var fromUserDetail = ConnectedUsers.Where(x => x.ConnectionId == id).FirstOrDefault();
            var fromUser = _db.Users.Where(x => x.Username.Equals(fromUserDetail.Username)).FirstOrDefault();
            var toUser = _db.Users.Where(x => x.Username.Equals(toUsername)).FirstOrDefault();
            var toUserDetail = ConnectedUsers.Where(x => x.Username.Equals(toUsername)).FirstOrDefault();

            var doctor = _db.Doctors.Where(u => u.Username.Equals(fromUser.Username)).FirstOrDefault();
            var patient = new Patient();
            if (doctor == null)
            {
                doctor = _db.Doctors.Where(u => u.Username.Equals(toUser.Username)).FirstOrDefault();
                patient = _db.Patients.Where(u => u.Username.Equals(fromUser.Username)).FirstOrDefault();
            }
            else
            {
                patient = _db.Patients.Where(u => u.Username.Equals(toUser.Username)).FirstOrDefault();
            }
            if (doctor != null && patient != null)
            {
                //Store in database
                Conversation conversation = _db.Conversations.Where(
                    x => (x.PatientId == patient.UserId && x.DoctorId == doctor.UserId))
                .OrderByDescending(x => x.DateConsulted).FirstOrDefault();

                Debug.WriteLine("DoctorId = " + toUser.UserId, "  PatientId = " + fromUser.UserId);
                if (conversation == null)
                {
                    conversation = new Conversation
                    {
                        DoctorId = toUser.UserId,
                        PatientId = fromUser.UserId,
                        DateConsulted = DateTime.Now,
                        LatestTimeFromDoctor = DateTime.Now,
                        LatestTimeFromPatient = DateTime.Now
                    };
                    _db.Conversations.Add(conversation);
                }

                conversation.LatestTimeFromPatient = DateTime.Now;
                conversation.LatestContentFromPatient = message;
                conversation.IsRead = false;

                ConversationDetail conversationDetail = new ConversationDetail
                {
                    UserId = fromUser.UserId,
                    Content = message,
                    Conversation = conversation,
                    CreatedDate = DateTime.Now,
                    IsRead = false
                };

                MessageDetail messageDetail = new MessageDetail
                {
                    Content = message,
                    Username = fromUser.Username,
                    CreatedDate = String.Format("{0:H:mm:ss}", DateTime.Now),
                    IsRead = false
                };

                _db.ConversationDetails.Add(conversationDetail);
                _db.SaveChanges();

                //Notify Receiver
                var receivers = ConnectedUsers.Where(x => x.Username == toUsername).ToList();
                if (receivers != null)
                {
                    foreach (var receiver in receivers)
                    {
                        receiver.CountMessageUnRead = business.CountMessageUnRead(toUser);
                        if (receiver != null && receiver.ConnectionId != null)
                            Clients.Client(receiver.ConnectionId).messageReceived(fromUserDetail, toUserDetail, messageDetail);
                    }
                }

                //Notify Caller
                Clients.Caller.messageReceived(fromUserDetail, toUserDetail, messageDetail);
            }
        }
        public void GetLastestChatMessage(string toEmail)
        {
            var id = Context.ConnectionId;
            var fromUserDetail = ConnectedUsers.Where(x => x.ConnectionId == id).FirstOrDefault();
            var fromUser = _db.Users.Where(x => x.Email.Equals(fromUserDetail.Email)).FirstOrDefault();
            var toUser = _db.Users.Where(x => x.Email.Equals(toEmail)).FirstOrDefault();
            var toUserDetail = ConnectedUsers.Where(x => x.Email.Equals(toEmail)).FirstOrDefault();

            var doctor = _db.Doctors.Where(u => u.Email.Equals(fromUser.Email)).FirstOrDefault();
            var patient = new Patient();
            if (doctor == null)
            {
                doctor = _db.Doctors.Where(u => u.Email.Equals(toUser.Email)).FirstOrDefault();
                patient = _db.Patients.Where(u => u.Email.Equals(fromUser.Email)).FirstOrDefault();
            }
            else
            {
                patient = _db.Patients.Where(u => u.Email.Equals(toUser.Email)).FirstOrDefault();
            }

            if (doctor != null && patient != null)
            {
                var conversation = _db.Conversations.Where(x => x.DoctorId == doctor.UserId && x.PatientId == patient.UserId).FirstOrDefault();
                var lastestConversationDetail = _db.ConversationDetails.Where(x => x.ConversationId == conversation.ConversationId).OrderByDescending(x => x.CreatedDate).FirstOrDefault();

                MessageDetail messageDetail = new MessageDetail
                {
                    Content = lastestConversationDetail.Content,
                    Attachment = lastestConversationDetail.Attachment,
                    CreatedDate = String.Format("{0:dd/MM/yyyy HH:mm:ss}", lastestConversationDetail.CreatedDate),
                    Email = lastestConversationDetail.User.Email,
                    IsRead = lastestConversationDetail.IsRead
                };

                //Check if toUserDetail is not online yet
                if (toUserDetail == null)
                {
                    toUserDetail = helper.ConvertUserToUserDetail(toUser);
                }

                //Notify Receiver
                var receiver = ConnectedUsers.Where(x => x.Email == toEmail).FirstOrDefault();

                if (receiver != null && receiver.ConnectionId != null)
                {
                    receiver.CountMessageUnRead = business.CountMessageUnRead(toUser);
                    Clients.Client(receiver.ConnectionId).messageReceived(fromUserDetail, toUserDetail, messageDetail);
                }

                //Notify Caller
                Clients.Caller.messageReceived(fromUserDetail, toUserDetail, messageDetail);
            }
        }