public ActionResult MakeAppointment(Appointments appointment)
 {
     return View(appointment);
 }
        public ActionResult MakeAppointment(int id, Appointments appointment, FormCollection formControls)
        {
            appointment.UserId2 = id;
            var month = formControls["Month"];
            var day = formControls["Day"];
            var year = formControls["Year"];
            var startHour = formControls["Hour"];
            var startMinute = formControls["Minute"];
            var ampm = formControls["ampm"];
         
            var endHour = formControls["EndHour"];
            var endMinute = formControls["EndMinute"];
            var endampm = formControls["Endampm"];
         
            var startTime = month + "/" + day + "/" + year + " " + startHour + ":" + startMinute + ":00 " + ampm;
            var endTime = month + "/" + day + "/" + year + " " + endHour + ":" + endMinute + ":00 " + endampm;
            var convertedStart = Convert.ToDateTime(startTime);
            var convertedEnd = Convert.ToDateTime(endTime);

            var language = formControls["language"];
            var learnOrTeach = formControls["LearnOrTeach"];

            appointment.AppointmentLanguage = language;
            appointment.AppointmentStartTime = convertedStart;
            appointment.AppointmentEndTime = convertedEnd;
      
            LanguageExchangeFactory.MakeAppointment(User.Identity.Name, id, learnOrTeach, appointment);
            return RedirectToAction("Index");
        }
        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();
        }