Source: models/production/uibuilder/season.js

define('application/models/production/uibuilder/season', [
    'rofl/models/api/abstract',
    'application/managers/channel',
    'application/managers/api',
    'antie/runtimecontext',
    'rofl/lib/utils',
    'application/models/relatedcontent',
    'application/managers/session',
    'application/models/broadcast',
    'rofl/lib/promise'
], function (
    Abstract,
    ChannelManager,
    ApiManager,
    RuntimeContext,
    Utils,
    RelatedContent,
    SessionManager,
    Broadcast,
    Promise
) {
    'use strict';

    var imageFormat = RuntimeContext.getCurrentApplication().getLayout().imageFormat,
        landscapeFormat = imageFormat.landscape,
        imageEndpoint = ApiManager.getImageAPI('vod/') + '{{imageId}}/{{size}}.jpg',
        api = ApiManager.getKPNAPI(),
        Season;

    Season = Abstract.extend({

        /**
         * Initialises the search result model from API response object.
         *
         * @param {Object} response - API search result object.
         */
        init: function (response) {
            var metadata = response.metadata,
                additionalQueryParams;

            this._id = metadata.contentId;
            this._contentOptions = metadata.contentOptions;
            this._contentSubtype = metadata.contentSubtype;
            this._contentType = metadata._contentType;
            this._videoRatings = metadata.pcExtendedRatings;
            this._pcLevel = metadata.pcLevel;
            this._imageUrl = metadata.pictureUrl;
            this._title = metadata.title;
            this._year = metadata.year;
            this._seriesTitle = metadata.seriesTitle;
            this._season = metadata.season;
            this._items = [];

            additionalQueryParams = {
                filter_quality: 'fhd',
                orderBy: 'episodeNumber'

            };

            this._apiEndpoint = Utils.addToQueryStringOfURL('101/1.2.0/A/nld/smarttv/kpn' + response.retrieveItems.uri, additionalQueryParams);

            if (this._pcLevel === 99) {
                this._pcLevel = 0;
            }
        },

        /**
         * 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 () {
            var self = this;

            if (this._items.length) {
                return Promise.resolve(self);
            }

            return api.read(this, {
                withCredentials: true
            })
                .then(Utils.bind(function (response) {
                    var items = [],
                        containers = response.resultObj.containers;

                    Utils.each(containers, function (container) {
                        items.push(new Broadcast(container));
                    });

                    this._items = items;

                    return this;
                }, this));
        },

        /**
         * Returns the items.
         *
         * @returns {Array} - The carousel items to be appended.
         */
        getItems: function () {
            return this._items;
        },

        /**
         * Returns PC level.
         *
         * @returns {number} - PC level.
         */
        getPCLevel: function () {
            return this._pcLevel;
        },

        /**
         * Return id of the broadcast.
         *
         * @returns {string} - The id.
         */
        getId: function () {
            return this._id;
        },

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

        /**
         * Returns URL of preview image.
         *
         * @param {string} size - Size of the image.
         * @returns {string} - URL of preview image.
         */
        getImageUrl: function (size) {
            size = size || landscapeFormat.width + 'x' + landscapeFormat.height;

            return Utils.formatTemplate(imageEndpoint, {
                imageId: this._imageUrl,
                size: size
            });
        },

        /**
         * Returns the background.
         *
         * @param {string} orientation - Orientation.
         * @param {Object} [dimensions] - The dimensions. Should contain width and height.
         * @returns {string} - The background url.
         * @private
         */
        getImage: function (orientation, dimensions) {
            var format,
                size;

            orientation = orientation || 'landscape'; // Default to landscape image.

            if (orientation === 'manual') {
                size = dimensions.width + 'x' + dimensions.height + '.jpg';
            } else {
                format = imageFormat[orientation];
                size = format.width + 'x' + format.height + '.jpg';
            }

            return Utils.formatTemplate(imageEndpoint, {
                imageId: this._imageUrl,
                size: size
            });
        },

        /**
         * Returns the video ratings.
         *
         * @returns {Array} - The video ratings.
         */
        getVideoRatings: function () {
            return this._videoRatings;
        },

        /**
         * Returns the description.
         *
         * @returns {string} - The description.
         */
        getDescription: function () {
            return this._description;
        },

        /**
         * Returns the age rating.
         *
         * @returns {string} - The age rating.
         */
        getAgeRating: function () {
            return this._ageRating;
        },

        /**
         * Returns the genres.
         *
         * @returns {Array} - The genres.
         */
        getGenres: function () {
            return this._genres;
        },

        /**
         * Returns the content type.
         *
         * @returns {string} - The content type.
         */
        getContentType: function () {
            return this._contentType;
        },

        /**
         * Returns the year.
         *
         * @returns {string} - The year.
         */
        getYear: function () {
            return this._year;
        },

        /**
         * Returns true if the broadcast is locked.
         *
         * @returns {boolean} - True if the broadcast is locked.
         */
        isLocked: function () {
            var parentalControlParams = SessionManager.getInstance().getUserPCParams(),
                parentalControlLevel = parseInt(Utils.getNested(parentalControlParams, 'parentalControlLevel')),
                parentalControlGenres = Utils.getNested(parentalControlParams, 'parentalGenres'),
                genres = this.getParentalGenres(),
                isGenreProtected,
                VODContentTypes = ['VOD', 'GROUP_OF_BUNDLES', 'BUNDLE'];

            if (VODContentTypes.indexOf(this.getContentType()) >= 0) {
                parentalControlLevel = parseInt(Utils.getNested(parentalControlParams, 'parentalVODControlLevel'));
            }

            isGenreProtected = function () {
                var i;

                if (parentalControlGenres.length) {
                    for (i = 0; i < parentalControlGenres.length; i++) {
                        if (genres.indexOf(parentalControlGenres[i]) > -1) {
                            return true;
                        }
                    }
                }
                return false;
            };

            return !(parentalControlLevel >= this.getPCLevel()) || isGenreProtected();
        },

        /**
         * Returns the season.
         *
         * @returns {string} - The season.
         */
        getSeason: function () {
            return this._season;
        }
    });

    return Season;
});