private VisitProposition GetVisitByDoctor(DateTime when, string DoctorId, Guid? ClinicId)
        {
            VisitProposition proposition = null;
            do
            {
                var day = when.DayOfWeek;
                List<Workday> workday;
                // in case we're interested in particular doctor only
                if (ClinicId == null)
                {
                    workday = db.Workdays.Where(w => w.DoctorId == DoctorId).Where(w => w.Day == day).ToList();
                }
                // in case we're interested in doctor within particular clinic
                else
                {
                    workday = db.Workdays.Where(w => w.DoctorId == DoctorId).Where(w => w.ClinicId == ClinicId).Where(w => w.Day == day).ToList();
                }

                if (workday.Count == 0)
                {
                    when = when.AddHours(24 - when.Hour + 5);
                    continue;
                }
                else
                {
                    // getting hours of visits for the day
                    var visits = db.Visits.Where(v => v.DoctorId == DoctorId).ToList().Where(v => DateTime.Compare(v.StartDateTime.Date, when.Date) == 0).Select(v => v.StartDateTime).OrderBy(t => t.TimeOfDay).ToList();
                    foreach (var plan in workday)
                    {
                        DateTime start = plan.StartHour;
                        // preparing when, to make sure that we schedule to visit which WILL happen
                        if (when.TimeOfDay > plan.StartHour.TimeOfDay)
                        {
                            if (when.Minute > 30)
                            {
                                // to avoid date switch
                                if (when.AddHours(1).Date == when.Date)
                                {
                                    when = when.AddHours(1);
                                    when = when.AddMinutes(-when.Minute);
                                }
                            }
                            else if (when.Minute > 0)
                            {
                                when = when.AddMinutes(30 - when.Minute);
                            }
                            start = plan.StartHour.AddHours(when.Hour - plan.StartHour.Hour);
                            start = start.AddMinutes(when.Minute - plan.StartHour.Minute);
                        }
                        // looking for 30 minutes gap
                        for (DateTime i = start; i < plan.EndHour; i = i.AddMinutes(30))
                        {
                            bool flag = false;
                            foreach (var visit in visits)
                            {
                                // proposed time overlaps with existing visit
                                if (i.TimeOfDay == visit.TimeOfDay)
                                {
                                    flag = true;
                                    break;
                                }
                            }
                            // no visit on proposed time
                            if (!flag)
                            {
                                var dt = new DateTime(when.Year, when.Month, when.Day, i.Hour, i.Minute, 0);
                                proposition = new VisitProposition() { DoctorId = DoctorId, ClinicId = plan.ClinicId, StartDateTime = dt };
                                return proposition;
                            }
                        }
                    }
                    // all visits are scheduled already, let's move to another day
                    when = when.AddHours(24 - when.Hour + 5);
                }
            }
            while (true);
        }
 public ActionResult Proposition(VisitProposition model)
 {
     var doctor = UserManager.FindById(model.DoctorId);
     var clinic = db.Clinics.Find(model.ClinicId);
     return View(new VisitPropositionViewModel()
     {
         ClinicId = model.ClinicId,
         DoctorId = model.DoctorId,
         StartDateTime = model.StartDateTime,
         Clinic = clinic,
         Doctor = doctor,
         Date = model.StartDateTime,
         Time = model.StartDateTime,
         Day = model.StartDateTime.DayOfWeek
     });
 }