public ActionResult MapSchedule(int conferenceId)
 {
     var result = ConferenceLayer.GetConfRoomsForConference(IbaDb, conferenceId);
     ConferenceLayer.CreateScheduleEventLocationsIfDoesntExist(Db, result);
     var vm = new MapScheduleViewModel()
     {
         Rooms = result,
         VenueName = "Sydney"
     };
     ViewBag.id = conferenceId;
     return View(vm);
 }
        public void LoadInSchedule(int conferenceId)
        {
            var allConferenceRooms = ConferenceLayer.GetConfRoomsForConference(IbaDb, conferenceId);
            for (var level = 2; level < 6; level++)
            {
                var path = Server.MapPath("~/App_Data/level" + level + ".json");
                using (var r = new StreamReader(path))
                {
                    var json = r.ReadToEnd();
                    var items = JsonConvert.DeserializeObject<List<ScheduleEventJsonViewModel>>(json);
                    foreach (var item in items)
                    {
                        var scheduleLocation = Db.EventLocations.SingleOrDefault(s => s.LocationName == item.linkedLocation);
                        if (scheduleLocation == null)
                        {
                            //NOTE: If no location exists check it's is valid
                            if(allConferenceRooms.Select(t=>t.RoomName).Contains(item.linkedLocation))
                            {
                                var roomEvent = new ScheduleEventLocation
                                {
                                    Floor = level,
                                    CentreX = item.centreX,
                                    CentreY = item.centreY,
                                    LocationName = item.linkedLocation,
                                    ConferenceId = conferenceId,

                                };
                                Db.EventLocations.Add(roomEvent);
                            }
                        } else
                        {
                            scheduleLocation.Floor = level;
                            scheduleLocation.ConferenceId = conferenceId;
                            scheduleLocation.CentreX = item.centreX;
                            scheduleLocation.CentreY = item.centreY;
                        }
                        
                        Db.SaveChanges();
                    }
                }
            }
        }
        public JsonResult SaveConferenceEvents([ModelBinder(typeof(JsonNetModelBinder))]ScheduleEventMainJsonViewModel model)
        {
            var allConferenceRooms = ConferenceLayer.GetConfRoomsForConference(IbaDb, model.id);


            foreach (var building in model.locations)
            {
                var floorNumber = 0;
                foreach(var floor in building.floors)
                {
                    foreach(var ev in floor)
                    {

                        //NOTE: Create building if needed
                        var scheduleLocation = Db.EventLocations.SingleOrDefault(s => s.LocationName == ev.linkedLocation);
                        if (scheduleLocation == null || ev.isOffsite)
                        {
                            //NOTE: First check if this location is offsite
                            if(ev.isOffsite)
                            {
                                var eventBuilding = Db.EventBuildings.SingleOrDefault(r => r.Name == "Offsite" && r.ConferenceId == model.id);
                                if( eventBuilding == null)
                                {
                                    eventBuilding = new ScheduleEventBuilding()
                                    {
                                        Name = "Offsite",
                                        ConferenceId = model.id,
                                    };
                                }

                                if (scheduleLocation != null)
                                {
                                    scheduleLocation.ConferenceId = model.id;
                                    scheduleLocation.Long = ev.lon;
                                    scheduleLocation.Lat = ev.lat;
                                    scheduleLocation.TranslatedTitle = ev.TranslatedTitle;
                                    scheduleLocation.Building = eventBuilding;
                                } else
                                {
                                    var roomEvent = new ScheduleEventLocation
                                    {
                                        IsOffsite = true,
                                        Long = ev.lon,
                                        Lat = ev.lat,
                                        TranslatedTitle = ev.TranslatedTitle,
                                        LocationName = ev.linkedLocation,
                                        ConferenceId = model.id,
                                        Building = eventBuilding

                                    };
                                    Db.EventLocations.Add(roomEvent);
                                }

                                
                                Db.SaveChanges();

                            } else
                            {
                                //NOTE: If no location exists check it's is valid
                                if (allConferenceRooms.Select(t => t.RoomName).Contains(ev.linkedLocation))
                                {
                                    var eventBuilding = Db.EventBuildings.SingleOrDefault(r => r.Name == building.buildingName && r.ConferenceId == model.id);
                                    if (eventBuilding == null)
                                    {
                                        eventBuilding = new ScheduleEventBuilding()
                                        {
                                            Name = building.buildingName,
                                            ConferenceId = model.id,
                                        };
                                    }
                                    var roomEvent = new ScheduleEventLocation
                                    {
                                        Floor = floorNumber,
                                        CentreX = ev.centreX,
                                        CentreY = ev.centreY,
                                        LocationName = ev.linkedLocation,
                                        ConferenceId = model.id,
                                        Building = eventBuilding

                                    };
                                    Db.EventLocations.Add(roomEvent);
                                    Db.SaveChanges();
                                }
                            }
                        }
                        else
                        {
                            var eventBuilding = Db.EventBuildings.SingleOrDefault(r => r.Name == building.buildingName && r.ConferenceId == model.id);
                            if (eventBuilding == null)
                            {
                                eventBuilding = new ScheduleEventBuilding()
                                {
                                    Name = building.buildingName,
                                    ConferenceId = model.id,
                                };
                            }
                            scheduleLocation.Floor = floorNumber;
                            scheduleLocation.ConferenceId = model.id;
                            scheduleLocation.CentreX = ev.centreX;
                            scheduleLocation.CentreY = ev.centreY;
                            scheduleLocation.Building = eventBuilding;
                            Db.SaveChanges();

                        }
                    }
                    floorNumber += 1;
                }
            }
            return new JsonResult();
        }