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);
        }
        public ulong GetUserId(string signedRequest)
        {
            var client = new global::Facebook.FacebookClient
            {
                AppId = Settings.AppId,
                AppSecret = Settings.AppSecret
            };

//            var result = client.Get("oauth/access_token", new
//            {
//                client_id = client.AppId,
//                client_secret = client.AppSecret,
//                grant_type = "client_credentials"
//            });
//            client.AccessToken = result.access_token;

            var userId = (client.ParseSignedRequest(signedRequest) as dynamic).user_id;
            
            return UInt64.Parse(userId);
        }
        private object GetObjects(Type modelType, string userFacebookId, global::Facebook.FacebookClient client)
        {
            var typeName = GetTypeName(modelType);

            if (String.IsNullOrEmpty(typeName))
            {
                return(null);
            }

            var objects = facebookObjectStorageService.GetObjects(userFacebookId);

            if (objects == null || objects.Count == 0 /* || Some other time period has been met and we should sync again */)
            {
                return(LoadObjects(client, modelType, userFacebookId, "me/" + typeName + facebookService.GetFields(modelType)));
            }

            var castedObjects = (IList)Activator.CreateInstance(modelType);

            foreach (var obj in objects)
            {
                castedObjects.Add(obj);
            }
            return(castedObjects);
        }
        private object LoadObjects(global::Facebook.FacebookClient client, Type modelType, string userFacebookId, string query)
        {
            dynamic objects     = Activator.CreateInstance(modelType);
            dynamic objectList  = client.Get(query);
            var     genericType = GetGenericType(modelType);

            if (objects.Count == 0)
            {
                foreach (var obj in objectList.data)
                {
                    dynamic o = Activator.CreateInstance(genericType);
                    o.FacebookId     = obj.Id;
                    o.FacebookUserId = userFacebookId;
                    o.Data           = obj;
                    ApplyFields(o, obj);
                    objects.Add(o);
                    facebookObjectStorageService.AddObject((FacebookObject)o);
                }
            }
            else
            {
                // add, edit
                foreach (var obj in objectList.data)
                {
                    dynamic o = FindById(objects, obj.id);
                    if (o == null)
                    {
                        o                = Activator.CreateInstance(genericType);
                        o.FacebookId     = obj.Id;
                        o.FacebookUserId = userFacebookId;
                        o.Data           = obj;
                        ApplyFields(o, obj);
                        objects.Add(o);
                        facebookObjectStorageService.AddObject((FacebookObject)o);
                        continue;
                    }
                    ApplyFields(o, obj);
                    facebookObjectStorageService.UpdateObject((FacebookObject)o);
                }

                // remove
                var removeCount = 0;
                for (var i = 0; i < (objects.Count - removeCount); i++)
                {
                    var obj         = objects[i - removeCount];
                    var foundObject = false;
                    foreach (var newObject in objectList.data)
                    {
                        if (obj.FacebookId == newObject.id)
                        {
                            foundObject = true;
                            break;
                        }
                    }
                    if (foundObject)
                    {
                        objects.RemoveAt(i - removeCount);
                        i--;
                        removeCount++;
                    }
                }
            }

            return(objects);
        }