public bool AddFacebookFriendsGender(string ProfileId, string FacebookUserId)
        {
            Api.Socioboard.Services.FacebookAccount _FacebookAccount = new FacebookAccount();
            Domain.Socioboard.Domain.FacebookAccount _facebookAccount = new Domain.Socioboard.Domain.FacebookAccount();

            _facebookAccount = (Domain.Socioboard.Domain.FacebookAccount)(new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(_FacebookAccount.getFacebookAccountDetailsById(ProfileId, FacebookUserId), typeof(Domain.Socioboard.Domain.FacebookAccount)));

            if (string.IsNullOrEmpty(_facebookAccount.AccessToken))
            {
                _facebookAccount = new Domain.Socioboard.Domain.FacebookAccount();
                FacebookAccountRepository _FacebookAccountRepository = new FacebookAccountRepository();

                System.Collections.ArrayList lstFacebookAccounts = _FacebookAccountRepository.getAllFacebookAccounts();

                Random _random = new Random();
                var rnum = _random.Next(0, lstFacebookAccounts.Count - 1);
                _facebookAccount = (Domain.Socioboard.Domain.FacebookAccount)lstFacebookAccounts[rnum];
            }
            int malecount = 0;
            int femalecount = 0;

            Domain.Socioboard.Domain.FacebookStats objfbStats = new Domain.Socioboard.Domain.FacebookStats();
            FacebookStatsRepository objFBStatsRepo = new FacebookStatsRepository();

            FacebookClient fb = new FacebookClient(); 
            fb.AccessToken = _facebookAccount.AccessToken;
            try
            {
                dynamic data = fb.Get("v2.0/me/friends?fields=gender");

                //dynamic data, dynamic profile, Guid userId


                foreach (var item in data["data"])
                {

                    try
                    {
                        if (item["gender"] == "male")
                            malecount++;
                        else if (item["gender"] == "female")
                            femalecount++;
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                return false;
            }
            objfbStats.EntryDate = DateTime.Now;
            objfbStats.FbUserId = _facebookAccount.FbUserId;//profile["id"].ToString();
            objfbStats.FemaleCount = femalecount;
            objfbStats.Id = Guid.NewGuid();
            objfbStats.MaleCount = malecount;
            objfbStats.UserId = _facebookAccount.UserId;
            objfbStats.FanCount = getfanCount(objfbStats, _facebookAccount.AccessToken);

            if (objFBStatsRepo.checkFacebookStatsExists(objfbStats.FbUserId.ToString(), objfbStats.UserId, objfbStats.FanCount, objfbStats.MaleCount, objfbStats.FemaleCount))
            {
                objFBStatsRepo.addFacebookStats(objfbStats);
            }


            return true;
        }
        public void getfbFriendsGenderStatsForFanPage(dynamic profile, Guid userId, ref FacebookAccount objfbacnt)
        {
            FacebookStats objfbStats = new FacebookStats();
            FacebookStatsRepository objFBStatsRepo = new FacebookStatsRepository();
            //int malecount = 0;
            //int femalecount = 0;
            //foreach (var item in data["data"])
            //{
            //    if (item["gender"] == "male")
            //        malecount++;
            //    else if (item["gender"] == "female")
            //        femalecount++;
            //}
            objfbStats.EntryDate = DateTime.Now;
            objfbStats.FbUserId = profile["id"].ToString();
            //objfbStats.FemaleCount = femalecount;
            objfbStats.Id = Guid.NewGuid();
            //objfbStats.MaleCount = malecount;
            objfbStats.UserId = userId;
            objfbStats.FanCount = objfbacnt.Friends;
            //objfbStats.ShareCount = getShareCount();
            //objfbStats.CommentCount = getCommentCount();
            //objfbStats.LikeCount = getLikeCount();
            objFBStatsRepo.addFacebookStats(objfbStats);
            FacebookInsightStatsHelper objfbinshlpr = new FacebookInsightStatsHelper();
            string pId = profile["id"].ToString();
            //string pId = "329139457226886";
            objfbinshlpr.getPageImpresion(pId, userId, 7);

        }
        private void AddFacebookStats(string UserId, FacebookClient fb, dynamic profile)
        {
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                dynamic friendsgenderstats = fb.Get("v2.0/" + profile["id"] + "/friends?fields=gender");
                Domain.Socioboard.Domain.FacebookStats objfbStats = new Domain.Socioboard.Domain.FacebookStats();
                FacebookStatsRepository objFBStatsRepo = new FacebookStatsRepository();
                int malecount = 0;
                int femalecount = 0;
                foreach (var item in friendsgenderstats["data"])
                {
                    if (item["gender"] == "male")
                        malecount++;
                    else if (item["gender"] == "female")
                        femalecount++;
                }
                objfbStats.EntryDate = DateTime.Now;
                objfbStats.FbUserId = profile["id"].ToString();
                objfbStats.FemaleCount = femalecount;
                objfbStats.Id = Guid.NewGuid();
                objfbStats.MaleCount = malecount;
                objfbStats.UserId = Guid.Parse(UserId);


                #region fancount
                int fancountPage = 0;
                try
                {
                    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                    //dynamic fancount = fb.Get("fql", new { q = " SELECT fan_count FROM page WHERE page_id =" + objfbStats.FbUserId });
                    //foreach (var friend in fancount.data)
                    //{
                    //    fancountPage = Convert.ToInt32(friend.fan_count);
                    //}

                    dynamic friends = fb.Get("v2.0/" + objfbStats.FbUserId + "/friends");
                    fancountPage = Convert.ToInt16(friends["summary"]["total_count"].ToString());

                }
                catch (Exception)
                {
                    fancountPage = 0;
                }
                #endregion

                objfbStats.FanCount = fancountPage;//getfanCount(objfbStats,fb.AccessToken.ToString());
                objFBStatsRepo.addFacebookStats(objfbStats);

            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
            }

        }
        public void getfbFriendsGenderStats(dynamic data, dynamic profile, Guid userId)
        {
            FacebookStats objfbStats = new FacebookStats();
            FacebookStatsRepository objFBStatsRepo = new FacebookStatsRepository();
            int malecount = 0;
            int femalecount = 0;
            foreach (var item in data["data"])
            {
                if (item["gender"] == "male")
                    malecount++;
                else if (item["gender"] == "female")
                    femalecount++;
            }
            objfbStats.EntryDate = DateTime.Now;
            objfbStats.FbUserId = profile["id"].ToString();
            objfbStats.FemaleCount = femalecount;
            objfbStats.Id = Guid.NewGuid();
            objfbStats.MaleCount = malecount;
            objfbStats.UserId = userId;
            objfbStats.FanCount = getfanCount(ref objfbStats);
            objFBStatsRepo.addFacebookStats(objfbStats);

        }