angular
        .module('carfriend')
        .factory('ParkingService', parkingService);

function parkingService(Backand, $http, $q, $filter) {
    var baseUrl = '/1/objects/';
    var object = 'parking/';

    var services = {
        post: post,
        getCarWithinPosition: getCarWithinPosition,
        lastParkingInfo: lastParkingInfo,
        getCurrentSecurity:getCurrentSecurity,
        disableSecurity: disableSecurity
    };
    return services;

    ////////////

    function getUrl() {
        return Backand.getApiUrl() + baseUrl + object;
    }
    ;

    function getUrlForId(id) {
        return getUrl() + id;
    }
    ;

    function post(parking) {
        return $http.post(getUrl(), parking);
    }
    ;

    function disableSecurity(id) {
        var deffered = $q.defer();
        $http({
            method: 'PUT',
            url: getUrlForId(id),
            data: {
                security: false
            }
        }).then(function (res) {
            deffered.resolve(res);
        }, function (err) {
            deffered.reject(err);
        });
        return deffered.promise;
    }
    ;
    function getCurrentSecurity(carId) {
        var deffered = $q.defer();
        $http({
            method: 'GET',
            url: getUrl(),
            params: {
                pageSize: 1,
                sort: [{
                        "fieldName": "from",
                        "order": "desc"
                    }],
                filter: JSON.stringify([{
                        "fieldName": "car",
                        "operator": "in",
                        "value": carId
                    }, {
                        "fieldName": "security",
                        "operator": "equals",
                        "value": true
                    }])
            }
        }).then(function (res) {
            deffered.resolve(res);
        }, function (err) {
            deffered.reject(err);
        });
        return deffered.promise;
    }
    ;

    function lastParkingInfo(carId) {
        var deffered = $q.defer();
        $http({
            method: 'GET',
            url: getUrl(),
            params: {
                pageSize: 1,
                sort: [{
                        "fieldName": "from",
                        "order": "desc"
                    }],
                filter: JSON.stringify([{
                        "fieldName": "car",
                        "operator": "in",
                        "value": carId
                    }])
            }
        }).then(function (res) {
            deffered.resolve(res);
        }, function (err) {
            deffered.reject(err);
        });
        return deffered.promise;
    }
    ;

    function getCarWithinPosition(within, position) {
        var deffered = $q.defer();
        $http({
            method: 'GET',
            url: getUrl(),
            params: {
                deep: true,
                level: 6,
                filter:
                        {
                            "q": {
                                "geo": {"$within": [[position.latitude, position.longitude], within]},
                                "security": true
                            }
                        }

            }
        }).then(function (res) {
            console.log(res);
            deffered.resolve(res);
        }, function (err) {
            deffered.reject(err);
        });
        return deffered.promise;
    }
    ;

}
;

