// (C) Copyright 2020 Hewlett-Packard Enterprise Company, L.P.
define(['hp/services/REST'],
function(REST) {"use strict";

    var StartingService = ( function() {

        var STARTING_FAILURE_TIMEOUT = 900000; // 15 minutes - observed startup of minimal appliance takes 3m45s.
        var STARTING_PROGRESS_URL = '/rest/appliance/progress';
        var EXPECTED_ERROR = 'GENERIC_HTTP_503';

        /**
         * @class Service to retrieve the starting progress from the server.
         */
        function StartingService() {
            /**
             * Return the progress of the starting process or the error code if
             * the progress is not yet available.
             *
             * @return {errorCode:string} to the waiting handler for expected 503 (Service note available) errors.
             * @return {target:number, current:number} to the progress handler for current progress.
             * @return {errorCode:string} to the error handler for all other (unexpected) errors.
             */
            this.getProgress = function(presenterHandlers) {
                var modelHandlers = {
                    success : function(webAppStatus) {
                        var progress = {}; // Object to pass progress to Presenter and View
                        progress.current = webAppStatus.complete;
                        progress.target = webAppStatus.total;
                        presenterHandlers.progress(progress);
                    },
                    error : function(webAppStatus) {
                        if (EXPECTED_ERROR === webAppStatus.errorCode) {
                            presenterHandlers.waiting(webAppStatus);
                        } else {
                            presenterHandlers.error(webAppStatus);
                        }
                    }
                };
                REST.getURI(STARTING_PROGRESS_URL, modelHandlers, {timeout: STARTING_FAILURE_TIMEOUT, apiVersion: 100});
            };
        }
        return new StartingService();
    }());
    return StartingService;
});
