/*************************************************
 * Copyright (c) 2016 Ansible, Inc.
 *
 * All Rights Reserved
 *************************************************/

/**
 * @ngdoc function
 * @name controllers.function:Home
 * @description This controller's for the dashboard
*/


/**
 * @ngdoc method
 * @name controllers.function:Home#Home
 * @methodOf controllers.function:Home
 * @description this function loads all the widgets on the dashboard.
 *  dashboardReady (emit) - this is called when the preliminary parts of the dashboard have been loaded, and loads each of the widgets. Note that the
 *                  Host count graph should only be loaded if the user is a super user
 *
*/

export function Home($scope, $compile, $stateParams, $rootScope, $location, $log, Wait,
    ClearScope, Rest, GetBasePath, ProcessErrors, $window, graphData){

    ClearScope('home');

    var dataCount = 0;

    $scope.$on('ws-jobs', function () {
        Rest.setUrl(GetBasePath('dashboard'));
        Rest.get()
        .success(function (data) {
            $scope.dashboardData = data;
        })
        .error(function (data, status) {
            ProcessErrors($scope, data, status, null, { hdr: 'Error!', msg: 'Failed to get dashboard host graph data: ' + status });
        });

        Rest.setUrl(GetBasePath("jobs") + "?order_by=-finished&page_size=5&finished__isnull=false");
        Rest.get()
        .success(function (data) {
            $scope.dashboardJobsListData = data.results;
        })
        .error(function (data, status) {
            ProcessErrors($scope, data, status, null, { hdr: 'Error!', msg: 'Failed to get dashboard jobs list: ' + status });
        });

        Rest.setUrl(GetBasePath("unified_job_templates") + "?order_by=-last_job_run&page_size=5&last_job_run__isnull=false&type=workflow_job_template,job_template");
        Rest.get()
        .success(function (data) {
            $scope.dashboardJobTemplatesListData = data.results;
        })
        .error(function (data, status) {
            ProcessErrors($scope, data, status, null, { hdr: 'Error!', msg: 'Failed to get dashboard jobs list: ' + status });
        });

    });

    if ($scope.removeDashboardDataLoadComplete) {
        $scope.removeDashboardDataLoadComplete();
    }
    $scope.removeDashboardDataLoadComplete = $scope.$on('dashboardDataLoadComplete', function () {
        dataCount++;
        if (dataCount === 3) {
            Wait("stop");
            dataCount = 0;
        }
    });

    if ($scope.removeDashboardReady) {
        $scope.removeDashboardReady();
    }
    $scope.removeDashboardReady = $scope.$on('dashboardReady', function (e, data) {
        $scope.dashboardCountsData = data;
        $scope.graphData = graphData;
        $scope.$emit('dashboardDataLoadComplete');

        var cleanupJobListener =
            $rootScope.$on('DataReceived:JobStatusGraph', function(e, data) {
                $scope.graphData.jobStatus = data;
            });

        $scope.$on('$destroy', function() {
            cleanupJobListener();
        });
    });

    if ($scope.removeDashboardJobsListReady) {
        $scope.removeDashboardJobsListReady();
    }
    $scope.removeDashboardJobsListReady = $scope.$on('dashboardJobsListReady', function (e, data) {
        $scope.dashboardJobsListData = data;
        $scope.$emit('dashboardDataLoadComplete');
    });

    if ($scope.removeDashboardJobTemplatesListReady) {
        $scope.removeDashboardJobTemplatesListReady();
    }
    $scope.removeDashboardJobTemplatesListReady = $scope.$on('dashboardJobTemplatesListReady', function (e, data) {
        $scope.dashboardJobTemplatesListData = data;
        $scope.$emit('dashboardDataLoadComplete');
    });

    $scope.refresh = function () {
        Wait('start');
        Rest.setUrl(GetBasePath('dashboard'));
        Rest.get()
        .success(function (data) {
            $scope.dashboardData = data;
            $scope.$emit('dashboardReady', data);
        })
        .error(function (data, status) {
            ProcessErrors($scope, data, status, null, { hdr: 'Error!', msg: 'Failed to get dashboard: ' + status });
        });
        Rest.setUrl(GetBasePath("jobs") + "?order_by=-finished&page_size=5&finished__isnull=false");
        Rest.get()
        .success(function (data) {
            data = data.results;
            $scope.$emit('dashboardJobsListReady', data);
        })
        .error(function (data, status) {
            ProcessErrors($scope, data, status, null, { hdr: 'Error!', msg: 'Failed to get dashboard jobs list: ' + status });
        });
        Rest.setUrl(GetBasePath("unified_job_templates") + "?order_by=-last_job_run&page_size=5&last_job_run__isnull=false&type=workflow_job_template,job_template");
        Rest.get()
        .success(function (data) {
            data = data.results;
            $scope.$emit('dashboardJobTemplatesListReady', data);
        })
        .error(function (data, status) {
            ProcessErrors($scope, data, status, null, { hdr: 'Error!', msg: 'Failed to get dashboard job templates list: ' + status });
        });
    };

    $scope.refresh();
}

Home.$inject = ['$scope', '$compile', '$stateParams', '$rootScope', '$location', '$log','Wait',
    'ClearScope', 'Rest', 'GetBasePath', 'ProcessErrors', '$window', 'graphData'
];

@@ -1297,39 +1297,36 @@
 .setUrl(
-GetBasePath(%22jobs%22) + %22
+%22api/v1/unified_jobs
 ?order_b
@@ -1363,32 +1363,54 @@
 ed__isnull=false
+&type=workflow_job,job
 %22);%0A        Rest
@@ -4199,31 +4199,28 @@
 Url(
-GetBasePath(%22jobs%22) + %22
+%22api/v1/unified_jobs
 ?ord
@@ -4269,16 +4269,38 @@
 ll=false
+&type=workflow_job,job
 %22);%0A    

include workflows on dashboard recent job runs list