/// <summary>
        /// updates the groups for the calendar event.
        /// </summary>
        /// <param name="groupIds"></param>
        /// <param name="dc"></param>
        /// <param name="calevent"></param>
        private static void UpdateGroupsOfEvent(List<long> groupIds, ManagementContext dc, DataModels.Calendar.CalendarEvent calevent)
        {
            //removes any groups not in list.
            var tempGroupsForEachEvent = calevent.Groups.ToList();
            foreach (var group in tempGroupsForEachEvent)
            {
                if (!groupIds.Contains(group.Group.Id))
                {
                    calevent.Groups.Remove(group);
                }
            }

            //adds any groups not in list.
            foreach (var id in groupIds)
            {
                var group = dc.LeagueGroups.Where(x => x.Id == id).FirstOrDefault();
                if (group != null)
                {
                    if (calevent.Groups.Where(x => x.Group.Id == id).FirstOrDefault() == null)
                    {
                        RDN.Library.DataModels.Calendar.CalendarEventGroup newGroup = new RDN.Library.DataModels.Calendar.CalendarEventGroup();
                        newGroup.Group = group;
                        newGroup.Event = calevent;
                        calevent.Groups.Add(newGroup);
                    }
                }
            }
        }
        public static Guid CreateNewEvent(Guid calId, DateTime startDate, DateTime endDate, Guid locationId, string eventName, string link, string notes, bool AllowSelfCheckIn, bool isPublicEvent, bool isReocurring, Guid reocurringEventId, long selectedEventTypeId, bool broadcastEvent, string ticketUrl, string hexColor, List<long> groupIds, Guid memId)
        {
            DataModels.Calendar.CalendarEvent ev = new DataModels.Calendar.CalendarEvent();
            try
            {
                var dc = new ManagementContext();
                ev.Calendar = dc.Calendar.Where(x => x.CalendarId == calId).FirstOrDefault();
                if (ev.Calendar.IsCalendarInUTC)
                {
                    DateTimeOffset dtOffEnd = new DateTimeOffset(endDate.Ticks, new TimeSpan(ev.Calendar.TimeZone, 0, 0));
                    DateTimeOffset dtOffStart = new DateTimeOffset(startDate.Ticks, new TimeSpan(ev.Calendar.TimeZone, 0, 0));

                    ev.EndDate = dtOffEnd.UtcDateTime;
                    ev.StartDate = dtOffStart.UtcDateTime;
                    ev.IsInUTCTime = true;
                }
                else
                {
                    ev.EndDate = endDate;
                    ev.StartDate = startDate;
                }
                ev.AllowSelfCheckIn = AllowSelfCheckIn;
                ev.Location = dc.Locations.Include("Contact").Include("Contact.Addresses").Include("Contact.Communications").Where(x => x.LocationId == locationId).FirstOrDefault();
                ev.Name = eventName;
                ev.Notes = notes;
                ev.Link = link;
                ev.IsPublicEvent = isPublicEvent;
                ev.TicketUrl = ticketUrl;
                if (ev.EndDate < DateTime.UtcNow.AddYears(-1))
                    ev.EndDate = ev.StartDate;


                if (!String.IsNullOrEmpty(hexColor))
                {
                    Color cc = ColorTranslator.FromHtml(hexColor);
                    int arb = cc.ToArgb();
                    ev.Color = dc.Colors.Where(x => x.ColorIdCSharp == arb).FirstOrDefault();
                }
                else
                    ev.Color = null;
                ev.EventType = dc.CalendarEventTypes.Where(x => x.CalendarEventTypeId == selectedEventTypeId).FirstOrDefault();

                //we create a event.  Gotta check if its already in the db.
                //we had an instance where a league deleted an old reoccuring event
                //and they used the same name.  So we had to check if the event had the same reoccuring event
                //id.  
                DataModels.Calendar.CalendarEvent checkEventExists = null;
                if (isReocurring)
                {
                    ev.ReocurringEvent = dc.CalendarEventsReocurring.Where(x => x.CalendarItemId == reocurringEventId).FirstOrDefault();
                    //taking the groups from the reoccurence and adding them to the list of groups.
                    foreach (var g in ev.ReocurringEvent.Groups)
                        groupIds.Add(g.Group.Id);

                    checkEventExists = ev.Calendar.CalendarEvents.Where(x => x.EndDate == endDate && x.StartDate == startDate && x.Name == eventName && x.ReocurringEvent != null && x.ReocurringEvent.CalendarItemId == reocurringEventId).FirstOrDefault();
                }

                //keep under isrecocuring question.
                foreach (var id in groupIds)
                {
                    var group = dc.LeagueGroups.Where(x => x.Id == id).FirstOrDefault();
                    if (group != null)
                    {
                        if (ev.Groups.Where(x => x.Group.Id == id).FirstOrDefault() == null)
                        {
                            RDN.Library.DataModels.Calendar.CalendarEventGroup newGroup = new RDN.Library.DataModels.Calendar.CalendarEventGroup();
                            newGroup.Group = group;
                            newGroup.Event = ev;
                            ev.Groups.Add(newGroup);
                        }
                    }
                }


                if (checkEventExists == null)
                    dc.CalendarEvents.Add(ev);
                else
                    ev.CalendarItemId = checkEventExists.CalendarItemId;
                int c = dc.SaveChanges();

                if (broadcastEvent)
                {
                    List<Guid> memIds = new List<Guid>();
                    var memberCreated = MemberCache.GetMemberDisplay(memId);
                    if (ev.Groups.Count == 0)
                    {
                        //sends broadcast to all league members
                        var members = MemberCache.GetCurrentLeagueMembers(memId);
                        foreach (var mem in members)
                        {
                            SendEmailAboutNewEvent(calId, ev, null, memberCreated.DerbyName, mem.UserId, mem.DerbyName, startDate, endDate);
                            memIds.Add(mem.MemberId);
                        }
                    }
                    else
                    {
                        //gets all the members of the groups selected and sends an email broadcast to those members.
                        List<MemberDisplay> memsToSend = new List<MemberDisplay>();
                        var groups = MemberCache.GetLeagueGroupsOfMember(memId);
                        foreach (var group in ev.Groups)
                        {
                            var g = groups.Where(x => x.Id == group.Group.Id).FirstOrDefault();
                            if (g != null)
                            {
                                foreach (var temp in g.GroupMembers)
                                {
                                    MemberDisplay mtemp = new MemberDisplay();
                                    mtemp.DerbyName = temp.DerbyName;
                                    mtemp.UserId = temp.UserId;
                                    if (memsToSend.Where(x => x.UserId == mtemp.UserId).FirstOrDefault() == null)
                                        memsToSend.Add(mtemp);
                                }
                            }
                        }
                        var members = MemberCache.GetCurrentLeagueMembers(memId);
                        foreach (var mem in memsToSend)
                        {
                            SendEmailAboutNewEvent(calId, ev, null, memberCreated.DerbyName, mem.UserId, mem.DerbyName, startDate, endDate);
                            memIds.Add(mem.MemberId);
                        }

                    }
                    var fact = new MobileNotificationFactory()
                         .Initialize("New Event Created:", ev.Name, Mobile.Enums.NotificationTypeEnum.NewCalendarEventBroadcast)
                         .AddCalendarEvent(ev.CalendarItemId, calId, ev.Name)
                         .AddMembers(memIds)
                         .SendNotifications();
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: endDate + ":" + startDate);
            }
            return ev.CalendarItemId;
        }