public static void Seed(OMCS.DAL.Model.OMCSDBContext _db)
        {
            Doctor bsNguyenVanA = _db.Doctors.Where(d => d.Email.Equals("*****@*****.**")).FirstOrDefault();
            Patient sutran = _db.Patients.Where(p => p.Email.Equals("*****@*****.**")).FirstOrDefault();
            Conversation suTranVsBacSiA = new Conversation
            {
                Doctor = bsNguyenVanA,
                Patient = sutran
            };

            ConversationDetail line1 = new ConversationDetail
            {
                Conversation = suTranVsBacSiA, Content = "Chào bác sĩ",
                User = sutran, CreatedDate = new DateTime(2014, 6, 1, 8, 20, 12)
            };

            ConversationDetail line2 = new ConversationDetail
            {
                Conversation = suTranVsBacSiA, Content = "Chào bạn",
                User = bsNguyenVanA,
                CreatedDate = new DateTime(2014, 6, 1, 8, 20, 40)
            };

            ConversationDetail line3 = new ConversationDetail
            {
                Conversation = suTranVsBacSiA,
                Content = "Em xin hỏi, nếu đau đầu mà tai và mắt cứ giật liên hồi, " +
                "sau mỗi lần giật lại càng đau đầu hơn, kèm theo sốt, thì nguyên nhân do đâu?",
                User = sutran, CreatedDate = new DateTime(2014, 6, 1, 8, 22, 12)
            };

            ConversationDetail line4 = new ConversationDetail
            {
                Conversation = suTranVsBacSiA,
                Content = "Đau đầu là một trong những triệu chứng thường gặp "+
                "nhất của nhiều bệnh, có nhiều nguyên nhân khác nhau gây đau đầu, "+
                "cảm giác đau ở một trong những điểm như: đau ở ngay phía trên 2 mắt, "+
                "2 tai, đau ở phía sau gáy, vùng trên của cổ.",
                User = bsNguyenVanA,
                CreatedDate = new DateTime(2014, 6, 1, 8, 25, 12)
            };

            ConversationDetail line5 = new ConversationDetail
            {
                Conversation = suTranVsBacSiA,
                Content = "Đau đầu là một trong những triệu chứng thường gặp " +
                "nhất của nhiều bệnh, có nhiều nguyên nhân khác nhau gây đau đầu, " +
                "cảm giác đau ở một trong những điểm như: đau ở ngay phía trên 2 mắt, " +
                "2 tai, đau ở phía sau gáy, vùng trên của cổ.",
                User = sutran,
                CreatedDate = new DateTime(2014, 6, 1, 8, 27, 12)
            };

            _db.ConversationDetails.Add(line1);
            _db.ConversationDetails.Add(line2);
            _db.ConversationDetails.Add(line3);
            _db.ConversationDetails.Add(line4);
            _db.ConversationDetails.Add(line5);
            _db.SaveChanges();
        }
        public void SendPrivateMessage(string toUserId, string message, int conversationid, string username)
        {
            string fromUserId = Context.ConnectionId;

            var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId);
            toUser.CountMessageUnRead += 1;
            var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);

            if (toUser != null && fromUser != null)
            {
                // send to
                Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.Username, message);

                // send to caller doctor
                Clients.Caller.sendPrivateMessage(toUserId, fromUser.Username, message);

                Clients.Client(toUserId).sendPrivateMessageToDoctor(toUser.CountMessageUnRead);
                //Debug.WriteLine(toUser.CountMessageUnRead);
                //Debug.WriteLine("Da vao day roi ne");
                //Debug.WriteLine(toUserId + fromUserId);
            }

            var conversation = _db.Conversations.Find(conversationid);
            var user = _db.Users.Where(u => u.Username == username).FirstOrDefault();
            var conversationDetail = new ConversationDetail
            {
                Conversation = conversation,
                Content = message,
                CreatedDate = DateTime.Now,
                User = user
            };
            //_db.Conversations.Add(conversation);
            Debug.WriteLine(conversationid);
            Debug.WriteLine(username);
            _db.ConversationDetails.Add(conversationDetail);
            _db.SaveChanges();
        }
        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 JsonResult Upload(string fromEmail, string toEmail)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase file = Request.Files[i]; //Uploaded file
                //Use the following properties to get file's name, size and MIMEType
                if (file != null)
                {
                    int fileSize = file.ContentLength;
                    string fileName = file.FileName;
                    string mimeType = file.ContentType;
                    System.IO.Stream fileContent = file.InputStream;
                    //To save file, use SaveAs method
                    file.SaveAs(Server.MapPath("~/Content/Upload/") + fileName); //File will be saved in application root
                    var fromUser = _db.Users.Where(x => x.Email.Equals(fromEmail)).FirstOrDefault();
                    var toUser = _db.Users.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();
                    }

                    Conversation conversation = _db.Conversations.Where(x => (x.PatientId == patient.UserId && x.DoctorId == doctor.UserId)).FirstOrDefault();

                    if (conversation == null)
                    {
                        conversation = new Conversation
                        {
                            DoctorId = doctor.UserId,
                            PatientId = patient.UserId,
                            LatestTimeFromDoctor = DateTime.Now,
                            LatestTimeFromPatient = DateTime.Now
                        };
                        _db.Conversations.Add(conversation);
                    }

                    if (fromUser.Email == patient.Email)
                    {
                        conversation.LatestTimeFromPatient = DateTime.Now;
                        conversation.LatestContentFromPatient = fileName;
                        conversation.IsDoctorRead = false;
                    }
                    else
                    {
                        conversation.LatestTimeFromDoctor = DateTime.Now;
                        conversation.LatestContentFromDoctor = fileName;
                        conversation.IsPatientRead = false;
                    }
                    String date = String.Format("{0:dd/MM/yyyy HH:mm:ss}", DateTime.Now);
                    IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
                    ConversationDetail conversationDetail = new ConversationDetail
                    {
                        UserId = fromUser.UserId,
                        Attachment = fileName,
                        Conversation = conversation,
                        CreatedDate = DateTime.Parse(date, culture, System.Globalization.DateTimeStyles.AssumeLocal),
                        IsRead = false
                    };
                    _db.ConversationDetails.Add(conversationDetail);
                    _db.SaveChanges();
                }

            }
            return Json("Đã upload " + Request.Files.Count + " dữ liệu");
        }