/// <summary>
        /// Get twitter account info
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static SocialMediaResponseModel GetTwitterAccountInfo(SocialMediaResponseModel model)
        {
            var authorizer = new MvcAuthorizer
            {
                CredentialStore = new SingleUserInMemoryCredentialStore
                {
                    ConsumerKey       = model.AppId,
                    ConsumerSecret    = model.AppSecret,
                    AccessToken       = model.AccessToken,
                    AccessTokenSecret = model.AccessTokenSecret
                }
            };

            var context = new TwitterContext(authorizer);

            var account = (from a in context.Account
                           where a.Type == AccountType.VerifyCredentials
                           select a).SingleOrDefault();

            if (account != null)
            {
                model.FullName = account.User.ScreenNameResponse;
            }

            return(model);
        }
        /// <summary>
        /// Save social media response
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseModel SaveSocialMediaResponse(SocialMediaResponseModel model)
        {
            var socialMediaToken = GetById(model.Id);

            if (socialMediaToken != null)
            {
                socialMediaToken.FullName          = model.FullName;
                socialMediaToken.Email             = model.Email;
                socialMediaToken.AccessToken       = model.AccessToken;
                socialMediaToken.AccessTokenSecret = model.AccessTokenSecret;
                socialMediaToken.Verifier          = model.Verifier;
                socialMediaToken.ExpiredDate       = model.ExpiredDate;
                socialMediaToken.Status            = SocialMediaEnums.TokenStatus.Active;

                //If there are no default token for this social network then assign current token as default one
                if (!Fetch(token => token.IsDefault && token.SocialMediaId == socialMediaToken.SocialMediaId).Any())
                {
                    socialMediaToken.IsDefault = true;
                }

                var response = Update(socialMediaToken);

                return(response.SetMessage(response.Success
                    ? T("SocialMediaToken_Message_UpdateSuccessfully")
                    : T("SocialMediaToken_Message_UpdateFailure")));
            }

            return(new ResponseModel
            {
                Success = false,
                Message = T("SocialMediaToken_Message_ObjectNotFound")
            });
        }
        /// <summary>
        /// Post status
        /// </summary>
        /// <param name="model"></param>
        /// <param name="network"></param>
        /// <returns></returns>
        public ResponseModel Post(SocialMessageModel model, SocialMediaEnums.SocialNetwork network)
        {
            try
            {
                var token = GetActiveTokenOfSocialMedia(network);

                if (token != null)
                {
                    object response       = null;
                    var    authorizeModel = new SocialMediaResponseModel
                    {
                        AppId             = token.AppId,
                        AppSecret         = token.AppSecret,
                        AccessToken       = token.AccessToken,
                        AccessTokenSecret = token.AccessTokenSecret
                    };

                    switch (network)
                    {
                    case SocialMediaEnums.SocialNetwork.Facebook:
                        response = SocialUtilities.PostFacebookStatus(authorizeModel, model);
                        break;

                    case SocialMediaEnums.SocialNetwork.Twitter:
                        response = SocialUtilities.PostTwitterStatus(authorizeModel, model);
                        break;

                    case SocialMediaEnums.SocialNetwork.LinkedIn:
                        response = SocialUtilities.PostLinkedInStatus(authorizeModel, model);
                        break;
                    }

                    var responseResult = string.Empty;
                    if (response != null)
                    {
                        responseResult = SerializeUtilities.Serialize(response);
                    }

                    return(new ResponseModel
                    {
                        Success = true,
                        Data = responseResult
                    });
                }

                return(new ResponseModel
                {
                    Success = false,
                    Message = T("SocialMediaToken_Message_ObjectNotFound")
                });
            }
            catch (Exception exception)
            {
                return(new ResponseModel(exception));
            }
        }
        /// <summary>
        /// Get facebook access token
        /// </summary>
        /// <param name="model"></param>
        /// <param name="callbackUrl"></param>
        /// <returns></returns>
        public static SocialMediaResponseModel GetFacebookAccountInfo(SocialMediaResponseModel model, string callbackUrl)
        {
            model.Verifier = HttpContext.Current.Request["code"];

            #region Get Access Token

            var client = new FacebookClient();

            dynamic response = client.Get("/oauth/access_token", new
            {
                client_id     = model.AppId,
                client_secret = model.AppSecret,
                code          = model.Verifier,
                redirect_uri  = callbackUrl
            });

            model.AccessToken = response.access_token;

            //Default expiry date of Facebook is 60 days
            model.ExpiredDate = DateTime.UtcNow.AddDays(60);

            #endregion

            #region Get account info

            client = new FacebookClient(model.AccessToken);

            dynamic me = client.Get("me", new
            {
                fields = "name,email,first_name,last_name"
            });
            model.Email    = me.email;
            model.FullName = StringUtilities.GenerateFullName(me.first_name, me.last_name);

            #endregion

            return(model);
        }
        /// <summary>
        /// Get LinkedIn account info
        /// </summary>
        /// <param name="model"></param>
        /// <param name="callbackUrl"></param>
        /// <returns></returns>
        public static SocialMediaResponseModel GetLinkedInAccountInfo(SocialMediaResponseModel model, string callbackUrl)
        {
            model.Verifier = HttpContext.Current.Request["code"];

            DateTime expiredDate;

            // Get access token and expired date
            model.AccessToken = LinkedInHelper.GetAccessToken(model.AppId, model.AppSecret, model.Verifier, callbackUrl, out expiredDate);

            model.ExpiredDate = expiredDate;

            #region Get User Profile

            var linkedLinkHelper = new LinkedInHelper(model.AccessToken);

            var userInfo = linkedLinkHelper.GetCurrentUser();

            model.FullName = StringUtilities.GenerateFullName(userInfo.FirstName, userInfo.LastName);
            model.Email    = userInfo.Email;

            #endregion

            return(model);
        }
        /// <summary>
        /// Get social information
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel SaveSocialMediaTokenInformation(int id)
        {
            var socialMediaToken = GetById(id);

            if (socialMediaToken != null)
            {
                var model = new SocialMediaResponseModel
                {
                    Id                = socialMediaToken.Id,
                    AppId             = socialMediaToken.AppId,
                    AppSecret         = socialMediaToken.AppSecret,
                    FullName          = socialMediaToken.FullName,
                    Email             = socialMediaToken.Email,
                    AccessToken       = socialMediaToken.AccessToken,
                    AccessTokenSecret = socialMediaToken.AccessTokenSecret,
                    Verifier          = socialMediaToken.Verifier
                };

                var callbackUrl = UrlUtilities.GenerateUrl(
                    HttpContext.Current.Request.RequestContext,
                    "SocialMediaTokens",
                    "Callback", new
                {
                    id
                }, true);

                try
                {
                    switch (socialMediaToken.SocialMediaId.ToEnum <SocialMediaEnums.SocialNetwork>())
                    {
                    case SocialMediaEnums.SocialNetwork.Facebook:
                        model = SocialUtilities.GetFacebookAccountInfo(model, callbackUrl);
                        break;

                    case SocialMediaEnums.SocialNetwork.Twitter:
                        model = SocialUtilities.GetTwitterAccountInfo(model);
                        break;

                    case SocialMediaEnums.SocialNetwork.LinkedIn:
                        //Specific callback url for LinkedIn
                        callbackUrl = UrlUtilities.GenerateUrl(
                            HttpContext.Current.Request.RequestContext,
                            "SocialMediaTokens",
                            "LinkedInCallback", null, true);

                        StateManager.SetSession(EzCMSContants.LinkedInCallbackId, id);

                        model = SocialUtilities.GetLinkedInAccountInfo(model, callbackUrl);
                        break;
                    }
                    return(SaveSocialMediaResponse(model));
                }
                catch (Exception exception)
                {
                    return(new ResponseModel
                    {
                        Success = false,
                        Message = T("SocialMediaToken_Message_InvalidConfigure"),
                        DetailMessage = exception.BuildErrorMessage()
                    });
                }
            }

            return(new ResponseModel
            {
                Success = false
            });
        }