(function () {
    global.dashModule
        .controller('dashboardsFiltersCtrl', ['$scope', 'dashboardsFiltersService', dashboardsFiltersCtrl])
        .directive('dashboardsFilters', [dashboardsFiltersDirective]);

    function dashboardsFiltersDirective() {
        return {
            restrict: 'E',
            templateUrl: 'dashboards/html/dashboardsFiltersTemplate'
        };
    }

    function dashboardsFiltersCtrl($scope, dashboardsFiltersService) {
        $scope.applyFilter = applyFilter;
        $scope.setFilterParams = setFilterParams;
        $scope.isFilterSet = isFilterSet;
        $scope.getFilterText = dashboardsFiltersService.getFilterText;
        $scope.clearFilter = clearFilter;
        $scope.filters = {};
        $scope.filterKey = '';

        function applyFilter(gadget){
            dashboardsFiltersService.updateFilter(gadget, $scope.filters);
        }

        function clearFilter(gadget){
            dashboardsFiltersService.clearFilter(gadget);
            $scope.filters = dashboardsFiltersService.getFilters(gadget);
        }

        function setFilterParams(gadget){
            $scope.filterKey = () => {
                return dashboardsFiltersService.getFilterActiveKey(gadget);
            };

            $scope.filters = dashboardsFiltersService.getFilters(gadget);
        }

        function isFilterSet(){
            return Object.keys($scope.filters).length > 0;
        }

    }
})();
