/* global angular  */
'use strict';
/**
 * @ngdoc service
 * @name tagResourceApp.tag
 * @description
 * # tag
 * Service in the tagResourceApp.
 */
angular.module('app')
.service('Tag', function($q, $log, $http, HostFactory, Model) {


    function Tag(opts) {
        opts = opts || {};
        Model.call(this, opts);
    }

    Tag.prototype = new Model();

    Tag.prototype.constructor = Tag;

    Tag.prototype.getAllTags = function(query) {
        var rawQuery;
        var rawOptions;

        rawQuery = query || {};
        rawOptions = {
            sort: {
                name: 1
            }
        };
        rawOptions = JSON.stringify(rawOptions);
        rawOptions = encodeURIComponent(rawOptions);

        return this.runRetrieve({
            query: rawQuery,
            options: rawOptions
        });

    };
    Tag.prototype.getTagsPage = function(query, page) {
        var rawQuery, rawOptions;

        rawQuery = query || {};
        rawOptions = {
            sort: {
                name: 1
            },
            limit: 25,
            skip: page * 25
        };


        return this.runRetrieve({
            query: rawQuery,
            options: rawOptions
        });


    };
    Tag.prototype.getAllCategories = function(query) {
        var rawQuery;

        rawQuery = query || {};

        return this.runDistinct('category', rawQuery);


    };
    Tag.prototype.getAllDomains = function(query) {
        var rawQuery;

        rawQuery = query || {};

        return this.runDistinct('domain', rawQuery);
    };

    Tag.prototype.removeTag = function(tag) {
        return this.runDelete({
            id: tag._id
        });
    };

    Tag.prototype.update = function(tag) {

        return this.runUpdate({
            id: tag._id,
            update: tag
        });
    };
    Tag.prototype.save = function(tag) {
        return this.runCreate({
            object: tag
        });
    };
    Tag.prototype.updateOrCreate = function(tag) {
        var query = {
            domain: tag.domain,
            languages: {
                '$in': [tag.language]
            },
            name: tag.name,
            '$and': [{
                type: {
                    '$ne': 'company-generated'
                }
            }, {
                type: {
                    '$ne': 'ana-generated'
                }
            }]
        };
        var update = {
            '$set': {
                name: tag.name,
                type: 'user-generated',
                domain: tag.domain,
                syn: tag.syn,
                uid: tag.uid,
                category: tag.category
            },
            '+$push': {
                languages: tag.language
            }
        };
        var options = {
            upsert: true
        };

        return this.runUpdate({
            id: tag._id,
            query: query,
            update: update,
            options: options
        });
    };

    Tag.prototype.getAllUidsByDomainName = function(domainName) {
        var rawQuery;
        rawQuery = {
            domain: domainName
        };

        return this.runDistinct('uid', rawQuery);
    };

    return new Tag({
        scheme: HostFactory.getScheme(),
        host: HostFactory.getHost(),
        dbName: HostFactory.getDbUrlComponent(),
        collectionName: 'tags'
    });

});