// u ask to join to a group
        public ActionResult AskToJoinGroup(int id, int gID)
        {
            Group g = db.Groups.SingleOrDefault(r => r.GroupID == gID);
            GroupRequest gr = new GroupRequest();
            gr.ActiveID = id;
            gr.ReceiveID = g.OwnerID;
            gr.GroupID = g.GroupID;
            db.GroupRequests.Add(gr);
            db.SaveChanges();

            return RedirectToAction("FindFriend", new { id = id });
        }
        // u invite o to u's group
        public ActionResult InviteToGroup(int id, int oID)
        {
            ExaminationsPaper u = db.ExaminationsPapers.SingleOrDefault(r => r.ExamPaperID == id);

            if (u.GroupID == null) // u has no group
            {
                Group g = new Group(); // create new group
                g.OwnerID = id; // set u as owner
                g.Quantity = 1;
                db.Groups.Add(g);
                db.SaveChanges();
                u.GroupID = g.GroupID; // connect u to the group
                db.SaveChanges();
                u = db.ExaminationsPapers.SingleOrDefault(r => r.ExamPaperID == id); // retrieve new info
            }

            GroupRequest gr = new GroupRequest();
            gr.ActiveID = id;
            gr.ReceiveID = oID;
            gr.GroupID = u.GroupID.Value; // sure u already in a group
            db.GroupRequests.Add(gr);
            db.SaveChanges();

            return RedirectToAction("FindFriend", new { id = id });
        }
        public JsonResult GetDataFindFriend(int id)
        {
            ExaminationsPaper ep = db.ExaminationsPapers.SingleOrDefault(r => r.ExamPaperID == id);
            var eps = db.ExaminationsPapers.Where(r => r.LodgeRegisteredID == ep.LodgeRegisteredID && r.ExamPaperID != ep.ExamPaperID);

            List<DataFindFriend> results = new List<DataFindFriend>();
            DataFindFriend record = new DataFindFriend();
            GroupRequest gr = new GroupRequest();
            foreach (ExaminationsPaper item in eps)
            {
                record = new DataFindFriend();
                record.name = item.Candidate.Account.Profile.Lastname  + " " + @item.Candidate.Account.Profile.Firstname;
                record.gender = item.Candidate.Account.Profile.Gender ? "Nam" : "Nữ";
                record.group = item.GroupID == null ? "Chưa có nhóm" : "Nhóm " + item.GroupID;

                if (ep.GroupID == null) // A has no group
                {
                    if (item.GroupID == null) // both are free
                    {
                        if (item.Candidate.Account.Profile.Gender == ep.Candidate.Account.Profile.Gender) // same gender
                        {
                            record.actions = String.Format("<a class='btn-u btn-u-pink' href='/Candidate/{0}/{1}?{2}={3}'><i class='icon-thumbs-up'></i>&nbsp;{4}</a>", "InviteToGroup", ep.ExamPaperID, "oID", item.ExamPaperID, "Mời vào nhóm");
                            record.note = "";
                        }
                    }
                    else // B has group
                    {
                        gr = ep.Receives.SingleOrDefault(r => r.GroupID == item.GroupID); // B's group invited A?
                        if (gr != null) // invited
                        {
                            record.actions = String.Format("<a class='btn-u btn-u-blue' href='/Candidate/{0}/{1}?{2}={3}'><i class='icon-ok'></i>&nbsp;{4}</a>", "AcceptRequest", ep.ExamPaperID, "gID", gr.GroupID, "Đồng ý");
                            record.actions += String.Format("<a class='btn-u btn-u-red' href='/Candidate/{0}/{1}'><i class='icon-remove'></i>&nbsp;{2}</a>", "DeleteRequest", gr.ID, "Từ chối");
                            record.note = gr.Active.Candidate.Account.Profile.Firstname + " đã mời bạn vào nhóm " + gr.GroupID;
                        }
                        else // not yet invited
                        {
                            gr = ep.Actives.SingleOrDefault(r => r.GroupID == item.GroupID); // A asked to join in B's group?
                            if (gr != null) // asked
                            {
                                record.actions = String.Format("<a class='btn-u btn-u-red' href='/Candidate/{0}/{1}'><i class='icon-remove'></i>&nbsp;{2}</a>", "DeleteRequest", gr.ID, "Huỷ");
                                record.note = "Bạn đã gửi lời đề nghị được vào nhóm " + item.GroupID;
                            }
                            else // not invited, not asked --> not linked: can ask to join in b's group if same gender
                            {
                                if (item.Candidate.Account.Profile.Gender == ep.Candidate.Account.Profile.Gender) // same gender
                                {
                                    record.actions = String.Format("<a class='btn-u btn-u-purple' href='/Candidate/{0}/{1}?{2}={3}'><i class='icon-group'></i>&nbsp;{4}</a>", "AskToJoinGroup", ep.ExamPaperID, "gID", item.GroupID, "Gửi yêu cầu vào nhóm");
                                    record.note = "";
                                }
                            }
                        }
                    }
                }
                else // A has group
                {
                    if (item.GroupID == null) // B has no group
                    {
                        gr = item.Receives.SingleOrDefault(r => r.GroupID == ep.GroupID); // A's group invited B?
                        if (gr != null) // invited
                        {
                            if (ep.Group.OwnerID == ep.ExamPaperID) // is owner of the group
                                record.actions = String.Format("<a class='btn-u btn-u-red' href='/Candidate/{0}/{1}'><i class='icon-remove'></i>&nbsp;{2}</a>", "DeleteRequest", gr.ID, "Hủy lời mời");
                            record.note = (gr.ActiveID == ep.ExamPaperID ? "Bạn đã mời vào nhóm" : gr.Active.Candidate.Account.Profile.Firstname + " đã mời vào nhóm");
                        }
                        else // not yet invited
                        {
                            gr = item.Actives.SingleOrDefault(r => r.GroupID == ep.GroupID); // B asked to join to A's group?
                            if (gr != null) // asked
                            {
                                if (ep.Group.OwnerID == ep.ExamPaperID) // is owner of the group
                                {
                                    record.actions = String.Format("<a class='btn-u btn-u-blue' href='/Candidate/{0}/{1}?{2}={3}'><i class='icon-ok'></i>&nbsp;{4}</a>", "AcceptRequest", item.ExamPaperID, "gID", ep.GroupID, "Đồng ý");
                                    record.actions += String.Format("<a class='btn-u btn-u-red' href='/Candidate/{0}/{1}'><i class='icon-remove'></i>&nbsp;{2}</a>", "DeleteRequest", gr.ID, "Từ chối");
                                }
                                record.note = "Đang yêu cầu được vào nhóm";
                            }
                            else // not invited, not asked --> not linked: can invite if same gender
                            {
                                if (item.Candidate.Account.Profile.Gender == ep.Candidate.Account.Profile.Gender) // same gender
                                {
                                    record.actions = String.Format("<a class='btn-u btn-u-pink' href='/Candidate/{0}/{1}?{2}={3}'><i class='icon-thumbs-up'></i>&nbsp;{4}</a>", "InviteToGroup", ep.ExamPaperID, "oID", item.ExamPaperID, "Mời vào nhóm");
                                    record.note = "";
                                }
                            }
                        }
                    }
                    else // both are in groups
                    {
                        if (ep.GroupID == item.GroupID) // and in the same group
                        {
                            if (item.ExamPaperID == ep.Group.OwnerID) // B is the owner of A's group
                            {
                                record.actions = "";
                                record.note = "Nhóm trưởng của nhóm bạn";
                            }
                            else  // B is group member of A's group
                            {
                                record.actions = "";
                                record.note = "Thành viên của nhóm bạn";
                            }
                        }
                    }
                }
                results.Add(record);
            }

            return Json(new { success = true, data = results });
        }