private object GetUser(Type modelType, string facebookId, global::Facebook.FacebookClient client)
        {
            var user = facebookUserStorageService.GetUser(facebookId);

            //TODO: (ErikPo) Fill in the other half of this condition
            if (user == null /* or this is the first time the app has started */)
            {
                object userFields = client.Get("me" + (!String.IsNullOrEmpty(fields) ? "?fields=" + fields.Replace(" ", String.Empty) : facebookService.GetFields(modelType)));

                if (modelType == typeof(FacebookUser))
                {
                    return(new FacebookUser {
                        Data = userFields
                    });
                }
                else if (modelType == typeof(object))
                {
                    return((dynamic)userFields);
                }

                user      = (FacebookUser)Activator.CreateInstance(modelType);
                user.Data = userFields;

                var          facebookFields = GetUserFields(modelType);
                PropertyInfo userProperty;
                string       facebookFieldName;
                object       fieldValue;
                foreach (var field in facebookFields)
                {
                    if (field.Value != null && field.Value.Ignore)
                    {
                        continue;
                    }
                    userProperty      = field.Key;
                    facebookFieldName = field.Value != null ? field.Value.JsonField : String.Empty;
                    if (!String.IsNullOrEmpty(facebookFieldName))
                    {
                        fieldValue = GetFBFieldValue(userFields, facebookFieldName.Split('.'));
                    }
                    else
                    {
                        fieldValue = GetFBFieldValue(userFields, new[] { userProperty.Name });
                    }
                    if (fieldValue != null)
                    {
                        userProperty.SetValue(user, fieldValue, null);
                    }
                }

                facebookUserStorageService.AddUser(user);
            }

            return(user);
        }
        private TUser RefreshUser <TUser>(TUser user, string accessToken) where TUser : FacebookUser, new()
        {
            var isNewUser   = false;
            var changedUser = false;
            var userFields  = GetActualFields(typeof(TUser)).Select(uf => uf.Value.FieldName).Distinct();

            if (userFields.Count() == 1 && userFields.ElementAt(0) == "id")
            {
                return(user);
            }

            var userFieldsQuery = userFields.Count() > 0 ? "?fields=" + String.Join(",", userFields) : String.Empty;
            var client          = CreateClient(accessToken);

            if (user == null)
            {
                user      = new TUser();
                isNewUser = true;
            }

            if (isNewUser)
            {
                dynamic me = client.Get("me" + userFieldsQuery);
                RefreshUserFields(user, me, userFields.ToArray());
                changedUser = true;
            }

            //TODO: (ErikPo) Move this out of here and make it more generic
            var types = GetFBFieldTypes(((TUser)user).GetType());

            if (types.Count > 0)
            {
                foreach (var type in types)
                {
                    switch (type.Value.TypeName)
                    {
                    case "friends":
                        var changedFriends = LoadFriends <TUser>(client, user, type.Key, type.Value, userFields, userFieldsQuery);
                        if (changedFriends)
                        {
                            changedUser = true;
                        }
                        break;
                    }
                }
            }

            if (changedUser)
            {
                if (isNewUser)
                {
                    facebookUserStorageService.AddUser(user);
                }
                else
                {
                    facebookUserStorageService.UpdateUser(user);
                }
            }

            return(user);
        }