Source: managers/api.js

define('application/managers/api', [
    'rofl/datasources/api',
    'antie/runtimecontext',
    'application/models/configuration',
    'rofl/application',
    'rofl/lib/promise'
], function (
    Api,
    RuntimeContext,
    KPNConfig,
    Application,
    Promise
) {
    'use strict';

    var instances = {},
        KPN_APIS = {
            STAGING: 'kpnstaging',
            BETA: 'kpnbeta',
            PROD: 'kpn'
        },
        HALO_API = {
            STAGING: 'halostaging',
            PROD: 'halo'
        },
        IMAGE_APIS = {
            BETA: 'https://images.prd.tv.kpn.com/',
            PROD: 'https://images.prd.tv.kpn.com/'
        },
        apiSetting = null,
        app = null,
        device = null;

    return {

        /**
         * Returns an Api instance.
         *
         * @param {string} key - Api config key.
         * @returns {Object} Api object.
         */
        getInstance: function (key) {
            if (key) {
                if (!instances[key]) {
                    instances[key] = new Api(key);
                }

                return instances[key];
            }

            throw new Error('You have to provide a key to retrieve and API');
        },

        /**
         * Returns the kpn api, for the given api setting.
         *
         * @returns {Object} - The endpoint based on the api setting.
         */
        getKPNAPI: function () {
            if (apiSetting === null) {
                apiSetting = RuntimeContext.getCurrentApplication().getAPISetting();
            }

            if (apiSetting === KPNConfig.APIS.BETA) {
                return this.getInstance(KPN_APIS.BETA);
            } else if (apiSetting === KPNConfig.APIS.STAGING) {
                return this.getInstance(KPN_APIS.STAGING);
            }

            return this.getInstance(KPN_APIS.PROD);
        },

        /**
         * Returns the Halo api, for the given api setting.
         *
         * @returns {Object} - The endpoint based on the api setting.
         */
        getHaloAPI: function () {
            if (apiSetting === null) {
                apiSetting = RuntimeContext.getCurrentApplication().getAPISetting();
            }

            if (apiSetting === KPNConfig.APIS.STAGING) {
                return this.getInstance(HALO_API.STAGING);
            }

            return this.getInstance(HALO_API.PROD);
        },

        /**
         * Returns the image api.
         *
         * @param {string} path - Path to append to base url. Possible values: 'vod','epg','logo'...
         * @returns {string} - The image api url.
         */
        getImageAPI: function (path) {
            path = path || '';

            if (apiSetting === KPNConfig.APIS.BETA) {
                return IMAGE_APIS.BETA + path;
            }

            return IMAGE_APIS.PROD + path;
        },

        /**
         * Returns version from server.
         *
         * @returns {Promise} Promise of new version.
         */
        getVersion: function () {

            if (!app) {
                app = RuntimeContext.getCurrentApplication();
            }

            if (!device) {
                device = app.getDevice();
            }

            return new Promise(function (fulfill, reject) {
                device.loadURL('src/json/version.json?v=' + app.getDate().getTime(), {
                    method: 'GET',
                    responseType: 'json',
                    onLoad: function (response) {
                        fulfill(response.version);
                    },
                    onError: function (err) {
                        reject(err);
                    }
                });
            });
        },

        /**
         * Returns API Error Codes.
         *
         * @returns {Object} - The API error codes.
         */
        getApiErrorCodes: function () {
            return KPNConfig.API_ERROR_CODES;
        }
    };

});