Source: managers/feature.js

define('application/managers/feature', [
    'rofl/class',
    'antie/runtimecontext',
    'rofl/lib/utils'
], function (
    Class,
    RuntimeContext,
    Utils
) {
    'use strict';

    var instance = false,
        Features = {
            RESTART: 'restart',
            LIVE_TRICKPLAY: 'liveTrickplay',
            TRICKPLAY: 'trickplay',
            REPLAY: 'replay',
            RECORDING: 'recording',
            BRANDING: 'branding',
            HOME_CHECK: 'homecheck',
            PARENTAL: 'parental',
            SUBTITLES: 'subtitles',
            LIVE_STREAM_TYPE: 'liveStreamType',
            RESTART_STREAM_TYPE: 'restartStreamType',
            VOD_STREAM_TYPE: 'vodStreamType',
            VOD_MOVIE_STREAM_TYPE: 'vodMovieStreamType',
            REPLAY_STREAM_TYPE: 'replayStreamType',
            RECORDING_STREAM_TYPE: 'recordingStreamType',
            MOVIES: 'movies',
            SERIES: 'series',
            SPORTS: 'sports',
            SURVEY: 'survey',
            HOME: 'home'
        },
        application,

        /*
         * feature config example
         * {
         *      defaultOption: true,
         *      philips: false
         * }
         * or
         * {
         *      defaultOption: true,
         *      philips: {
         *          'tv-2014': false
         *      }
         * }
         */
        featureConfig,
        device,
        brand,
        model,
        FeatureManager;

    FeatureManager = Class.extend({

        init: function () {
            application = RuntimeContext.getCurrentApplication();
            featureConfig = application.getConfiguration().featuremanager;
            device = application.getDevice();
            brand = device.getBrand();
            model = device.getModel();
        },

        /**
         * Checks whether the given feature is enabled or disabled in the config.
         * By default all features are enabled.
         *
         * @param {string} feature - Feature name.
         * @returns {boolean} Enabled.
         * @private
         */
        _checkFeature: function (feature) {
            var models = featureConfig[feature] && featureConfig[feature][brand],
                defaultOption = featureConfig[feature] && featureConfig[feature].defaultOption,
                isFeatureEnabled = defaultOption;

            // always return true when feature manager is disabled.
            if (featureConfig.disabled) {
                return true;
            }

            // check feature config whether the feature is enabled or disabled for brand/model
            if (!Utils.isUndefined(models) && Utils.isBool(models)) {

                // feature is enabled or disabled for all models
                isFeatureEnabled = models;

            } else if (!Utils.isUndefined(models) && Utils.isObject(models)) {

                // check based on model
                isFeatureEnabled = Utils.isUndefined(models[model]) ? defaultOption : models[model];
            }
            return isFeatureEnabled;
        },

        /**
         * Checks whether the given feature has a value in the config.
         * Returns the value per brand/model else the default option.
         *
         * @param {string} feature - Feature name.
         * @returns {Object} The value or default option.
         * @private
         */
        _getFeature: function (feature) {
            var models = featureConfig[feature] && featureConfig[feature][brand],
                defaultOption = featureConfig[feature] && featureConfig[feature].defaultOption,
                featureValue;

            if (!defaultOption) {
                throw Error('You must define a default option for feature ' + feature);
            }

            // always return default option when feature manager is disabled.
            if (featureConfig.disabled) {
                return defaultOption;
            }

            // check feature config whether the feature has a value for brand/model
            if (Utils.isString(models)) {

                return models;
            } else if (!Utils.isUndefined(models) && Utils.isObject(models)) {

                // check based on model
                featureValue = Utils.isUndefined(models[model]) ? defaultOption : models[model];
            } else {
                return defaultOption;
            }
            return featureValue;
        },

        /**
         * Gives a report about which features are supported & unsupported for this device.
         *
         * @returns {{supported: Array, unsupported: Array}} Returns a report of supported &
         *                                                   unsupported features.
         */
        getFeatureSupport: function () {
            var report = {
                supported: [],
                unsupported: []
            };

            Utils.each(featureConfig, function (value, key) {
                var isEnabled = this._checkFeature(key);

                report[isEnabled ? 'supported' : 'unsupported'].push(key);
            }, this);

            return report;
        },

        /**
         * Checks whether replay streams are allowed/enabled.
         *
         * @returns {boolean} Enabled.
         */
        isReplayEnabled: function () {
            return this._checkFeature(Features.REPLAY);
        },

        /**
         * Checks whether restart streams are allowed/enabled.
         *
         * @returns {boolean} Enabled.
         */
        isRestartEnabled: function () {
            return this._checkFeature(Features.RESTART);
        },

        /**
         * Checks whether recordings are allowed/enabled.
         *
         * @returns {boolean} Enabled.
         */
        isRecordingEnabled: function () {
            return this._checkFeature(Features.RECORDING);
        },

        /**
         * Checks whether trickplay in live streams is allowed/enabled.
         *
         * @returns {boolean} Enabled.
         */
        isLiveTrickplayEnabled: function () {
            return this._checkFeature(Features.LIVE_TRICKPLAY);
        },

        /**
         * Checks whether trickplay in streams is allowed/enabled.
         *
         * @returns {boolean} Enabled.
         */
        isTrickplayEnabled: function () {
            return this._checkFeature(Features.TRICKPLAY);
        },

        /**
         * Returns true if the branding is enabled.
         *
         * @returns {boolean} Enabled.
         */
        isBrandingEnabled: function () {
            return this._checkFeature(Features.BRANDING);
        },

        /**
         * Returns true if out of home is enabled.
         *
         * @returns {boolean} - True if out of home is enabled.
         */
        isOutOfHomeEnabled: function () {
            return this._checkFeature(Features.HOME_CHECK);
        },

        /**
         * Returns true if parental is enabled.
         *
         * @returns {boolean} - True if parental is enabled.
         */
        isParentalEnabled: function () {
            return this._checkFeature(Features.PARENTAL);
        },

        /**
         * Returns true if subtitles is enabled.
         *
         * @returns {boolean} - True if subtitles is enabled.
         */
        isSubtitlesEnabled: function () {
            return this._checkFeature(Features.SUBTITLES);
        },

        /**
         * Returns true if the movies feature is enabled.
         *
         * @returns {boolean} - True if movies is enabled.
         */
        isMoviesEnabled: function () {
            return this._checkFeature(Features.MOVIES);
        },

        /**
         * Returns true if the series feature is enabled.
         *
         * @returns {boolean} - True if series is enabled.
         */
        isSeriesEnabled: function () {
            return this._checkFeature(Features.SERIES);
        },

        /**
         * Returns true if the sports feature is enabled.
         *
         * @returns {boolean} - True if sports is enabled.
         */
        isSportsEnabled: function () {
            return this._checkFeature(Features.SPORTS);
        },

        /** Returns true if the survey feature is enabled.
         *
         * @returns {boolean} - True if survey is enabled.
         */
        isSurveyEnabled: function () {
            return this._checkFeature(Features.SURVEY);
        },

        /**
         * Returns true if the home feature is enabled.
         *
         * @returns {boolean} - True if home is enabled.
         */
        isHomeEnabled: function () {
            return this._checkFeature(Features.HOME);
        },

        /**
         * Returns the stream type.
         *
         * @param {string} videoType - The video type.
         * @returns {Object} - The streamtype.
         */
        getStreamType: function (videoType) {
            var streamType;

            switch (videoType.toLowerCase()) {
                case 'live':
                    streamType = this._getFeature(Features.LIVE_STREAM_TYPE);
                    break;
                case 'restart':
                    streamType = this._getFeature(Features.RESTART_STREAM_TYPE);
                    break;
                case 'vod':
                    streamType = this._getFeature(Features.VOD_STREAM_TYPE);
                    break;
                case 'vod_movie':
                    streamType = this._getFeature(Features.VOD_MOVIE_STREAM_TYPE);
                    break;
                case 'replay':
                    streamType = this._getFeature(Features.REPLAY_STREAM_TYPE);
                    break;
                case 'recording':
                    streamType = this._getFeature(Features.RECORDING_STREAM_TYPE);
                    break;
            }

            return streamType;
        }
    });

    return {
        getInstance: function () {
            if (!instance) {
                instance = new FeatureManager();
            }
            return instance;
        },
        Features: Features
    };
});