public bool CorrectDay(JsonEventModel ev, int i, int everyNum)
        {
            bool correctDay = false;

            if (ev.Repeat.M == true && ev.StartDateTime.AddDays(i + everyNum * 7).DayOfWeek.ToString() == "Monday")
            {
                correctDay = true;
            }
            else if (ev.Repeat.Tu == true && ev.StartDateTime.AddDays(i + everyNum * 7).DayOfWeek.ToString() == "Tuesday")
            {
                correctDay = true;
            }
            else if (ev.Repeat.W == true && ev.StartDateTime.AddDays(i + everyNum * 7).DayOfWeek.ToString() == "Wednesday")
            {
                correctDay = true;
            }
            else if (ev.Repeat.Th == true && ev.StartDateTime.AddDays(i + everyNum * 7).DayOfWeek.ToString() == "Thursday")
            {
                correctDay = true;
            }
            else if (ev.Repeat.F == true && ev.StartDateTime.AddDays(i + everyNum * 7).DayOfWeek.ToString() == "Friday")
            {
                correctDay = true;
            }
            else if (ev.Repeat.Sa == true && ev.StartDateTime.AddDays(i + everyNum * 7).DayOfWeek.ToString() == "Saturday")
            {
                correctDay = true;
            }
            else if (ev.Repeat.Su == true && ev.StartDateTime.AddDays(i + everyNum * 7).DayOfWeek.ToString() == "Sunday")
            {
                correctDay = true;
            }
            return(correctDay);
        }
        public async Task <IActionResult> CreateEvent([FromBody] JsonEventModel ev)
        {
            if (ev == null)
            {
                return(NotFound());
            }
            else
            {
                var Venue       = _context.Venue.SingleOrDefault(v => v.ApplicationUser.Id == userManager.GetUserId(HttpContext.User));
                var FamilyEvent = _context.FamilyEvent.SingleOrDefault(f => f.Name == ev.Name);
                int days        = 0;
                if (Venue == null)
                {
                    return(NotFound());
                }
                else if (FamilyEvent == null)
                {
                    FamilyEvent familyEvent = new FamilyEvent
                    {
                        Name = ev.Name
                    };
                    _context.Add(familyEvent);
                    await _context.SaveChangesAsync();

                    FamilyEvent = _context.FamilyEvent.SingleOrDefault(f => f.Name == ev.Name);
                }
                int everyNum = 0;
                switch (ev.Repeat.SelectRepeat)
                {
                case "Day":
                    if (ev.Repeat.AfterNumTimes != "")
                    {
                        for (int i = 0; i < int.Parse(ev.Repeat.AfterNumTimes); i++)
                        {
                            Event nEvent = new Event
                            {
                                Name          = ev.Name,
                                StartDateTime = ev.StartDateTime.AddDays(everyNum).ToLocalTime(),
                                EndTime       = ev.EndTime.AddDays(everyNum).ToLocalTime(),
                                EventTypeId   = int.Parse(ev.EventTypeId),
                                VenueId       = Venue.Id,
                                FamilyEventId = FamilyEvent.Id
                            };
                            everyNum += int.Parse(ev.Repeat.NumOfRepeat);
                            _context.Add(nEvent);
                        }
                    }
                    else
                    {
                        while (ev.Repeat.UntilDate > ev.StartDateTime.AddDays(everyNum))
                        {
                            Event nEvent = new Event
                            {
                                Name          = ev.Name,
                                StartDateTime = ev.StartDateTime.AddDays(everyNum).ToLocalTime(),
                                EndTime       = ev.EndTime.AddDays(everyNum).ToLocalTime(),
                                EventTypeId   = int.Parse(ev.EventTypeId),
                                VenueId       = Venue.Id,
                                FamilyEventId = FamilyEvent.Id
                            };
                            everyNum += int.Parse(ev.Repeat.NumOfRepeat);
                            _context.Add(nEvent);
                        }
                    }
                    break;

                case "Week":

                    if (ev.Repeat.AfterNumTimes != "")
                    {
                        for (int i = 0; i < int.Parse(ev.Repeat.AfterNumTimes) * 7; i++)
                        {
                            if (CorrectDay(ev, i, everyNum) == true)
                            {
                                Event nEvent = new Event
                                {
                                    Name          = ev.Name,
                                    StartDateTime = ev.StartDateTime.AddDays(i + (everyNum * 7)).ToLocalTime(),
                                    EndTime       = ev.EndTime.AddDays(i + (everyNum * 7)).ToLocalTime(),
                                    EventTypeId   = int.Parse(ev.EventTypeId),
                                    VenueId       = Venue.Id,
                                    FamilyEventId = FamilyEvent.Id
                                };

                                _context.Add(nEvent);
                            }
                            if (i + 1 >= 7)
                            {
                                if ((i + 1) % 7 == 0)
                                {
                                    everyNum += (int.Parse(ev.Repeat.NumOfRepeat) - 1);
                                }
                            }
                        }
                    }
                    else
                    {
                        int i = 0;
                        while (ev.Repeat.UntilDate > ev.StartDateTime.AddDays(i))
                        {
                            if (CorrectDay(ev, i, everyNum))
                            {
                                Event nEvent = new Event
                                {
                                    Name          = ev.Name,
                                    StartDateTime = ev.StartDateTime.AddDays(i + (everyNum * 7)).ToLocalTime(),
                                    EndTime       = ev.EndTime.AddDays(i + (everyNum * 7)).ToLocalTime(),
                                    EventTypeId   = int.Parse(ev.EventTypeId),
                                    VenueId       = Venue.Id,
                                    FamilyEventId = FamilyEvent.Id
                                };

                                _context.Add(nEvent);
                            }
                            if (i + 1 >= 7)
                            {
                                if ((i + 1) % 7 == 0)
                                {
                                    everyNum += (int.Parse(ev.Repeat.NumOfRepeat) - 1);
                                }
                            }
                            i++;
                        }
                    }
                    break;

                case "Month":
                    if (ev.Repeat.AfterNumTimes != "")
                    {
                        for (int i = 0; i < int.Parse(ev.Repeat.AfterNumTimes); i++)
                        {
                            Event nEvent = new Event
                            {
                                Name          = ev.Name,
                                StartDateTime = ev.StartDateTime.AddMonths(everyNum).ToLocalTime(),
                                EndTime       = ev.EndTime.AddMonths(everyNum).ToLocalTime(),
                                EventTypeId   = int.Parse(ev.EventTypeId),
                                VenueId       = Venue.Id,
                                FamilyEventId = FamilyEvent.Id
                            };
                            everyNum += int.Parse(ev.Repeat.NumOfRepeat);
                            _context.Add(nEvent);
                        }
                    }
                    else
                    {
                        while (ev.Repeat.UntilDate > ev.StartDateTime.AddMonths(everyNum))
                        {
                            Event nEvent = new Event
                            {
                                Name          = ev.Name,
                                StartDateTime = ev.StartDateTime.AddMonths(everyNum).ToLocalTime(),
                                EndTime       = ev.EndTime.AddMonths(everyNum).ToLocalTime(),
                                EventTypeId   = int.Parse(ev.EventTypeId),
                                VenueId       = Venue.Id,
                                FamilyEventId = FamilyEvent.Id
                            };
                            everyNum += int.Parse(ev.Repeat.NumOfRepeat);
                            _context.Add(nEvent);
                        }
                    }
                    break;
                }
            }
            await _context.SaveChangesAsync();

            Response.StatusCode = (int)HttpStatusCode.OK;
            return(Json(Response.StatusCode));
        }