angular
        .module('carfriend')
        .factory('NotificationsService', notificationsService);

function notificationsService(Backand, $http, $q, $filter) {
    var baseUrl = '/1/objects/';
    var object = 'notifications/';
    var victimID = '';

    var services = {
        getVictimID: getVictimID,
        setVictimID: setVictimID,
        sendNotification: sendNotification,
        getSendNotifications: getSendNotifications,
        getReceivedNotifications: getReceivedNotifications,
        getNewNotifications: getNewNotifications,
        thankNotification: thankNotification,
        rateNotification: rateNotification,
        seenNotification: seenNotification,
        checkNewNotifications: checkNewNotifications,
        sendAreaNotification: sendAreaNotification,
        getHistoryOfCurrentPosition: getHistoryOfCurrentPosition,
        lightFormatNotifications: lightFormatNotifications
    };

    return services;

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

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

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

    function getVictimID() {
        return victimID;
    }
    ;

    function setVictimID(id) {
        victimID = id;
    }
    ;
    function sendAreaNotification(notif) {
        var deffered = $q.defer();
        $http({
            method: 'POST',
            url: getUrl(), 
            params: {
                parameters: {
                    drivers: notif.drivers,
                    cars: notif.carsid
                }
            },
            data: {
                situation: notif.situation,
                datetime: $filter('date')(new Date(), 'yyyy-MM-dd HH:mm:ss'),
                from: notif.from,
                trust: notif.trust,
               // to: 0,
                type: '2',
                geo: [notif.geo.latitude, notif.geo.longitude ]
            }
        }).then(function (response) {
            deffered.resolve(response);

        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise;
    };

    function sendNotification(notif) {
        console.log("upozornenie ", notif);
        var deffered = $q.defer();
        $http({
            method: 'POST',
            url: getUrl(), // + '??returnObject=true',
            params: {
                parameters: {
                    email: notif.userEmail,
                    drivers: notif.drivers,
                    cars: notif.carsid
                }
            },
            data: {
                situation: notif.situation,
                message: notif.message,
                datetime: $filter('date')(new Date(), 'yyyy-MM-dd HH:mm:ss'),
                from: notif.from,
                to: victimID,
                photo: notif.photo,
                type: '1',
                geo: !$filter('isEmpty')(notif.geo) ? [notif.geo.latitude, notif.geo.longitude] : [],
                trust: notif.trust,
                identity: notif.identity
            }
        }).then(function (response) {
            victimID = '';
            deffered.resolve(response);

        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise;
    }
    ;
    function getSendNotifications(from, page, size) {
        var deffered = $q.defer();
        $http({
            method: 'GET',
            url: getUrl(),
            params: {
                deep: true,
                pageSize: size,
                pageNumber: page,
                sort: [{
                        "fieldName": "datetime",
                        "order": "desc"
                    }],
                filter: JSON.stringify([{
                        "fieldName": "from",
                        "operator": "in",
                        "value": from
                    }])
            }
        }).then(function (response) {
            ;
            deffered.resolve(response);
        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise;
    }
    ;

    function getReceivedNotifications(to, page, size) {
        var deffered = $q.defer();
        $http({
            method: 'GET',
            url: getUrl(),
            params: {
                deep: true,
                pageSize: size,
                pageNumber: page,
                sort: [{
                        "fieldName": "datetime",
                        "order": "desc"
                    }],
                filter: JSON.stringify([{
                        "fieldName": "to",
                        "operator": "in",
                        "value": to
                    }, {
                       "fieldName": "seen",
                        "operator": "in",
                        "value": true 
                    }])
            }
        }).then(function (response) {
            deffered.resolve(response);
        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise;
    }
    ;

    function checkNewNotifications(carid){
        var deffered = $q.defer();
        $http({
            method: 'GET',
            url: getUrl(),
            params: {
                filter: JSON.stringify([{
                        "fieldName": "to",
                        "operator": "in",
                        "value": carid
                    }, {
                        "fieldName": "seen",
                        "operator": "equals",
                        "value": false
                    }])
            }
        }).then(function (response) {
            deffered.resolve(response);
        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise; 
    }

    function getNewNotifications(car) {
        var deffered = $q.defer();
        $http({
            method: 'GET',
            url: getUrl(),
            params: {
                deep: true,
                sort: [{
                        "fieldName": "datetime",
                        "order": "desc"
                    }],
                filter: JSON.stringify([{
                        "fieldName": "to",
                        "operator": "in",
                        "value": car
                    }, {
                        "fieldName": "seen",
                        "operator": "equals",
                        "value": false
                    }])
            }
        }).then(function (response) {
            deffered.resolve(response);
        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise;
    }
    ;

    function thankNotification(notifID) {
        var deffered = $q.defer();
        $http({
            method: 'PUT',
            url: getUrlForId(notifID),
            data: {
                thanked: true
            }
        }).then(function (response) {
            deffered.resolve(response);
        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise;
    }
    ;

    function rateNotification(notifID, rate) {
        var deffered = $q.defer();
        $http({
            method: 'PUT',
            url: getUrlForId(notifID),
            data: {
                rated: rate
            }
        }).then(function (response) {
            deffered.resolve(response);
        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise;
    }
    ;
    
    function seenNotification(notifID) {
        var deffered = $q.defer();
        $http({
            method: 'PUT',
            url: getUrlForId(notifID),
            data: {
                seen: true
            }
        }).then(function (response) {
            deffered.resolve(response);
        }, function (error) {
            deffered.reject(error);
        });
        return deffered.promise;
    }
    ;
    
    
    function getHistoryOfCurrentPosition(within,position) {
        var deffered = $q.defer();
        $http({
            method: 'GET',
            url: getUrl(),
            params: {
                deep: false,
                exclude: "metadata",
                filter: {
                    "q": {
                        "geo": {"$within": [[position.latitude, position.longitude], within]}
                    }
                }
            }
        }).then(function (res){
            deffered.resolve(res);
        }, function(err){
            deffered.reject(err);
        });
        return deffered.promise;
    };
    
    function lightFormatNotifications(d) {
        angular.forEach(d, function (value, key) {
            if (value.situation === "clamp")
                value.situation = "Papuča na kolese";
            if (value.situation === "towing")
                value.situation = "Odtiahnutie vozidla";
            if (value.situation === "accident")
                value.situation = "Nehoda";
            if (value.situation === "broken_lights")
                value.situation = "Pokazené svetlá";
            if (value.situation === "vagabonds")
                value.situation = "Nebezpečné osoby";
            if (value.situation === "parking_tickets")
                value.situation = "Parkovacie lístky";
        });
        return d;
    }
    ;
}
;

