public void addFacebookUser(Domain.Socioboard.Domain.FacebookFanPage fbpage)
 {
     //Creates a database connection and opens up a session
     using (NHibernate.ISession session = SessionFactory.GetNewSession())
     {
         //After Session creation, start Transaction.
         using (NHibernate.ITransaction transaction = session.BeginTransaction())
         {
             //proceed action, to save data.
             try
             {
                 session.Save(fbpage);
                 transaction.Commit();
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.StackTrace);
             }
         } //End Transaction
     }     //End session
 }
        public string GetFacebookPageData(string FbId, string UserId)
        {
            string ret = string.Empty;
            try
            {
                Domain.Socioboard.Domain.FacebookAccount objFacebookAccount = new Domain.Socioboard.Domain.FacebookAccount();
                if (objFacebookAccountRepository.checkFacebookUserExists(FbId, Guid.Parse(UserId)))
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId, Guid.Parse(UserId));
                }
                else
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId);
                }
                FacebookClient fb = new FacebookClient();
                string profileId = string.Empty;
                string accessToken = objFacebookAccount.AccessToken;
                dynamic profile = null;
                dynamic friends = null;
                if (accessToken != null)
                {
                    fb.AccessToken = accessToken;
                    int fancountPage = 0;
                    try
                    {
                        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                        friends = fb.Get("v2.0/" + FbId);
                        fancountPage = Convert.ToInt16(friends["likes"].ToString());
                        objFacebookAccount.Friends = fancountPage;
                    }
                    catch (Exception)
                    {
                        fancountPage = 0;
                        UpdateSocialprofileStatus(UserId, FbId);
                        objFacebookAccount.IsActive = 2;
                        UpdateFacebookAccount(objFacebookAccount);
                    }
                    try
                    {
                        profile = fb.Get("v2.0/me");

                    }
                    catch (Exception ex)
                    {
                        string errormssg = ex.Message;
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        return "Token Expired";
                    }
                    if (objFacebookAccountRepository.checkFacebookUserExists(Convert.ToString(profile["id"]), Guid.Parse(UserId)))
                    {
                        #region Update FacebookAccount
                        UpdateFacebookAccount(objFacebookAccount);
                        #endregion
                        #region AddFacebook FanPge
                        Domain.Socioboard.Domain.FacebookFanPage objFacebookFanPage = new Domain.Socioboard.Domain.FacebookFanPage();
                        FacebookFanPageRepository objFacebookFanPageRepository = new FacebookFanPageRepository();
                        objFacebookFanPage.Id = Guid.NewGuid();
                        objFacebookFanPage.UserId = Guid.Parse(UserId);
                        objFacebookFanPage.ProfilePageId = (Convert.ToString(profile["id"]));
                        objFacebookFanPage.FanpageCount = fancountPage.ToString();
                        objFacebookFanPage.EntryDate = DateTime.Now;
                        objFacebookFanPageRepository.addFacebookUser(objFacebookFanPage);
                        #endregion
                        #region UpdateTeammemberprofile
                        Domain.Socioboard.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Socioboard.Domain.TeamMemberProfile();
                        objTeamMemberProfile.ProfileName = objFacebookAccount.FbUserName;
                        objTeamMemberProfile.ProfilePicUrl = "http://graph.facebook.com/" + objFacebookAccount.FbUserId + "/picture?type=small";
                        objTeamMemberProfile.ProfileId = objFacebookAccount.FbUserId;
                        objTeamMemberProfileRepository.updateTeamMemberbyprofileid(objTeamMemberProfile);
                        #endregion

                        //getUserNotifications(UserId,fb,profile);
                        GetFacebookPageFeed(objFacebookAccount.AccessToken, objFacebookAccount.FbUserId);
                        ret = "Facebook page info Updated Successfully";
                    }
                    else
                    {
                        ret = "Account already Exist !";
                    }

                }
                return new JavaScriptSerializer().Serialize(ret);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
                return "Something Went Wrong";
            }
        }
        public string GetFacebookData(string FbId, string UserId)
        {
            string ret = string.Empty;
            long friendscount = 0;
            try
            {
                Domain.Socioboard.Domain.FacebookAccount objFacebookAccount = new Domain.Socioboard.Domain.FacebookAccount();
                if (objFacebookAccountRepository.checkFacebookUserExists(FbId, Guid.Parse(UserId)))
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId, Guid.Parse(UserId));
                }
                else
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FbId);
                }


                FacebookClient fb = new FacebookClient();
                string profileId = string.Empty;
                string accessToken = objFacebookAccount.AccessToken;
                dynamic profile = null;
                dynamic friends = null;
                if (accessToken != null)
                {
                    fb.AccessToken = accessToken;
                    try
                    {
                        profile = fb.Get("v2.0/me");

                    }
                    catch (Exception ex)
                    {
                        string errormssg = ex.Message;
                        if (errormssg.Contains("changed the password"))
                        {
                            UpdateSocialprofileStatus(UserId, FbId);
                            objFacebookAccount.IsActive = 2;
                            UpdateFacebookAccount(objFacebookAccount);
                        }
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        return "Token Expired";
                    }
                    try
                    {
                        friends = fb.Get("v2.0/me/friends");

                    }
                    catch (Exception ex)
                    {
                        string errormssg = ex.Message;
                        if (errormssg.Contains("changed the password"))
                        {
                            UpdateSocialprofileStatus(UserId, FbId);
                        }
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        return "Token Expired";
                    }

                    try
                    {
                        friendscount = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                        objFacebookAccount.Friends = Convert.ToInt32(friendscount);
                    }
                    catch (Exception ex)
                    {
                        //try
                        //{
                        //    dynamic frndscount = fb.Get("fql", new { q = "SELECT friend_count FROM user WHERE uid=me()" });

                        //    foreach (var friend in frndscount.data)
                        //    {
                        //        frndscount = friend.friend_count;
                        //    }
                        //    friendscount = Convert.ToInt16(frndscount);
                        //}
                        //catch (Exception exx)
                        //{
                        //    friendscount = 0;
                        //    logger.Error(exx.Message);
                        //    logger.Error(exx.StackTrace);
                        //}
                        friendscount = 0;
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                    }

                    try
                    {
                        objFacebookAccount.FbUserName = (Convert.ToString(profile["name"]));
                    }
                    catch { }

                    if (objFacebookAccountRepository.checkFacebookUserExists(Convert.ToString(profile["id"]), Guid.Parse(UserId)))
                    {
                        #region Update FacebookAccount
                        UpdateFacebookAccount(objFacebookAccount);
                        #endregion
                        #region UpdateTeammemberprofile
                        Domain.Socioboard.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Socioboard.Domain.TeamMemberProfile();
                        objTeamMemberProfile.ProfileName = objFacebookAccount.FbUserName;
                        objTeamMemberProfile.ProfilePicUrl = "http://graph.facebook.com/" + objFacebookAccount.FbUserId + "/picture?type=small";
                        objTeamMemberProfile.ProfileId = objFacebookAccount.FbUserId;
                        objTeamMemberProfileRepository.updateTeamMemberbyprofileid(objTeamMemberProfile);
                        #endregion

                        #region UpdateFacebook FanPge
                        Domain.Socioboard.Domain.FacebookFanPage objFacebookFanPage = new Domain.Socioboard.Domain.FacebookFanPage();
                        FacebookFanPageRepository objFacebookFanPageRepository = new FacebookFanPageRepository();
                        objFacebookFanPage.Id = Guid.NewGuid();
                        objFacebookFanPage.UserId = Guid.Parse(UserId);
                        objFacebookFanPage.ProfilePageId = FbId;
                        objFacebookFanPage.FanpageCount = friendscount.ToString();
                        objFacebookFanPage.EntryDate = DateTime.Now;
                        objFacebookFanPageRepository.addFacebookUser(objFacebookFanPage);
                        #endregion

                        #region Add Facebook Feeds
                        //AddFacebookFeeds(UserId, fb, profile);
                        AddFacebookFeedsWithPagination(UserId, fb, profile);
                        #endregion
                        #region Add Facebook User Home
                        AddFacebookUserHome(UserId, fb, profile);
                        #endregion
                        #region Add Facebook User Inbox Message
                        AddFacebookMessageWithPagination(UserId, fb, profile);
                        #endregion
                        #region Add Facebook User Notifications
                        // getUserNotifications(UserId, fb, profile);
                        #endregion
                        ret = "Facebook info Updated Successfully";
                    }
                    else
                    {
                        ret = "Account already Exist !";
                    }
                }
                return new JavaScriptSerializer().Serialize(ret);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                Console.WriteLine(ex.StackTrace);
                return "Something Went Wrong";
            }
        }
        public string AddFacebookPagesInfo(string userid, string profileId, string accessToken, string groupId, string email)
        {
            string ret = string.Empty;
            try
            {
                FacebookClient fb = new FacebookClient();
                fb.AccessToken = accessToken;
                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 =" + profileId });
                    //foreach (var friend in fancount.data)
                    //{
                    //    fancountPage = Convert.ToInt32(friend.fan_count);
                    //}

                    // dynamic friends = fb.Get("v2.0/me/friends");
                    dynamic friends = fb.Get("v2.0/" + profileId);
                    //fancountPage = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                    fancountPage = Convert.ToInt32(friends["likes"].ToString());

                }
                catch (Exception)
                {
                    fancountPage = 0;
                }

                if (accessToken != null)
                {
                    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;

                    dynamic profile = fb.Get("v2.0/me");

                    #region Add FacebookAccount
                    objFacebookAccount = new Domain.Socioboard.Domain.FacebookAccount();
                    objFacebookAccount.Id = Guid.NewGuid();
                    objFacebookAccount.FbUserId = (Convert.ToString(profile["id"]));
                    objFacebookAccount.FbUserName = (Convert.ToString(profile["name"]));
                    objFacebookAccount.AccessToken = accessToken;
                    objFacebookAccount.Friends = Convert.ToInt32(fancountPage);
                    objFacebookAccount.EmailId = email;
                    objFacebookAccount.Type = "Page";
                    objFacebookAccount.ProfileUrl = (Convert.ToString(profile["link"]));
                    objFacebookAccount.IsActive = 1;
                    objFacebookAccount.UserId = Guid.Parse(userid);
                    if (!objFacebookAccountRepository.checkFacebookUserExists(objFacebookAccount.FbUserId, objFacebookAccount.UserId))
                    {
                        objFacebookAccountRepository.addFacebookUser(objFacebookAccount);
                    }

                    #endregion
                    #region SocialProfile
                    Domain.Socioboard.Domain.SocialProfile objSocialProfile = new Domain.Socioboard.Domain.SocialProfile();
                    objSocialProfile.Id = Guid.NewGuid();
                    objSocialProfile.ProfileType = "facebook_page";
                    objSocialProfile.ProfileId = (Convert.ToString(profile["id"]));
                    objSocialProfile.UserId = Guid.Parse(userid);
                    objSocialProfile.ProfileDate = DateTime.Now;
                    objSocialProfile.ProfileStatus = 1;
                    if (!objSocialProfilesRepository.checkUserProfileExist(objSocialProfile))
                    {
                        objSocialProfilesRepository.addNewProfileForUser(objSocialProfile);
                    }
                    #endregion
                    #region Add TeamMemberProfile
                    Domain.Socioboard.Domain.Team objTeam = objTeamRepository.GetTeamByGroupId(Guid.Parse(groupId));
                    Domain.Socioboard.Domain.TeamMemberProfile objTeamMemberProfile = new Domain.Socioboard.Domain.TeamMemberProfile();
                    objTeamMemberProfile.Id = Guid.NewGuid();
                    objTeamMemberProfile.TeamId = objTeam.Id;
                    objTeamMemberProfile.Status = 1;
                    objTeamMemberProfile.ProfileType = "facebook_page";
                    objTeamMemberProfile.StatusUpdateDate = DateTime.Now;
                    objTeamMemberProfile.ProfileId = Convert.ToString(profile["id"]);

                    objTeamMemberProfile.ProfileName = objFacebookAccount.FbUserName;
                    objTeamMemberProfile.ProfilePicUrl = "http://graph.facebook.com/" + objTeamMemberProfile.ProfileId + "/picture?type=small";

                    if (!objTeamMemberProfileRepository.checkTeamMemberProfile(objTeamMemberProfile.TeamId, objTeamMemberProfile.ProfileId))
                    {
                        objTeamMemberProfileRepository.addNewTeamMember(objTeamMemberProfile);
                    }

                    #endregion
                    #region Add Facebook Feeds
                    AddFacebookFeeds(userid, fb, profile);
                    #endregion
                    #region AddFacebook FanPge
                    Domain.Socioboard.Domain.FacebookFanPage objFacebookFanPage = new Domain.Socioboard.Domain.FacebookFanPage();
                    FacebookFanPageRepository objFacebookFanPageRepository = new FacebookFanPageRepository();
                    objFacebookFanPage.Id = Guid.NewGuid();
                    objFacebookFanPage.UserId = Guid.Parse(userid);
                    objFacebookFanPage.ProfilePageId = (Convert.ToString(profile["id"]));
                    objFacebookFanPage.FanpageCount = fancountPage.ToString();
                    objFacebookFanPage.EntryDate = DateTime.UtcNow;
                    objFacebookFanPageRepository.addFacebookUser(objFacebookFanPage);
                    #endregion

                    AddFbPagePost(userid, accessToken, profileId);
                    getPageConversations(userid, fb, profile);
                    GetFacebookPageFeed(accessToken, profileId);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            return ret;
        }