/// <summary>
        /// This is the method for creating a new UserAccount. It takes first name, last name, phone number, address, city, state, zip, and country as parameters.
        /// </summary>
        public static UserAccount CreateUserAccount(string firstName, string lastName, string email)
        {
            using (var db = new LanguageExchangeModel())
            {
                var user = new UserAccount();
                user.UserFirstName = firstName;
                user.UserLastName = lastName;
                user.UserEmail = email;
                db.UserAccounts.Add(user);
                db.SaveChanges();

                SystemEmail("Welcome to Xchange (http://languagexchange.azurewebsites.net/)! Don't forget to fill out your profile so you can start connecting with other language learners!", email);

                return user;
            }
        }
        public static void CheckForAll(string email)
        {
            using (var db = new LanguageExchangeModel())
            {
                var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
                if (user == null)
                {
                    return;
                }
                var userId = user.UserIdNumber;
                var pref = db.UserPreferences.Where(i => i.UserId == userId).FirstOrDefault();
                if (pref == null)
                {
                    pref = new UserPreferences();
                    pref.UserPreferencesName = user.UserFirstName + " " + user.UserLastName;
                    pref.UserId = userId;
                    pref.UserDescription = "Something interesting about me!";
                    pref.LastLogin = DateTime.Now;
                    db.UserPreferences.Add(pref);
                }
                var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault();
                if (calendar == null)
                {
                    calendar = new AppointmentCalendar();
                    calendar.UserId = userId;
                    db.AppointmentCalendars.Add(calendar);
                }

                var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault();
                if (inbox == null)
                {
                    inbox = new EmailInbox();
                    inbox.UserId = userId;
                    db.EmailInboxes.Add(inbox);
                }

                var tracking = db.UserTrackingDetails.Where(t => t.UserId == userId).FirstOrDefault();
                if (tracking == null)
                {
                    tracking = new UserTrackingDetails();
                    tracking.UserId = userId;
                    db.UserTrackingDetails.Add(tracking);
                }
                db.SaveChanges();
            }
            
        }
 public static AppointmentCalendar ShowAppointmentCalendar(string email)
 {
     var db = new LanguageExchangeModel();
     var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
     var userId = user.UserIdNumber;
     var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault();
     if (calendar == null)
     {
         calendar = new AppointmentCalendar();
         calendar.UserId = userId;
         db.AppointmentCalendars.Add(calendar);
         db.SaveChanges();
     }
     return calendar;
 }
 public static IEnumerable<UserPreferences> GetUserPreferences()
 {
     var db = new LanguageExchangeModel();
     return db.UserPreferences;
 }
 public static UserPreferences ShowUserPreferences (int userId)
 {
     var db = new LanguageExchangeModel();
     var user = db.UserAccounts.Where(u => u.UserIdNumber == userId).FirstOrDefault();
     var pref = db.UserPreferences.Where(i => i.UserId == userId).FirstOrDefault();
     if (pref == null)
     {
         pref = new UserPreferences();
         pref.UserPreferencesName = user.UserFirstName + " " + user.UserLastName;
         pref.UserId = userId;
         db.UserPreferences.Add(pref);
         db.SaveChanges();
     }
     return pref;
 }
 public static void ConfirmAppointment (int id)
 {
     var db = new LanguageExchangeModel();
     var appointment = db.Appointments.Where(a => a.AppointmentId == id).FirstOrDefault();
     
     appointment.AppointmentConfirmedByReceiver = true;
     db.SaveChanges();
 }
 public static String CancelledAppt (string email)
 {
     using (var db = new LanguageExchangeModel())
     {
         var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
         if (user == null)
         {
             return "notcancelled";
         }
         var userId = user.UserIdNumber;
         var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault();
         if (calendar == null)
         {
             return "notcancelled";
         }
         var cancelledAppt = calendar.Appointments.Where(a => a.Cancelled == true
         && a.WhoCancelled != userId).FirstOrDefault();
         if (cancelledAppt == null)
         {
             return "notcancelled";
         }
         else
         {
             return "cancelled";
         }
     }
 }
        public static void MakeAppointment (string email1, int id, string learnOrTeach, Appointments updatedAppointment)
        {
            var db = new LanguageExchangeModel();
            
            var user1 = db.UserAccounts.Where(u => u.UserEmail == email1).FirstOrDefault();
            var userId1 = user1.UserIdNumber;
            var user2 = db.UserAccounts.Where(u => u.UserIdNumber == id).FirstOrDefault();
            var userId2 = user2.UserIdNumber;
            var user2email = user2.UserEmail;
            if (userId1 == userId2)
            {
                return;
            }
            var calendar1 = db.AppointmentCalendars.Where(c => c.UserId == userId1).FirstOrDefault();
            if  (calendar1 == null)
            {
                throw new ArgumentException("Appointment Calendar not valid for the user");
            }
            var calendar2 = db.AppointmentCalendars.Where(c => c.UserId == userId2).FirstOrDefault();
            if (calendar2 == null)
            {
                throw new ArgumentException("Appointment Calendar not valid for the user");
            }
            var appointment = new Appointments();

            if (updatedAppointment.AppointmentStartTime < DateTime.Now)
            {
                return;
            }
            
            appointment.AppointmentStartTime = updatedAppointment.AppointmentStartTime;
            appointment.AppointmentEndTime = updatedAppointment.AppointmentEndTime;
            appointment.AppointmentLanguage = updatedAppointment.AppointmentLanguage;
            appointment.UserId1 = calendar1.UserId;
            appointment.UserId2 = calendar2.UserId;
            appointment.TimeZone = updatedAppointment.TimeZone;
            if (learnOrTeach == "Learning")
            {
                appointment.AppointmentLearner = userId1;
                appointment.AppointmentTeacher = userId2;
            }
            if (learnOrTeach == "Teaching")
            {
                appointment.AppointmentLearner = userId2;
                appointment.AppointmentTeacher = userId1;
            }
            appointment.AppointmentPerson1 = user1.UserFirstName + " " + user1.UserLastName;
            appointment.AppointmentPerson2 = user2.UserFirstName + " " + user2.UserLastName;
            appointment.AppointmentConfirmedBySender = true;
            appointment.AppointmentConfirmedByReceiver = false;

            calendar1.Appointments.Add(appointment);
            calendar2.Appointments.Add(appointment);

            SystemEmail("You have a new appointment request on Xchange (http://languagexchange.azurewebsites.net/). Login to view and confirm.", user2email);

            db.Appointments.Add(appointment);

            db.SaveChanges();
        }
 public static void DeleteAppt(int apptId, string email)
 {
     using (var db = new LanguageExchangeModel())
     {
         var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
         var userId = user.UserIdNumber;
         var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault();
         var appt = calendar.Appointments.Where(a => a.AppointmentId == apptId).FirstOrDefault();
         if (appt == null)
         {
             return;
         }
         appt.Cancelled = true;
         if (appt.WhoCancelled == 0)
         {
             appt.WhoCancelled = userId;
         }
         if (appt.UserId1 == userId)
         {
             appt.UserId1 = -1;
         }
         if (appt.UserId2 == userId)
         {
             appt.UserId2 = -1;
         }
         if (appt.UserId1 == -1 && appt.UserId2 == -1)
         {
             db.Appointments.Remove(appt);
         }
         db.SaveChanges();
     }
 }
        public static void CreateMessage(int id, string email, Messages UpdatedMessage)
        {
            var db = new LanguageExchangeModel();

            var userFrom = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
            var userFromId = userFrom.UserIdNumber;
            var userTo = db.UserAccounts.Where(u => u.UserIdNumber == id).FirstOrDefault();
            var userToId = userTo.UserIdNumber;
            var user2email = userTo.UserEmail;
            var inboxFrom = db.EmailInboxes.Where(i => i.UserId == userFromId).FirstOrDefault();
            if (inboxFrom == null)
            {
                inboxFrom = new EmailInbox();
                inboxFrom.UserId = userFromId;
                db.EmailInboxes.Add(inboxFrom);
                db.SaveChanges();
            }
            var inboxTo = db.EmailInboxes.Where(i => i.UserId == userToId).FirstOrDefault();
            if (inboxTo == null)
            {
                inboxTo = new EmailInbox();
                inboxTo.UserId = userToId;
                db.EmailInboxes.Add(inboxTo);
                db.SaveChanges();
            }
            var message = new Messages();
            message.InboxId1 = userFromId;
            message.InboxId2 = userToId;
            var userFromPrefs = db.UserPreferences.Where(p => p.UserId == userFromId).FirstOrDefault();
            if (userFromPrefs == null)
            {
                message.MessageFrom = userFrom.UserFirstName;
            }
            message.MessageFrom = userFromPrefs.UserPreferencesName;
            var userToPrefs = db.UserPreferences.Where(p => p.UserId == userToId).FirstOrDefault();
            if (userToPrefs == null)
            {
                message.MessageTo = userTo.UserFirstName;
            }
            message.MessageTo = userToPrefs.UserPreferencesName;
            message.MessageHeader = UpdatedMessage.MessageHeader;
            message.MessageContent = UpdatedMessage.MessageContent;
            message.MessageTimeStamp = DateTime.Now;
         
            inboxFrom.Messages.Add(message);
            inboxTo.Messages.Add(message);
            db.Messages.Add(message);

            SystemEmail("You have a new message on Xchange (http://languagexchange.azurewebsites.net/). Login to view.", user2email);

            db.SaveChanges();

            
        }
 public static Messages ShowMessage(int id)
 {
     using (var db = new LanguageExchangeModel())
     {
         return db.Messages.Where(i => i.MessageId == id).FirstOrDefault();
     }
 }
 public static IEnumerable<UserPreferences> SearchForLanguage(string targetLanguage, string learnerOrTeacher)
 {
     var db = new LanguageExchangeModel();
     if (learnerOrTeacher == "Learner")
     {
         return db.UserPreferences.Where(l => l.UserPrimaryLearningLanguage == targetLanguage || 
         l.UserOtherLearningLanguage == targetLanguage);
     }
     else if (learnerOrTeacher == "Teacher") {
         return db.UserPreferences.Where(l => l.UserPrimaryTeachingLanguage == targetLanguage ||
         l.UserOtherTeachingLanguage == targetLanguage);
     }
     else
     {
         return db.UserPreferences.Where(l => l.UserPrimaryTeachingLanguage == targetLanguage ||
     l.UserPrimaryLearningLanguage == targetLanguage || l.UserOtherLearningLanguage == targetLanguage ||
     l.UserOtherTeachingLanguage == targetLanguage);
     }
 }
 public static Appointments ShowAppointment(int id)
 {
     var db = new LanguageExchangeModel();
     return db.Appointments.Where(i => i.AppointmentId == id).FirstOrDefault();
 }
        public static IEnumerable<Appointments> ShowAllAppointments(string email)
        {
            var db = new LanguageExchangeModel();
            var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
            var userId = user.UserIdNumber;
            var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault();
            var appts = calendar.Appointments.Where(a => a.UserId1 == userId || a.UserId2 == userId);

            var sortedAppts = appts.OrderByDescending(x => x.AppointmentStartTime);

            return sortedAppts;
        }
 public static EmailInbox ShowEmailInbox(string email)
 {
     var db = new LanguageExchangeModel();
     var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
     var userId = user.UserIdNumber;
     var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault();
     if (inbox == null)
     {
         inbox = new EmailInbox();
         inbox.UserId = userId;
         db.EmailInboxes.Add(inbox);
         db.SaveChanges();
     }
     return inbox;
 }
 public static void DeletePrefs (UserPreferences prefs)
 {
     using (var db = new LanguageExchangeModel())
     {
         db.UserPreferences.Remove(prefs);
         db.SaveChanges();
     }
 }
 public static void DeleteMessage(int messageId, string email)
 {
     using (var db = new LanguageExchangeModel())
     {
         var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
         var userId = user.UserIdNumber;
         var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault();
         var message = inbox.Messages.Where(m => m.MessageId == messageId).FirstOrDefault();
         if (message == null)
         {
             return;
         }
         if (message.InboxId1 == userId)
         {
             message.InboxId1 = -1;
         }
         if (message.InboxId2 == userId)
         {
             message.InboxId2 = -1;
         }
         if (message.InboxId1 == -1 && message.InboxId2 == -1)
         {
             db.Messages.Remove(message);
         }
         db.SaveChanges();
     }
 }
        public static void sendMessage(Messages UpdatedMessage)
        {
            using (var db = new LanguageExchangeModel())
            {
                var message = db.Messages.Where(m => m.MessageId == UpdatedMessage.MessageId).FirstOrDefault();
                if (message == null)
                {
                    return;
                }
                var originalMessage = message;
                message.MessageHeader = UpdatedMessage.MessageHeader;
                message.MessageContent = UpdatedMessage.MessageContent;
                message.MessageTimeStamp = DateTime.Now;

                db.Entry(originalMessage).CurrentValues.SetValues(message);
                db.SaveChanges();
            }
        }
        public static String ApptCancelled (string email, int id)
        {
            var db = new LanguageExchangeModel();
            var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
            var userId = user.UserIdNumber;
            var calendar = db.AppointmentCalendars.Where(c => c.UserId == userId).FirstOrDefault();
            var appt = calendar.Appointments.Where(a => a.AppointmentId == id).FirstOrDefault();

            if (appt.Cancelled == true)
            {
                return "cancelled";
            }
            else
            {
                return "notcancelled";
            }
        }
        public static IEnumerable<Messages> ShowInboxMessages (string email)
        {
            var db = new LanguageExchangeModel();
            var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
            var userId = user.UserIdNumber;
            var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault();
            if (inbox == null)
            {
                inbox = new EmailInbox();
                inbox.UserId = userId;
                
            }
            var messages = inbox.Messages.Where(m => m.InboxId2 == userId);
            if (messages == null)
            {
                var message = new Messages();
                message.InboxId1 = 1;
                message.InboxId2 = userId;
                message.MessageHeader = "Welcome to Xchange!";
                message.MessageContent = "Welcome to Xchange! We're glad you're here. Fill out your profile details and search for other language learners to get started.";
                message.MessageTimeStamp = DateTime.Now;
                message.MessageTo = user.UserFirstName;
                message.MessageFrom = "Xchange Team";

                inbox.Messages.Add(message);
                
                db.Messages.Add(message);

                db.SaveChanges();
            }
            var sortedMessages = messages.OrderByDescending(x => x.MessageTimeStamp);

            return sortedMessages;
        }
 public static String NewAppt (string email)
 {
     using (var db = new LanguageExchangeModel())
     {
         var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
         if (user == null)
         {
             return ("oldappt");
         }
         var userId = user.UserIdNumber;
         var newAppt = db.Appointments.Where(a => a.UserId2 == userId && a.AppointmentConfirmedByReceiver == false).FirstOrDefault();
         if (newAppt == null)
         {
             return ("oldappt");
         }
         else
         {
             return ("newappt");
         }
     }
 }
        public static String NewMail (string email)
        {
            using (var db = new LanguageExchangeModel())
            {
                var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
                if (user == null)
                {
                    return ("oldmail");
                }
                var userId = user.UserIdNumber;
                var newMail = db.Messages.Where(m => m.InboxId2 == userId && m.Read == false).FirstOrDefault();
                if (newMail == null)
                {
                    return ("oldmail");
                }
                else
                {
                    return ("newmail");
                }

            }
        }
 public static String IsAppointmentConfirmable (string email, int appointmentId)
 {
     var db = new LanguageExchangeModel();
     var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
     var userId = user.UserIdNumber;
     var appointment = db.Appointments.Where(a => a.AppointmentId == appointmentId).FirstOrDefault();
     if (appointment.UserId1 != userId && appointment.AppointmentConfirmedByReceiver == false 
         && appointment.UserId1 != -1 && appointment.UserId2 != -1)
     {
         return "confirmable";
     }
     else
     {
         return "unconfirmable";
     }
 }
 public static void Rate(string email, int rating, int apptId)
 {
     using (var db = new LanguageExchangeModel())
     {
         var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
         var userId = user.UserIdNumber;
         var appt = db.Appointments.Where(a => a.AppointmentId == apptId).FirstOrDefault();
         if (appt.AppointmentTeacher == userId && rating > 0 && rating < 5)
         {
             appt.LearnerRating = rating;
         }
         if (appt.AppointmentLearner == userId && rating > 0 && rating < 5)
         {
             appt.TeacherRating = rating; 
         }
         
         db.SaveChanges();
     }
 }
        public static void UpdateUserPreferences(string email, UserPreferences updatedPreferences)
        {
            using (var db = new LanguageExchangeModel())
            {
                var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
                var pref = db.UserPreferences.Where(p => p.UserId == user.UserIdNumber).FirstOrDefault();
                if (pref == null)
                {
                    return;
                }
                var originalPrefs = pref;
                pref.UserPrimaryTeachingLanguage = updatedPreferences.UserPrimaryTeachingLanguage;
                pref.UserPrimaryLearningLanguage = updatedPreferences.UserPrimaryLearningLanguage;
                pref.UserOtherTeachingLanguage = updatedPreferences.UserOtherTeachingLanguage;
                pref.UserOtherLearningLanguage = updatedPreferences.UserOtherLearningLanguage;
                pref.UserSkype = updatedPreferences.UserSkype;
                pref.UserPrefersTalkOrText = updatedPreferences.UserPrefersTalkOrText;
                pref.UserTimeZone = updatedPreferences.UserTimeZone;
                pref.UserPhoto = updatedPreferences.UserPhoto;
                pref.UserDescription = updatedPreferences.UserDescription;
                pref.UserPreferencesName = updatedPreferences.UserPreferencesName;
                pref.UserCountry = updatedPreferences.UserCountry;

                db.Entry(originalPrefs).CurrentValues.SetValues(pref);
                db.SaveChanges(); 
            }
        }
 public static string Rateable(string email, int id)
 {
     using (var db = new LanguageExchangeModel())
     {
         var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
         var userId = user.UserIdNumber;
         var appt = db.Appointments.Where(a => a.AppointmentId == id).FirstOrDefault();
         if (appt.AppointmentTeacher == userId && appt.LearnerRating != 0 || appt.AppointmentLearner == userId && appt.TeacherRating != 0)
         {
             return "unrateable";
         }
         if (appt.Cancelled == false && appt.AppointmentEndTime < DateTime.Now && 
             appt.AppointmentConfirmedByReceiver == true && appt.AppointmentConfirmedBySender == true)
         {
             return "rateable";
         }
         else
         {
             return "unrateable";
         }
     }
 }
 public static UserPreferences ShowMyUserPreferences(string email)
 {
     var db = new LanguageExchangeModel();
     var userAccount = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
     var userId = userAccount.UserIdNumber;
     var pref = db.UserPreferences.Where(i => i.UserId == userId).FirstOrDefault();
     if (pref == null)
     {
         pref = new UserPreferences();
         pref.UserPreferencesName = userAccount.UserFirstName + " " + userAccount.UserLastName;
         pref.UserId = userId;
         pref.UserDescription = "Something interesting about me!";
         pref.LastLogin = DateTime.Now;
         db.UserPreferences.Add(pref);
         db.SaveChanges();
     }
     return pref;
 }
 public static void MessageRead(int id)
 {
     var db = new LanguageExchangeModel();
     var message = db.Messages.Where(i => i.MessageId == id).FirstOrDefault();
     message.Read = true;
     db.SaveChanges();
 }
 public static string GetName (int id)
 {
     using (var db = new LanguageExchangeModel())
     {
         var info = db.UserAccounts.Where(a => a.UserIdNumber == id).FirstOrDefault();
         var realName = info.UserFirstName;
         var prefs = db.UserPreferences.Where(p => p.UserId == id).FirstOrDefault();
         if (prefs == null)
         {
             return realName;
         }
         var name = prefs.UserPreferencesName;
         return name;
     }
 }
 public static IEnumerable<Messages> ShowSentMessages(string email)
 {
     var db = new LanguageExchangeModel();
     var user = db.UserAccounts.Where(u => u.UserEmail == email).FirstOrDefault();
     var userId = user.UserIdNumber;
     var inbox = db.EmailInboxes.Where(i => i.UserId == userId).FirstOrDefault();
     var messages = inbox.Messages.Where(m => m.InboxId1 == userId);
     return messages;
 }