(function () {
    global.dashModule
        .controller('gadgetNextActionsCtrl', ['$scope', '$http', '$', 'utilsService', 'dashboardsFiltersService', gadgetNextActionsCtrl])
        .directive('gadgetNextActions', [gadgetNextActionsDirective]);

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

    function gadgetNextActionsCtrl($scope, $http, $, utilsService, dashboardsFiltersService) {
        $scope.filterService = dashboardsFiltersService;
        dashboardsFiltersService.registerOnReloadData($scope.gadget.module, getNextActionsInfo);
        $scope.actions = [];

        getNextActionsInfo();

        function getNextActionsInfo(filters, sortBy) {
            const params = $.param({
                filters: JSON.stringify(filters) || null,
                sortBy : JSON.stringify(sortBy) || null
            });

            return $http({
                method: 'POST',
                url: 'dashboards/api/getNextActionsInfo',
                data: params
            }).then(setGadgetInfo, utilsService.onHttpError);
        }

        function setGadgetInfo(response) {
            $scope.actions = (response.data && response.data.length) ? response.data : [];
        }
    }
})();
