Source: models/uibuilder/hero.js

define('application/models/uibuilder/hero', [
    'rofl/models/api/abstract',
    'application/managers/api',
    'application/models/broadcast',
    'application/components/uibuilder',
    'rofl/lib/utils'
], function (
    Class,
    ApiManager,
    Broadcast,
    UIBuilder,
    Utils
) {
    'use strict';

    var api = ApiManager.getKPNAPI();

    return Class.extend({

        /**
         * Initialises the Hero model.
         *
         * @param {Object} data - The API data.
         */
        init: function (data) {
            this._title = data.metadata.label;
            this._apiEndpoint = '101/1.2.0/A/nld/smarttv/kpn' + data.retrieveItems.uri;
        },

        /**
         * Returns the type of widget this model to be used with.
         *
         * @returns {string} - The type of widget.
         */
        getWidgetType: function () {
            return UIBuilder.WIDGET_TYPES.HERO;
        },

        /**
         * Returns the title.
         *
         * @returns {string} - The title.
         */
        getTitle: function () {
            return this._title;
        },

        /**
         * Returns the api endpoint.
         *
         * @returns {string} - The api endpoint.
         */
        resolveEndpoint: function () {
            return this._apiEndpoint;
        },

        /**
         * Prepares the widget and loads all required data.
         *
         * @returns {Promise} - Promise resolving with the carousel model.
         */
        prepare: function () {
            return api.read(this, {
                    withCredentials: true
                })
                .then(Utils.bind(function (response) {
                    if (response.resultObj.containers.length > 0) {
                        this._item = new Broadcast(response.resultObj.containers[0]);

                        if (this._item.getContentType() === 'VOD') {
                            return api.read('userdata', {
                                params: {
                                    data: this._item
                                },
                                withCredentials: true
                            })
                                .then(Utils.bind(this._onUserDataResponse, this));
                        }

                        return this;
                    }

                    return this;
                }, this));
        },

        /**
         * User Data Response.
         *
         * @param {Object} userDataResponse - The user data response.
         *
         * @returns {Object} - The hero widget.
         * @private
         */
        _onUserDataResponse: function (userDataResponse) {
            if (userDataResponse && userDataResponse.resultObj.containers.length && this._item) {
                this._processUserData(userDataResponse);

                // Set the rights for the hero content.
                if (this._rights) {
                    this._item.setRights(this._rights);
                }
            }

            return this;
        },

        /**
         * Processes user data.
         *
         * @param {Object} data - User data.
         * @private
         */
        _processUserData: function (data) {
            var containers = Utils.getNested(data, 'resultObj', 'containers'),
                assets,
                asset,
                i;

            if (!containers.length) {
                return;
            }

            assets = Utils.getNested(containers[0], 'entitlement', 'assets');

            if (!assets.length) {
                return;
            }

            for (i = 0; i < assets.length; i++) {
                if (assets[i].assetType === 'MASTER') {
                    asset = assets[i];
                    break;
                }
            }

            this._rights = asset ? Utils.getNested(asset, 'rights') : null;
        },

        /**
         * Returns the item.
         *
         * @returns {Object} - The actual hero item.
         */
        getItem: function () {
            return this._item;
        },

        /**
         * Returns true if the user has rights.
         *
         * @returns {boolean} - Returns true if the item has rights to be watched.
         */
        hasRights: function () {
            if (!this._rights) {
                return true;
            }

            return this._rights && (this._rights !== 'buy' && this._rights !== 'none');
        },

        /**
         * Builds and returns the image url from the item data given its width and height.
         *
         * @param {Object} layout - The slider item's layout.
         * @returns {string} - The image URL.
         */
        getSliderImageUrl: function (layout) {
            var heroItem = this._item,
                imageUrl = '';

            if (layout && heroItem) {
                imageUrl = heroItem.getImage('manual', {
                    width: Math.round(layout.width),
                    height: Math.round(layout.height)
                });
            }

            return imageUrl;
        },

        /**
         * Returns the program's title.
         *
         * @returns {string} - The program's title.
         */
        getSliderTitle: function () {
            var heroItem = this._item,
                title = '';

            if (heroItem) {
                title = heroItem.getTitle();
            }

            return title;
        },

        /**
         * Returns the program channels's logo.
         *
         * @returns {string} - The program's title.
         */
        getChannelLogo: function () {
            var heroItem = this._item;

            return heroItem && heroItem.getChannelLogo && heroItem.getChannelLogo();
        },

        /**
         * Returns the program's title.
         *
         * @returns {string} - The program's title.
         */
        getSliderSubtitle: function () {
            return this._title;
        },

        /**
         * Checks if the slider item is locked.
         *
         * @returns {boolean} - True if slider item content is locked.
         */
        isLocked: function () {
            var contentItem = this.getItem();

            return contentItem && contentItem.isLocked();
        }
    });
});