/*{
            set { NumDays = DateTime.ParseExact(CurrentMonth, "MMMM", System.Globalization.CultureInfo.InvariantCulture).Month; }
        }*/
        public CalendarViewModel()
        {
            cd = new CalendarData();
            Events = new List<EventModel>();
            NewEvent = new EventModel();

            // We shouldn't need this call anymore
            //Events = cd.TestGetEvents(Events);
        }
        public ActionResult ModifyEvents(EventModel model, string StartTime, string EndTime, DateTime NewDate)
        {
            ModifyEventViewModel model2 = new ModifyEventViewModel();
            int startMin;
            int startHour;
            int endMin;
            int endHour;

            string[] split = StartTime.Split(new char[] { ':', ' ' });
            startMin = Int32.Parse(split[1]);

            if (split[2].Equals("PM") & !split[2].Equals("12"))
            {
                startHour = Int32.Parse(split[0]) + 12;
            }
            else if (split[2].Equals("AM") && split[2].Equals("12"))
            {
                startHour = 24;
            }
            else
            {
                startHour = Int32.Parse(split[0]);
            }

            string[] split2 = EndTime.Split(new char[] { ':', ' ' });
            endMin = Int32.Parse(split2[1]);

            if (split2[2].Equals("PM") & !split2[2].Equals("12"))
            {
                endHour = Int32.Parse(split2[0]) + 12;
            }
            else if (split2[2].Equals("AM") && split2[2].Equals("12"))
            {
                endHour = 24;
            }
            else
            {
                endHour = Int32.Parse(split2[0]);
            }
            model.StartMin = startMin;
            model.StartHour = startHour;
            model.EndMin = endMin;
            model.EndHour = endHour;
            model.Day = NewDate.Day;
            model.Month = NewDate.Month;
            model.Year = NewDate.Year;

            //uvModel.GetEventsCreatedBy((int)Session["UserId"]);
            model.ChangeEventDetails(model);
            model2.GetEventsCreatedBy((int)Session["UserId"]);

            return View("ModifyEvents", model2);
        }
 public void ChangeEventDetails(EventModel model)
 {
     UserData ud = new UserData();
     ud.ChangeEventDetails(model);
 }
        public List<EventModel> GetEventsByUserId(int userId)
        {
            string query, query2;
            SQLiteCommand cmd;
            EventModel eventModel;
            List<EventModel> Events = new List<EventModel>();

            try
            {
                dbConn.Open();
                // Get the event info and the donation amount if it isn't null
                // This will set the donation amount to 0 if there is no donation amount associated with the user/event
                query = "select E.* from events E, user_event UE " +
                        "where UE.user_id = '" + userId + "' and UE.event_id = E.event_id";
                DataTable dt = new DataTable();
                using (cmd = new SQLiteCommand(query, dbConn))
                {
                    using (SQLiteDataReader dr = cmd.ExecuteReader())
                    {
                        // Load the reader data into the DataTable
                        dt.Load(dr);

                        // While there are rows in the returned data create EventModels and add them to the EventModel list
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            eventModel = new EventModel(dt.Rows[i]);

                            Events.Add(eventModel);
                        }
                    }
                }

                dbConn.Close();
            }
            catch (SQLiteException ex)
            {
                Console.Write(ex.ToString());
                dbConn.Close();
            }

            return Events;
        }
        public string ChangeEventDetails(EventModel eventModel)
        {
            string query, errorMessage = "Update Successful";
            SQLiteCommand cmd;

            query = "update events set month = " + eventModel.Month + ", day = " + eventModel.Day + ", year = " + eventModel.Year + ", start_hour = " + eventModel.StartHour +
                    ", start_min = " + eventModel.StartMin + ", end_hour = " + eventModel.EndHour + ", end_min = " + eventModel.EndMin +
                    " where created_by_id = " + eventModel.CreatedById + " and event_id = " + eventModel.EventId;
            try
            {
                dbConn.Open();
                cmd = new SQLiteCommand(query, dbConn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                errorMessage = e.ToString();
                dbConn.Close();
            }

            return errorMessage;
        }
        public string AddUserToEvent(int userId, int eventId)
        {
            string query, errorMessage = "";
            SQLiteCommand cmd, cmd2;
            EventModel eventModel;
            List<EventModel> Events = new List<EventModel>();

            try
            {
                dbConn.Open();

                //query = "with recursive event(month, day, year, start_hour, start_min, end_hour, end_min) as (select month, day, year, start_hour, start_min, end_hour, end_min from events where event_id = " + eventId + ") "
                //        + "select count(*) from events where event_id != " + eventId + " and month = event.month and day = event.day and year = event.year "
                //        + "and ((start_hour < event.start_hour and end_hour > event.end_hour) or "
                //        + "(start_hour < event.start_hour and end_hour = event.end_hour and end_min > event.end_min) or "
                //        + "(start_hour = event.start_hour and end_hour > event.end_hour and start_min < event.start_min) or"
                //        + "(start_hour = event.start_hour and end_hour = event.end_hour and (start_min between event.start_min and event.end_min or end_min between event.start_min and event.end_min)) or "
                //        + "(start_hour = event.start_hour and end_hour = event.end_hour and start_min < event.start_min and end_min > event.end_min))";
                query = "select E.* from events E, user_event UE where UE.user_id = " + userId + " and UE.event_id = E.event_id";
                   // insert into user_event values (" + userId + ", " + eventId;
                DataTable dt = new DataTable();
                using (cmd = new SQLiteCommand(query, dbConn))
                {
                    //doesn't execute
                    using (SQLiteDataReader dr = cmd.ExecuteReader())
                    {
                        // Load the reader data into the DataTable
                        dt.Load(dr);

                        // While there are rows in the returned data create EventModels and add them to the EventModel list
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            eventModel = new EventModel(dt.Rows[i]);
                            Events.Add(eventModel);
                        }

                        // Check if there is a conflict with the events this user is signed up for
                        query = "select * from events where event_id = " + eventId + " and status not in ('Cancelled', 'Removed')";
                        DataTable dt2 = new DataTable();
                        using (cmd2 = new SQLiteCommand(query, dbConn))
                        {
                            using (SQLiteDataReader dr2 = cmd2.ExecuteReader())
                            {
                                dt2.Load(dr2);
                                eventModel = new EventModel(dt2.Rows[0]);

                                Events = Events.Where(x => x.Month == eventModel.Month && x.Day == eventModel.Day && x.Year == eventModel.Year &&
                                                           ((x.StartHour < eventModel.StartHour && x.EndHour > eventModel.EndHour) ||
                                                           (x.StartHour < eventModel.StartHour && x.EndHour == eventModel.EndHour && x.EndMin > eventModel.EndMin) ||
                                                           (x.StartHour == eventModel.StartHour && x.EndHour > eventModel.EndHour && x.StartMin < eventModel.StartMin) ||
                                                           (x.StartHour == eventModel.StartHour && x.EndHour == eventModel.EndHour && (x.StartMin >= eventModel.StartMin && x.StartMin <= eventModel.EndMin) || (x.EndMin >= eventModel.StartMin && x.EndMin <= eventModel.EndMin)) ||
                                                           (x.StartHour == eventModel.StartHour && x.EndHour == eventModel.EndHour && x.StartMin < eventModel.StartMin && x.EndMin > eventModel.EndMin))).ToList();
                                // There is no event overlap for the user
                                //if (Events.Count == 0)
                                //{
                                    bool current = false;
                                    foreach(var e in Events)
                                    {
                                        if(e.Status == EventStatus.Current)
                                        {
                                            current = true;
                                        }
                                    }
                                    if (!current)
                                    {
                                        query = "insert into user_event values (" + userId + ", " + eventId + ", '" + EventStatus.Current.ToString() + "')";
                                        cmd2 = new SQLiteCommand(query, dbConn);
                                        cmd2.ExecuteNonQuery();
                                    }
                                //}
                                // There is event overlap for the user
                                else
                                {
                                    errorMessage = "You are already enrolled in an event during this time period";
                                }
                            }
                        }

                    }
                }

                dbConn.Close();
            }
            catch (SQLiteException ex)
            {
                //Console.Write(ex.ToString());
                errorMessage = ex.ToString();
                dbConn.Close();
            }

            return errorMessage;
        }