(function() {
    var Ext = window.Ext4 || window.Ext;

    /**
     * Provides useful methods for manipulating server urls based on the current Rally execution environment.
     * It can be accessed via the global instance returned by `Rally.environment`
     *
     *     var currentServer = Rally.environment.getServer();
     *
     *     //Get the WSAPI url
     *     var wsapiUrl = currentServer.getWsapiUrl('v2.0);
     *
     */
    Ext.define('Rally.env.Server', {
        requires: [
            'Rally.data.notifications.Api'
        ],

        config: {
            /**
             * The protocol and domain to use.
             * @cfg {String}
             */
            baseUrl: '',

            /**
             * The context path to use.
             * @cfg {String}
             */
            contextPath: '',

            /**
             * The path to the resources, relative to the context path.
             * @cfg {String}
             */
            resourcesPath: '',

            /**
             * The path to the libraries, relative to the context path.
             * @cfg {String}
             */
            libraryPath: '',

            /**
             * The default wsapi version to use.
             * @cfg {String}
             */
            defaultWsapiVersion: 'v2.x',

            /**
             * The default lookback version to use.
             * @cfg {String}
             */
            defaultLookbackVersion: 'v2.0',

            /**
             * Applicable version aliases for WSAPI
             * @cfg {String[]}
             */
            wsapiVersionAliases: ['x', 'v2.x']
        },

        constructor: function(config) {
            config = config || {};
            this.mergeConfig(config);
        },

        /**
         * @private
         * Get the base url for constructing hash based urls
         * @return {String}
         */
        getHashBaseUrl: function() {
            return '';
        },

        /**
         * Get the current ALM web services url
         * @param {String} [version=this.getWsapiVersion()] The webservice version to use
         * @return {String} the current ALM web services url
         *
         * Example: /slm/webservice/x
         */
        getWsapiUrl: function(version) {
            return this.getContextUrl() + "/webservice/" + this.getWsapiVersion(version);
        },

        /**
         * Get the current ALM web services url
         * @param {String} [version=this.getWsapiVersion()] The webservice version to use
         * @return {String} the current ALM web services url
         *
         * Example: /slm/webservice/x
         */
        getRelativeWsapiUrl: function(version) {
            return this.contextPath + "/webservice/" + this.getWsapiVersion(version);
        },

        /**
         * Get the version of wsapi, using either the passed in value or the default value
         * @param {String} [version=this.getWsapiVersion()] The webservice version to use
         * @return {String} The version of wsapi, using either the passed in value or the default value
         */
        getWsapiVersion: function(version) {
            return (version || this.defaultWsapiVersion);
        },

        /**
         * Get the extension to use for WSAPI REST calls
         * @param {String} [version=this.getWsapiVersion()] The webservice version to use
         * @return {String} The extension to use for WSAPI REST calls
         */
        getWsapiRestExtension: function(version) {
            if (this.getWsapiVersion(version).indexOf('v2.') === 0) {
                return '';
            }
            return '.js';
        },

        /**
         * Get the current Lookback API web services url
         * @param {String/Number} version The web service version to use.
         * @return {String} the current Lookback web services url
         *
         * Example: /analytics/v2.0
         */
        getLookbackUrl: function(version) {
            return this.getBaseUrl() + '/analytics/' + this.getLookbackVersion(version);
        },

        /**
         * Get the version of lookback, using either the passed in value or the default value
         * @param {String} [version=this.getLoockbackVersion()] The lookback version to use
         * @return {String} The version of lookback, using either the passed in value or the default value
         */
        getLookbackVersion: function(version) {
            return version || this.defaultLookbackVersion;
        },

        /**
         * Get the current Notifications API web services url
         * @return {String} the current Notifications web services url
         */
        getNotificationsUrl: function() {
            return "/notifications/api/v" + Rally.data.notifications.Api.version;
        },

        /**
         * Get the url to the RUI images dir
         * @return {String} the current RUI images base url
         */
        getImagesPath: function() {
            return this.getResourcesPath() + '/css/images';
        },

        /**
         * Get the path to the resources (root of css and images) directory
         * @returns {String}
         */
        getResourcesPath: function() {
            return this.resourcesPath;
        },

        /**
         * Get the path to the libraries (root of bundled libraries) directory
         * @returns {String}
         */
        getLibraryPath: function() {
            return this.libraryPath;
        },

        /**
         * Returns the protocol and domain.
         * Returns empty string for relative paths.
         * @return {String} protocol and domain.
         */
        getBaseUrl: function() {
            return this.baseUrl;
        },

        /**
         * Returns the contextUrl.
         * @return {String} contextUrl
         */
        getContextUrl: function() {
            return this.getBaseUrl() + this.contextPath;
        }
    });
})();