Source: widgets/detail/tabs/info.js

define('application/widgets/detail/tabs/info', [
    'rofl/widgets/container',
    'rofl/widgets/label',
    'rofl/lib/l10n',
    'rofl/lib/utils',
    'antie/runtimecontext',
    'application/widgets/detail/imdblabel'
], function (
    Container,
    Label,
    L10N,
    Utils,
    RuntimeContext,
    ImdbLabel
) {
    'use strict';

    var l10n = L10N.getInstance(),
        VIDEO_RATINGS = {
            G: 'icon-kijkwijzer-geweld-v2', // Geweld
            A: 'icon-kijkwijzer-eng-v2', // Fear
            S: 'icon-kijkwijzer-sex-v2', // Sex
            D: 'icon-kijkwijzer-pesten-v2', // Discrimination
            H: 'icon-kijkwijzer-drugs-v2', // Drugs or Alcohol use
            T: 'icon-kijkwijzer-taal-v2', // Violent language,
            'PC-6': 'icon-kijkwijzer-6-v2',
            'PC-12': 'icon-kijkwijzer-12-v2',
            'PC-16': 'icon-kijkwijzer-16-v2',
            'PC-99': 'icon-kijkwijzer-al-v2'
        },
        application = RuntimeContext.getCurrentApplication(),
        layout = application.getLayout(),
        descriptionLayout = layout.details.description,
        DESCRIPTION_LINE_LIMIT = 9;

    return Container.extend({

        /**
         * Initialises the widget.
         *
         * @param {Object} data - The data.
         */
        init: function init (data) {
            init.base.call(this);

            this.addClass('info-tab');

            this._build();

            if (data) {
                this.setDataItem(data);
            }
        },

        /**
         * Builds the widget.
         *
         * @private
         */
        _build: function () {
            var descriptionContainer = this._descriptionContainer = new Container(),
                description = this._description = new Label(''),
                list = this._list = new Container();

            description.setWidth(descriptionLayout.width);
            description.setMaximumLines(DESCRIPTION_LINE_LIMIT);
            description.setTruncationMode(Label.TRUNCATION_MODE_RIGHT_ELLIPSIS);

            descriptionContainer.appendChildWidget(description);
            descriptionContainer.addClass('description');
            description.addClass('info-tab-description');

            this._buildImdbRating();
            this._buildVideoRatings();

            list.addClass('people');
            list.appendChildWidget(this._buildPeopleList('actors'));
            list.appendChildWidget(this._buildPeopleList('directors'));
            this.appendChildWidget(descriptionContainer);
            this.appendChildWidget(list);
        },

        /**
         * Builds the people list.
         *
         * @param {string} type - The type.
         * @returns {Object} - The list.
         * @private
         */
        _buildPeopleList: function (type) {
            var container = new Container(),
                title = new Label(l10n.get(type)),
                list = new Container();

            title.addClass(['title', type]);
            container.addClass(type);
            list.addClass('people-list');

            container.appendChildWidget(title);
            container.appendChildWidget(list);

            this['_peopleList' + type] = list;

            return container;
        },

        /**
         * Builds the icons list.
         */
        _buildVideoRatings: function () {
            var videoRatings = this._videoRatings = new Container();

            videoRatings.addClass('video-ratings');

            this._descriptionContainer.appendChildWidget(videoRatings);
        },

        /**
         * Builds the rating scale. (IMDB).
         */
        _buildImdbRating: function () {
            var userRating = this._imdbRating = new Container();

            userRating.addClass('imdb-rating-container');

            this._descriptionContainer.appendChildWidget(userRating);
        },

        /**
         * Sets the data item.
         *
         * @param {Object} opts - The data item.
         */
        setDataItem: function (opts) {
            var list = this._videoRatings,
                imdbRatingContainer = this._imdbRating,
                imdbRating = opts.imdbRating;

            this._description.setText(opts.description);

            Utils.each(opts.people, function (data) {
                this._addPeople(data);
            }, this);


            Utils.each(opts.ratings, function (rating) {
                list.appendChildWidget(this._createVideoRating(rating));
            }, this);

            if (imdbRating) {
                imdbRatingContainer.appendChildWidget(new ImdbLabel({rating: imdbRating}));
            } else {
                imdbRatingContainer.removeChildWidgets();
            }
        },

        /**
         * Adds people to the widget.
         *
         * @param {Object} data - The data.
         * @private
         */
        _addPeople: function (data) {
            var list = this['_peopleList' + data.type];

            if (!data.people.length) {

                this._list.removeChildWidget(list.parentWidget);
            }

            Utils.each(data.people, function (person) {
                list.appendChildWidget(new Label(person));
            });
        },

        /**
         * Creates a video rating label.
         *
         * @param {string} rating - The rating.
         * @returns {Object} - The video rating label.
         * @private
         */
        _createVideoRating: function (rating) {
            return new Label({ text: '', classNames: [VIDEO_RATINGS[rating], 'video-rating'] });
        }
    });
});