/*Checks to see if there are any new pics in the results returned from facebook, if yes, they are added to the database, otherwise return true "All Pics Added"
         */
        private Boolean SortPictures(PicturePlaceObject results, PicturePlaceDb db)
        {
            Boolean addedAllPics = true;

            foreach (var pic in results.data)                        //loop through all the Pics in results.data
            {
                if (pic.place != null && pic.place.location != null) //Only add Pics that have a place/location from results
                {
                    var placeId = db.data.Select(id => id.id);       //COLLECTION OF ALL THE IDS IN THE DATABASE
                    //check to see if the collection of ids already contains the new id, if not a new pic is found etc.
                    if (!placeId.Contains(pic.id))
                    {
                        addedAllPics = false;
                        StorePicture(pic, db);
                    }
                }
            }
            return(addedAllPics);
        }
        /*Method keeps sending get requets to facebook api using paging, untill all pictures have been added
         */
        private async void GetAllFbPic(PicturePlaceObject results, PropertySet parameters, string endpoint, PicturePlaceDb db)
        {
            Boolean addedAllPics = false;

            do                                            //only do this while there is a next page and all pics have not been added
            {
                addedAllPics = SortPictures(results, db); //Add Results to a list

                if (addedAllPics == false)
                {
                    parameters.Remove("after");                                                                    //Remove previous parameters
                    parameters.Add("after", results.paging.cursors.after);                                         //the next page to send the request too

                    FBSingleValue value       = new FBSingleValue(endpoint, parameters, DeserializeJson.FromJson); //send the request and get back a JSON responce
                    FBResult      graphResult = await value.GetAsync();                                            //check to see if the Requets Succeeded

                    results = graphResult.Object as PicturePlaceObject;
                }
            } while ((results.paging != null || results.data.Count() != 0) && addedAllPics == false);
            PaintPins(db);
        }
        /*On Successful login, send GET request to FB endpoint with params and DeserializeJson the results into objects/Dictonary
         */
        private async void OnSuccessLogin()
        {
            string endpoint = "/me/photos";//where the url starts from

            PropertySet parameters = new PropertySet();

            parameters.Add("fields", "source,place");                                                      //Required fields needed

            FBSingleValue value       = new FBSingleValue(endpoint, parameters, DeserializeJson.FromJson); //send the request and get back a JSON responce
            FBResult      graphResult = await value.GetAsync();

            if (graphResult.Succeeded)//check to see if the Request Succeeded
            {
                PicturePlaceObject results = graphResult.Object as PicturePlaceObject;

                var db = new PicturePlaceDb();
                GetAllFbPic(results, parameters, endpoint, db);
            }
            else
            {
                MessageDialog dialog = new MessageDialog("Try Again Later!");
                await dialog.ShowAsync();
            }
        }