(function () {
    'use strict';

    angular
        .module('editorRotas')
        .factory('ItemFiltro', ItemFiltro);

    ItemFiltro.$inject = [];

    /* @ngInject */
    function ItemFiltro() {

        function ItemFiltro() {
            this.name;
            this.activated;
            this.selected;
            this.order;
            this.htmlNode;
            this.template;
            this.properties = {};
        }

        ItemFiltro.prototype.addItem = function (itemPesquisa) {
            var self = this;

            self.properties.Pesquisa.push(itemPesquisa);
        };

        ItemFiltro.prototype.removeItem = function (itemPesquisa) {
            var self = this;

            self.properties.Pesquisa = self.properties.Pesquisa.filter(function (item) {
                return item !== itemPesquisa;
            });
        };

        ItemFiltro.prototype.clear = function () {
            var self = this;
            self.properties.Pesquisa = [];
        };

        ItemFiltro.prototype.getPrevious = function (list) {
            var self = this;

            return list.itemsFiltro.filter(function (item) {
                if (item.order == self.order - 1) {
                    return item;
                }
            })[0];
        };

        ItemFiltro.prototype.getNext = function (list) {
            var self = this;

            return list.itemsFiltro.filter(function (item) {
                if (item.order == self.order + 1) {
                    return item;
                }
            })[0];
        };

        ItemFiltro.prototype.isValid = function () {
            var self = this;

            if (self.properties.Template === 'template-data-multi-options' && self.properties.Tipo === 2) {
                return self.properties.Pesquisa.length >= 1;
            }

            return self.properties.Pesquisa.length > 0;
        };

        return ItemFiltro;
    }

})();

