Source: models/production/uibuilder/series.js

define('application/models/production/uibuilder/series', [
    'antie/class',
    'application/managers/channel',
    'application/managers/api',
    'antie/runtimecontext',
    'rofl/lib/utils',
    'application/models/relatedcontent',
    'application/managers/session',
    'rofl/lib/l10n',
    'application/models/production/uibuilder/season'
], function (
    Abstract,
    ChannelManager,
    ApiManager,
    RuntimeContext,
    Utils,
    RelatedContent,
    SessionManager,
    L10N,
    Season
) {
    'use strict';

    var imageFormat = RuntimeContext.getCurrentApplication().getLayout().imageFormat,
        landscapeFormat = imageFormat.landscape,
        imageEndpoint = ApiManager.getImageAPI('vod/') + '{{imageId}}/{{size}}.jpg{{query}}',
        getDetailsAction = function (actions) {
            var i,
                j,
                detailsAction;

            for (i = 0, j = actions.length; i < j; i++) {

                if (actions[i].targetType === 'DETAILS_PAGE') {
                    detailsAction = actions[i].uri;
                    break;
                }
            }

            return detailsAction;
        },
        Series,
        SEASONS_ORDER = {
            ASC: 'asc',
            DESC: 'desc'
        };

    Series = 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;

            this._actors = metadata.actors;
            this._id = metadata.contentId;
            this._contentOptions = metadata.contentOptions;
            this._contentSubtype = metadata.contentSubtype;
            this._contentType = metadata._contentType;
            this._directors = metadata.directors;
            this._genres = metadata.genres;
            this._description = metadata.longDescription;
            this._videoRatings = metadata.pcExtendedRatings;
            this._pcLevel = metadata.pcLevel;
            this._imageUrl = metadata.pictureUrl;
            this._title = metadata.title;
            this._year = metadata.year;
            this._seasons = [];
            this._detailsAction = getDetailsAction(response.actions || []);
            this._parentalGenres = metadata.pcExtendedRatings.length ? metadata.pcExtendedRatings : [];
            this._imdbRating = metadata.imdbRating || null;

            Utils.each(response.containers, function (container) {
                this._seasons.push(new Season(container));
            }, this);

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

        /**
         * 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) {
            var query = '';

            size = size || landscapeFormat.width + 'x' + landscapeFormat.height;

            if (this.isLocked()) {
                query += '?blurred=true';
            }

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

        /**
         * 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 query = '',
                format,
                size;

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

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

            if (this.isLocked()) {
                query += '?blurred=true';
            }

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

        /**
         * 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 recording id.
         *
         * @returns {string} - The recording id.
         */
        getRecordingId: function () {
            return this._recordingId;
        },

        /**
         * Returns the start delta time.
         *
         * @returns {number} - The start delta time.
         */
        getStartDeltaTime: function () {
            return this._startDeltaTime;
        },

        /**
         * 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 the actors.
         *
         * @returns {Array} - The actors.
         */
        getActors: function () {

            if (this._directors && this._directors.length) {

                return this._actors.slice(0, 4);
            }

            return this._actors.slice(0, 5);
        },

        /**
         * Returns the directors.
         *
         * @returns {Array} - The directors.
         */
        getDirectors: function () {

            if (this._actors && this._actors.length) {

                if (this._actors.length <= 3) {
                    return this._directors.slice(0, 2);
                }

                return this._directors.slice(0, 1);
            }

            return this._directors.slice(0, 5);
        },

        /**
         * Returns the seasons.
         *
         * @returns {Array} - The seasons.
         */
        getSeasons: function () {
            return this._seasons;
        },

        /**
         * Orders the seasons in ascending or descending mode.
         *
         * @param {string} order - ASC or DESC order for the seasons.
         * @returns {Array} - The ordered seasons.
         */
        orderSeasons: function (order) {
            return this._seasons.sort(function (seasonItemA, seasonItemB) {
                if (order === SEASONS_ORDER.ASC) {
                    return seasonItemA._season - seasonItemB._season;
                }

                return seasonItemB._season - seasonItemA._season;
            });
        },

        /**
         * Returns the details action.
         *
         * @returns {string} - The details action url.
         */
        getDetailsAction: function () {
            return this._detailsAction;
        },

        getDuration: function () {
            return null;
        },

        /**
         * 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 parental genres.
         *
         * @returns {Array} - Parental genres.
         */
        getParentalGenres: function () {
            return this._parentalGenres;
        },

        /**
         * Returns the user rating scale of the content. (IMDB).
         *
         * @returns {number|null} - The user rating scale.
         */
        getImdbRating: function () {
            return this._imdbRating;
        }
    });

    return Series;
});