(function () {
    global.dashModule.service('dashboardsFiltersService', ['$', dashboardsFiltersService]);

    function dashboardsFiltersService($) {
        const gadets = {};
        const sortIcons = {
            NAN : 'glyphicon-sort',
            ASC : 'glyphicon-sort-by-attributes',
            DESC : 'glyphicon-sort-by-attributes-alt'
        };

        return {
            getFilters,
            getFilterActiveKey,
            showFilter,
            isFilterVisible,
            isFilterSet,
            updateFilter,
            getFilterText,
            clearFilter,
            registerOnReloadData,
            reloadData,
            sortBy,
            getSortIcon
        };

        function isFilterSet(gadget){
            return gadets[gadget] && gadets[gadget].filters && Object.keys(gadets[gadget].filters).length !== 0;
        }

        function getFilterActiveKey(gadget) {
            return gadets[gadget].filterActiveKey;
        }

        function getFilters(gadget) {
            return gadets[gadget] && gadets[gadget].filters ? gadets[gadget].filters : {};
        }

        function getFilterText(gadget) {
            let fiterText = '';

            if (gadets[gadget] && gadets[gadget].filters) {
                $.each(gadets[gadget].filters, (key, value) => {
                    fiterText += key + ' is ' + value + ' and ';
                });
            }
            fiterText = fiterText.replace(/and\s$/, '');

            return fiterText;
        }

        function registerOnReloadData(gadget, callbackFunction) {
            if (!gadets[gadget]) {
                gadets[gadget] = {};
            }
            gadets[gadget].onReloadData = callbackFunction;
        }

        function reloadData(gadget) {
            gadets[gadget].onReloadData(gadets[gadget].filters, gadets[gadget].sortBy);
        }

        function showFilter(gadget, key) {
            if (!gadets[gadget]) {
                gadets[gadget] = {};
            }

            if (gadets[gadget].filterVisible && gadets[gadget].filterActiveKey !== key) {
                gadets[gadget].filterActiveKey = key;
                return;
            }

            gadets[gadget].filterActiveKey = key;
            gadets[gadget].filterVisible = !gadets[gadget].filterVisible;
        }

        function isFilterVisible(gadget) {
            return gadets[gadget] && gadets[gadget].filterVisible ? gadets[gadget].filterVisible : false;
        }

        function updateFilter(gadget, filters) {
            gadets[gadget].filters = filters;
            reloadData(gadget);
        }

        function clearFilter(gadget) {
            if (gadets[gadget] && gadets[gadget].filters) {
                gadets[gadget].filters = {};
            }

            reloadData(gadget);
        }

        function sortBy(gadget, key) {
            let direction;
            if (!gadets[gadget]) {
                gadets[gadget] = {};
            }

            direction = gadets[gadget].sortBy && gadets[gadget].sortBy.direction === 'ASC' ? 'DESC' : 'ASC';

            gadets[gadget].sortBy = {
                key,
                direction
            };
            reloadData(gadget);
        }

        function getSortIcon(gadget, sortKey){
            if(gadets[gadget] && gadets[gadget].sortBy && gadets[gadget].sortBy.key === sortKey){
                return sortIcons[gadets[gadget].sortBy.direction];
            }

            return sortIcons.NAN;
        }
    }
})();
