/// <summary>
        /// @Author: Jivanjot Brar
        /// POST: if the user chooses Accept friend request, this function takes the id of the user
        /// who chooses "accept" and who sent the request and adds the row to the friendslist table
        /// and also access the row entered by the user who sent the request and changes the status
        /// from "pending" to "accepted"
        /// </summary>
        /// <param name="friendID">Takes in the the id of the user who sent the request.</param>
        /// <param name="userID">Id of the user to whom the request was sent.</param>
        /// <returns></returns>
        public ActionResult AcceptRequest(int friendID, int userID)
        {
            try
            {
                HeartbeatMonitor loginTimer = new HeartbeatMonitor(SessionHandler.UID, HttpContext);
                var friend = (from fr in db.FriendsLists
                              where fr.id == userID && fr.friend_id == friendID
                              select fr).FirstOrDefault();

                if (friend != null)
                {
                    friend.status = "accepted";
                }
                else
                {
                    FriendsList fl = new FriendsList();
                    fl.id = userID;
                    fl.friend_id = friendID;
                    fl.num_requests_sent = 0;
                    fl.status = "accepted";
                    db.FriendsLists.Add(fl);
                }

                friend = (from fr in db.FriendsLists
                          where fr.id == friendID && fr.friend_id == userID
                          select fr).FirstOrDefault();

                if (friend != null)
                {
                    friend.status = "accepted";
                }

                var user = (from usr in db.Users
                            where usr.id == userID
                            select usr).FirstOrDefault();

                string userImage = user.image_url;

                UserMessage um = new UserMessage();
                um.datetime_sent = DateTime.Now;
                um.sender_id = userID;
                um.recipient_id = friendID;
                um.subject = "Friend Request Accepted";
                um.message = "<table><tr>" +
                    "<td border=\"0px\"> <img src=\"" + "../" + userImage + "\" height=\"50px\" width=\"45px\" /> </td>" +
                    "<td>" + user.firstname + " " + user.lastname + " has accepted your friend request. </td>" +
                    "</tr></table>";
                um.message_read = false;

                // add the message to the user messages table
                db.UserMessages.Add(um);

                // save the changes made to the database
                db.SaveChanges();

                // Redirect to specified view
                return RedirectToAction("Index", "FriendList");
            }
            catch (Exception ex)
            {
                return View("~/Views/Shared/Error.cshtml", ex);
            }
        }
        /// <summary>
        ///  @Author: Patrick Tseng
        /// Generates messages to admins if a post is reported
        /// </summary>
        /// <param name="id">id of post to report</param>
        /// <returns>Redirects to thread</returns>
        public ActionResult Report(int id)
        {
            //checks if the user has been authenticated
            if (!SessionHandler.Logon)
                return RedirectToAction("Index", "Login");

            //gets the post in question
            ForumPost fp = _db.ForumPosts.SingleOrDefault(i => i.id == id);
            int threadID = 0;

            if(fp != null)
            {
                threadID = fp.thread_id;
                string username = _db.Users.SingleOrDefault(i => i.id == SessionHandler.UID).username;
                var users =     from u in _db.Users
                                where u.role == "admin"
                                select u;

                //generates a message for each of the admins and sends them
                foreach (User admin in users)
                {
                    UserMessage um = new UserMessage();
                    um.datetime_sent = DateTime.Now;
                    um.id = 1;
                    um.sender_id = 69;
                    um.recipient_id = admin.id;
                    um.subject = "Forum post reported";
                    um.message = username + " reported post " + "<a href=\"/Posts?threadID=" + threadID + "#" + id + "\">#" + id + "</a>.";
                    um.message_read = false;
                    _db.UserMessages.Add(um);
                }

                _db.SaveChanges();
            }

            return RedirectToAction("Index", new { threadID = threadID});
        }
        public ActionResult Find(FormCollection fm)
        {
            bool sendRequest = true;
            if (SessionHandler.Logon)
            {

                try
                {
                    HeartbeatMonitor loginTimer = new HeartbeatMonitor(SessionHandler.UID, HttpContext);
                    //display a list of users not including the active user
                    int temp = Convert.ToInt32(fm.GetKey(0));

                    FriendsList friend = (from f in db.FriendsLists
                                          where f.id == SessionHandler.UID && f.friend_id == temp
                                          select f).FirstOrDefault();

                    if (friend != null)
                    {
                        if (friend.num_requests_sent < 10)
                        {
                            int i = friend.num_requests_sent;
                            friend.num_requests_sent = ++i;
                            friend.status = "pending";
                        }
                        else
                        {
                            sendRequest = false;
                        }
                    }
                    else
                    {
                        // Add a request to the friends list table
                        FriendsList fl = new FriendsList();
                        fl.id = SessionHandler.UID;
                        fl.friend_id = temp;
                        fl.num_requests_sent = 1;
                        fl.status = "pending";

                        db.FriendsLists.Add(fl);
                    }

                    if (sendRequest)
                    {
                        // get current users information
                        User c_user = (from userT in db.Users
                                       where userT.id == SessionHandler.UID
                                       select userT).FirstOrDefault();

                        // current users image
                        string userImage = c_user.image_url;

                        //send friend request message to the select user in find friends form.
                        UserMessage um = new UserMessage();

                        // current date and time, when message was sent
                        um.datetime_sent = DateTime.Now;

                        //this is the ID of the current logged in user
                        um.sender_id = SessionHandler.UID;

                        //This is the ID of the user, who is being sent the friend request.
                        um.recipient_id = temp;
                        um.subject = "Friend Request";
                        um.message = "<table><tr>" +
                            "<tdborder=\"0px\"> <img src=\"" + "../" + userImage + "\" height=\"50px\" width=\"45px\"/> </td>" +
                            "<td>" + c_user.firstname + " " + c_user.lastname + " has sent you a friend request. </td>" +
                            "<td><a href=\"../../../FriendList/AcceptRequest/?friendID=" + SessionHandler.UID + "&userID=" + temp + "\">Accept</a></td>" +
                            "<td><a href=\"../../../FriendList/DeclineRequest/?friendID=" + SessionHandler.UID + "&userID=" + temp + "\">Decline</a></td>" +
                            "</tr></table>";
                        um.message_read = false;

                        // add the message to the user messages table
                        db.UserMessages.Add(um);

                        // save the changes made to the database.
                        db.SaveChanges();
                    }

                    // Redirect to specified view
                    return RedirectToAction("Find", "FriendList");

                }
                catch (Exception ex)
                {
                    return View("~/Views/Shared/Error.cshtml", ex);
                }
            }
            else
            {
                //must go to the login action
                return RedirectToAction("Index", "Login");
            }
        }