public static List<ForumTopicJson> GetForumTopicsJsonUnread(Guid forumId, long groupId, Guid memId, int take = 500)
        {

            List<ForumTopicJson> topics = new List<ForumTopicJson>();
            try
            {

                var dc = new ManagementContext();
                var groups = MemberCache.GetGroupsApartOf(memId).ToList();

                var groupIds = groups.Select(x => x.Id).ToList();
                groupIds.Add(0);

                if (groupIds.Where(x => x == groupId).FirstOrDefault() != null)
                {
                    //otherwise take just the count.
                    var db = (from xx in dc.ForumInbox
                              where xx.ToUser.MemberId == memId
                              where xx.Topic.Forum.ForumId == forumId
                              where xx.Topic.IsRemoved == false
                              select new
                              {
                                  Created = xx.Topic.Created,
                                  topic = xx.Topic,
                                  LastModified = xx.LastModified
                              }).OrderByDescending(x => x.LastModified).Take(take).AsParallel().ToList();


                    bool isManager = RDN.Library.Cache.MemberCache.IsManagerOrBetterOfLeague(memId);
                    bool isModerator = MemberCache.IsModeratorOrBetterOfLeagueGroup(memId, groupId);
                    foreach (var message in db)
                    {
                        if (topics.Where(x => x.TopicId == message.topic.TopicId).FirstOrDefault() == null)
                        {
                            ForumTopicJson top = new ForumTopicJson();
                            if (message.topic.Category != null)
                            {
                                top.Category = message.topic.Category.NameOfCategory;
                                top.CategoryId = message.topic.Category.CategoryId;
                            }
                            top.ForumId = forumId;
                            top.LastModified = message.topic.LastModified.GetValueOrDefault();
                            top.Created = message.topic.Created;
                            top.CreatedHuman = RDN.Portable.Util.DateTimes.DateTimeExt.RelativeDateTime(message.topic.Created);
                            top.CreatedByMember = new MemberDisplayBasic();
                            top.CreatedByMember.DerbyName = message.topic.CreatedByMember.DerbyName;
                            top.CreatedByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.topic.CreatedByMember.DerbyName);
                            top.CreatedByMember.MemberId = message.topic.CreatedByMember.MemberId;
                            top.LastPostByMember = new MemberDisplayBasic();
                            top.LastPostByMember.DerbyName = message.topic.LastPostByMember.DerbyName;
                            top.LastPostByMember.MemberId = message.topic.LastPostByMember.MemberId;
                            top.LastPostByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.topic.LastPostByMember.DerbyName);
                            if (message.topic.LastPostDateTime != null)
                                top.LastPostHuman = RDN.Portable.Util.DateTimes.DateTimeExt.RelativeDateTime(message.topic.LastPostDateTime.GetValueOrDefault());
                            else
                                top.LastPostHuman = RDN.Portable.Util.DateTimes.DateTimeExt.RelativeDateTime(message.topic.LastModified.GetValueOrDefault());

                            top.TopicId = message.topic.TopicId;
                            top.GroupId = message.topic.GroupId;
                            top.TopicTitle = message.topic.TopicTitle;
                            top.TopicTitleForUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.topic.TopicTitle);
                            top.ViewCount = message.topic.ViewCount;
                            top.Replies = message.topic.Messages.Where(x => x.IsRemoved == false).Count() - 1;
                            top.IsLocked = message.topic.IsLocked;
                            top.IsPinned = message.topic.IsSticky;
                            //is league manager
                            top.IsManagerOfTopic = isManager;
                            //if nt league manager and group id > 0
                            if (message.topic.GroupId > 0 && !isManager)
                                top.IsManagerOfTopic = isModerator;

                            //case happens when the user selects to only see unread topics.
                            //which means the category will be -1.
                            topics.Add(top);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return topics;
        }
        public static List<ForumTopicJson> GetForumTopicsJson(string q, int limit, Guid forumId, long groupId)
        {
            List<ForumTopicJson> topics = new List<ForumTopicJson>();
            try
            {
                Guid memId = RDN.Library.Classes.Account.User.GetMemberId();
                var dc = new ManagementContext();

                var groups = MemberCache.GetGroupsApartOf(memId).ToList();

                var groupIds = groups.Select(x => x.Id).ToList();
                groupIds.Add(0);

                if (groupIds.Where(x => x == groupId).FirstOrDefault() != null)
                {
                    var db = (from xx in dc.ForumMessages
                              where xx.Topic.Forum.ForumId == forumId
                              where xx.Topic.GroupId == groupId
                              where xx.Topic.IsRemoved == false
                              where xx.Topic.TopicTitle.Contains(q) | xx.MessagePlain.Contains(q)
                              select new
                              {
                                  Created = xx.Created,
                                  topic = xx.Topic
                              }).AsParallel().ToList();

                    bool isManager = RDN.Library.Cache.MemberCache.IsManagerOrBetterOfLeague(memId);
                    bool isModerator = MemberCache.IsModeratorOrBetterOfLeagueGroup(memId, groupId);
                    foreach (var message in db)
                    {
                        if (topics.Where(x => x.TopicId == message.topic.TopicId).FirstOrDefault() == null)
                        {
                            ForumTopicJson top = new ForumTopicJson();
                            if (message.topic.Category != null)
                            {
                                top.Category = message.topic.Category.NameOfCategory;
                                top.CategoryId = message.topic.Category.CategoryId;
                            }
                            top.ForumId = forumId;
                            top.LastModified = message.topic.LastModified.GetValueOrDefault();
                            top.Created = message.topic.Created;
                            top.CreatedHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.topic.Created);
                            top.CreatedByMember = new MemberDisplayBasic();
                            top.CreatedByMember.DerbyName = message.topic.CreatedByMember.DerbyName;
                            top.CreatedByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.topic.CreatedByMember.DerbyName);
                            top.CreatedByMember.MemberId = message.topic.CreatedByMember.MemberId;
                            top.LastPostByMember = new MemberDisplayBasic();
                            top.LastPostByMember.DerbyName = message.topic.LastPostByMember.DerbyName;
                            top.LastPostByMember.MemberId = message.topic.LastPostByMember.MemberId;
                            top.LastPostByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.topic.LastPostByMember.DerbyName);
                            if (message.topic.LastPostDateTime != null)
                                top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.topic.LastPostDateTime.GetValueOrDefault());
                            else
                                top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.topic.LastModified.GetValueOrDefault());

                            top.TopicId = message.topic.TopicId;
                            top.GroupId = message.topic.GroupId;
                            top.TopicTitle = message.topic.TopicTitle;
                            top.TopicTitleForUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.topic.TopicTitle);
                            top.ViewCount = message.topic.ViewCount;
                            top.Replies = message.topic.Messages.Where(x => x.IsRemoved == false).Count() - 1;
                            top.IsLocked = message.topic.IsLocked;
                            top.IsPinned = message.topic.IsSticky;
                            //is league manager
                            top.IsManagerOfTopic = isManager;
                            //if nt league manager and group id > 0
                            if (message.topic.GroupId > 0 && !isManager)
                                top.IsManagerOfTopic = isModerator;

                            if (message.topic.TopicsInbox.Where(x => x.ToUser.MemberId == memId).FirstOrDefault() == null)
                                top.IsRead = true;

                            if (!top.IsPinned)
                                topics.Add(top);
                            else
                                topics.Insert(0, top);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return topics;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="forumId"></param>
        /// <param name="groupId"></param>
        /// <param name="categoryId">id of the category selected.
        /// Category ID could be -1 which in that case the user selected just UNREAD messages.
        /// </param>
        /// <returns></returns>
        public static List<ForumTopicJson> GetForumTopicsJson(int page, int count, Guid forumId, long groupId, long categoryId, bool isArchived, ForumOwnerTypeEnum forumType)
        {

            List<ForumTopicJson> topics = new List<ForumTopicJson>();
            try
            {
                Guid memId = RDN.Library.Classes.Account.User.GetMemberId();
                var dc = new ManagementContext();
                int pNum = page * count;

                //because if the page is greater than 1, then the league already went through the default 100 topics.
                if (page > 0)
                    pNum = ((page - 1) * count) + Forum.DEFAULT_PAGE_SIZE;

                var groups = MemberCache.GetGroupsApartOf(memId).ToList();

                var groupIds = groups.Select(x => x.Id).ToList();
                groupIds.Add(0);
                List<DataModels.Forum.ForumTopic> dbSticky = new List<DataModels.Forum.ForumTopic>();
                List<DataModels.Forum.ForumTopic> db = new List<DataModels.Forum.ForumTopic>();

                if (groupIds.Where(x => x == groupId).FirstOrDefault() != null)
                {

                    if (categoryId == 0 || categoryId == -1)
                    {
                        //don't call stickies if we are on second page.
                        if (page == 0)
                        {
                            dbSticky = (from xx in dc.ForumTopics
                                        where xx.Forum.ForumId == forumId
                                        where xx.GroupId == groupId
                                        where xx.IsRemoved == false
                                        where xx.IsSticky == true
                                        where xx.IsArchived == isArchived
                                        select xx).OrderByDescending(x => x.LastPostDateTime).AsParallel().ToList();
                        }
                        db = (from xx in dc.ForumTopics
                              where xx.Forum.ForumId == forumId
                              where xx.GroupId == groupId
                              where xx.IsRemoved == false
                              where xx.IsArchived == isArchived
                              select xx).OrderByDescending(x => x.LastPostDateTime).Skip(pNum).Take(count).AsParallel().ToList();
                    }
                    else
                    {
                        //don't call stickies if we are on second page.
                        if (page == 0)
                        {
                            dbSticky = (from xx in dc.ForumTopics
                                        where xx.Forum.ForumId == forumId
                                        where xx.Category.CategoryId == categoryId
                                        where xx.GroupId == groupId
                                        where xx.IsRemoved == false
                                        where xx.IsArchived == isArchived
                                        where xx.IsSticky == true
                                        select xx).OrderByDescending(x => x.LastPostDateTime).AsParallel().ToList();
                        }
                        db = (from xx in dc.ForumTopics
                              where xx.Forum.ForumId == forumId
                              where xx.Category.CategoryId == categoryId
                              where xx.GroupId == groupId
                              where xx.IsRemoved == false
                              where xx.IsArchived == isArchived
                              select xx).OrderByDescending(x => x.LastPostDateTime).Skip(pNum).Take(count).AsParallel().ToList();
                    }
                    bool isManager = false;
                    bool isModerator = false;

                    if (forumType == ForumOwnerTypeEnum.league)
                    {
                        isManager = RDN.Library.Cache.MemberCache.IsManagerOrBetterOfLeague(memId);
                        isModerator = MemberCache.IsModeratorOrBetterOfLeagueGroup(memId, groupId);
                    }
                    else if (forumType == ForumOwnerTypeEnum.main)
                    {
                        isManager = MemberCache.IsAdministrator(memId);
                        isModerator = MemberCache.IsAdministrator(memId);
                    }
                    if (categoryId != -1)
                    {
                        foreach (var message in dbSticky)
                        {
                            if (topics.Where(x => x.TopicId == message.TopicId).FirstOrDefault() == null)
                            {
                                ForumTopicJson top = new ForumTopicJson();
                                if (message.Category != null)
                                {
                                    top.Category = message.Category.NameOfCategory;
                                    top.CategoryId = message.Category.CategoryId;
                                }
                                top.ForumOwnerTypeEnum = forumType.ToString();
                                top.ForumId = forumId;
                                top.IsArchived = isArchived;
                                top.LastModified = message.LastModified.GetValueOrDefault();
                                top.Created = message.Created;
                                top.CreatedHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.Created);
                                top.CreatedByMember = new MemberDisplayBasic();
                                top.CreatedByMember.DerbyName = message.CreatedByMember.DerbyName;
                                top.CreatedByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.CreatedByMember.DerbyName);
                                top.CreatedByMember.MemberId = message.CreatedByMember.MemberId;
                                top.LastPostByMember = new MemberDisplayBasic();
                                top.LastPostByMember.DerbyName = message.LastPostByMember.DerbyName;
                                top.LastPostByMember.MemberId = message.LastPostByMember.MemberId;
                                top.LastPostByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.LastPostByMember.DerbyName);
                                if (message.LastPostDateTime != null)
                                    top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.LastPostDateTime.GetValueOrDefault());
                                else
                                    top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.LastModified.GetValueOrDefault());

                                top.TopicId = message.TopicId;
                                top.GroupId = message.GroupId;
                                top.TopicTitle = message.TopicTitle;
                                top.TopicTitleForUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.TopicTitle);
                                top.ViewCount = message.ViewCount;
                                top.Replies = message.Messages.Where(x => x.IsRemoved == false).Count() - 1;
                                top.IsLocked = message.IsLocked;
                                top.IsPinned = message.IsSticky;
                                //is league manager
                                top.IsManagerOfTopic = isManager;
                                //if nt league manager and group id > 0
                                if (message.GroupId > 0 && !isManager)
                                    top.IsManagerOfTopic = isModerator;

                                if (message.TopicsInbox.Where(x => x.ToUser.MemberId == memId).FirstOrDefault() == null)
                                    top.IsRead = true;

                                if (!top.IsPinned)
                                    topics.Add(top);
                                else
                                    topics.Insert(0, top);
                            }
                        }
                    }
                    foreach (var message in db)
                    {
                        if (topics.Where(x => x.TopicId == message.TopicId).FirstOrDefault() == null)
                        {
                            ForumTopicJson top = new ForumTopicJson();
                            if (message.Category != null)
                            {
                                top.Category = message.Category.NameOfCategory;
                                top.CategoryId = message.Category.CategoryId;
                            }
                            top.ForumOwnerTypeEnum = forumType.ToString();
                            top.ForumId = forumId;
                            top.IsArchived = isArchived;
                            top.LastModified = message.LastModified.GetValueOrDefault();
                            top.Created = message.Created;
                            top.CreatedHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.Created);
                            top.CreatedByMember = new MemberDisplayBasic();
                            top.CreatedByMember.DerbyName = message.CreatedByMember.DerbyName;
                            top.CreatedByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.CreatedByMember.DerbyName);
                            top.CreatedByMember.MemberId = message.CreatedByMember.MemberId;
                            top.LastPostByMember = new MemberDisplayBasic();
                            top.LastPostByMember.DerbyName = message.LastPostByMember.DerbyName;
                            top.LastPostByMember.MemberId = message.LastPostByMember.MemberId;
                            top.LastPostByMember.DerbyNameUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.LastPostByMember.DerbyName);
                            if (message.LastPostDateTime != null)
                                top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.LastPostDateTime.GetValueOrDefault());
                            else
                                top.LastPostHuman = RDN.Utilities.Dates.DateTimeExt.RelativeDateTime(message.LastModified.GetValueOrDefault());

                            top.TopicId = message.TopicId;
                            top.GroupId = message.GroupId;
                            top.TopicTitle = message.TopicTitle;
                            top.TopicTitleForUrl = RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(message.TopicTitle);
                            top.ViewCount = message.ViewCount;
                            top.Replies = message.Messages.Where(x => x.IsRemoved == false).Count() - 1;
                            top.IsLocked = message.IsLocked;
                            top.IsPinned = message.IsSticky;
                            //is league manager
                            top.IsManagerOfTopic = isManager;
                            //if nt league manager and group id > 0
                            if (message.GroupId > 0 && !isManager)
                                top.IsManagerOfTopic = isModerator;

                            if (message.TopicsInbox.Where(x => x.ToUser.MemberId == memId).FirstOrDefault() == null)
                                top.IsRead = true;
                            if (categoryId != -1)
                            {
                                //users selects something other than unread topics.
                                if (!top.IsPinned)
                                    topics.Add(top);
                                else
                                    topics.Insert(0, top);
                            }
                            else if (top.IsRead == false)
                            {
                                //case happens when the user selects to only see unread topics.
                                //which means the category will be -1.
                                topics.Add(top);
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return topics;
        }