(function () {
    'use strict';

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

    ItemFiltro.$inject = [];

    /* @ngInject */
    function ItemFiltro() {

        /* jshint shadow:true */
        function ItemFiltro() {
            this.name = null;
            this.activated = null;
            this.selected = null;
            this.order = null;
            this.htmlNode = null;
            this.template = null;
            this.properties = {};
        }

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

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

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

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

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

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

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

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

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

        ItemFiltro.prototype.isValid = function () {
            let 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;
    }

})();

