angular.module("mediaModal")
.factory("pexels", function($http, pexelsApiKey, $q){
        function search(searchString, page, count){
            return $http.get("http://api.pexels.com/v1/search?query=" + searchString + "&per_page=" + count + "&page=" + (page + 1), {
                    headers: {
                        "Authorization": pexelsApiKey
                    }
                })
                .then(function(response){
                    return {
                        count: response.data.total_results,
                        images: response.data.photos.map(function(photo){
                            return {
                                thumbnail: photo.src.square,
                                full: photo.src.large,
                                title: photo.photographer
                            };
                        })
                    };
                },
                function(response){
                    return $q.reject(response.data);
                }
            );
        }
        return {
            search: search
        }
    }
);