var app = angular.module("app")
    .service("dataService", ['$http', '$q', 'ENV', function ($http, $q, ENV) {

        var service =  {};

        service.getCadastaProject = function(ckanProjectId){

            var deferred = $q.defer();

            // Cadasta API
            $http.get(ENV.apiCKANRoot + '/cadasta_get_all_projects?outputFormat=JSON&ckan_id=' + ckanProjectId, { cache: true })
                .then(function(response) {
                    deferred.resolve(response.data.result[0]);
                }, function(err) {
                    deferred.reject(err);
                });

            // CKAN API for project description
            return deferred.promise;
        };


        /**
         * This function returns a single Cadasta project with geometry
         * @returns {*}
         *
         */
        service.getCadastaProjectDetails = function(cadastaProjectId){
            var deferred = $q.defer();

            // Cadasta API
            $http.get(ENV.apiCKANRoot + '/cadasta_get_project_details?returnGeometry=true&project_id=' + cadastaProjectId, { cache: false })
                .then(function(response) {
                    if(response.data && response.data.error){
                        deferred.reject(response.data.error);
                    }
                    deferred.resolve(response.data.result);
                }, function(err) {
                    deferred.reject(err);
                });

            // CKAN API for project description

            return deferred.promise;
        };



        /**
         * This function gets all of the data required for the parcel overview page
         * @returns {*}
         *
         */
        service.getOverview = function(ckanProjectId, cadastaProjectId){

            var ckanProject = service.getCkanProject(ckanProjectId);
            var cadastaProject = service.getCadastaOverview(cadastaProjectId);

            return $q.all([ckanProject, cadastaProject]).then(function(results){

                results[1].features[0].properties.description = results[0].notes;
                return  results[1];
            });
        };

        service.getCadastaOverview = function(cadastaProjectId){
            var deferred = $q.defer();

            // Cadasta API
            $http.get(ENV.apiCKANRoot + '/cadasta_get_project_overview?project_id=' + cadastaProjectId, { cache: false })
                .then(function(response) {
                    if( response.data && response.data.error ){
                        deferred.reject(response.data.error);
                    }
                    deferred.resolve(response.data.result);
                }, function(response) {
                    deferred.reject(response);
                });

            return deferred.promise;
        };

        service.getCadastaMapData = function(cadastaProjectId){
            var deferred = $q.defer();

            // Cadasta API
            $http.get(ENV.apiCKANRoot + '/cadasta_get_project_mapdata?project_id=' + cadastaProjectId, { cache: false })
                .then(function(response) {
                    if( response.data && response.data.error ){
                        deferred.reject(response.data.error);
                    }
                    deferred.resolve(response.data.result);
                }, function(response) {
                    deferred.reject(response);
                });

            return deferred.promise;
        };

        service.getProjectResources = function(cadastaProjectId){
            var deferred = $q.defer();

            // Cadasta API
            $http.get(ENV.apiCKANRoot + '/cadasta_get_project_resources?project_id=' + cadastaProjectId, { cache: false })
                .then(function(response) {
                    if( response.data && response.data.error ){
                        deferred.reject(response.data.error);
                    }
                    deferred.resolve(response.data.result);
                }, function(response) {
                    deferred.reject(response);
                });

            return deferred.promise;
        };

        service.getCkanProject = function(ckanProjectId){

            var deferred = $q.defer();


            // CKAN API for project description
            $http.get(ENV.apiCKANRoot + '/package_show?id=' + ckanProjectId, { cache: false })
                .then(function(response) {
                    deferred.resolve(response.data.result);
                }, function(response) {
                    deferred.reject(response);
                });

            return deferred.promise;

        };

        /**
         * Get all activities associated with a project
         * @returns {*}
         */
        service.getProjectActivities = function(cadastaProjectId){

            var deferred = $q.defer();

            $http.get(ENV.apiCKANRoot + '/cadasta_get_project_activities?project_id=' + cadastaProjectId, { cache: false })
                .then(function(response) {
                    if(response.data && response.data.error){
                        deferred.reject(response.data.error);
                    }
                    deferred.resolve(response.data.result);
                }, function(response) {
                    deferred.reject(response);
                });

            return deferred.promise;
        };


        //todo pass in project_id as a parameter
        service.getProjectParcels = function(id){

            var deferred = $q.defer();

            $http.get(ENV.apiCadastaRoot + '/parcels?project_id=1', { cache: false })
                .then(function(response) {
                    deferred.resolve(response.data);
                }, function(response) {
                    deferred.reject(response);
                });

            return deferred.promise;
        };

        service.submitSurvey = function(){

            var deferred = $q.defer();

            var opts = {
                "headers":'Authorization'
            }

            $http.post('https://ona.io/api/v1/forms', { cache: false })
                .then(function(response) {
                    deferred.resolve(response.data);
                }, function(response) {
                    deferred.reject(response);
                });

            return deferred.promise;
        };

        return service;
    }])


@@ -5375,26 +5375,24 @@
      %7D;%0A
-%0A%0A
         
 //todo p
@@ -5387,477 +5387,8 @@
     
-//todo pass in project_id as a parameter%0A        service.getProjectParcels = function(id)%7B%0A%0A            var deferred = $q.defer();%0A%0A            $http.get(ENV.apiCadastaRoot + '/parcels?project_id=1', %7B cache: false %7D)%0A                .then(function(response) %7B%0A                    deferred.resolve(response.data);%0A                %7D, function(response) %7B%0A                    deferred.reject(response);%0A                %7D);%0A%0A            return deferred.promise;%0A        %7D;
 %0A%0A  

remove old service function