Source: widgets/detail/detailslist.js

define('application/widgets/detail/detailslist', [
    'rofl/widgets/verticallist',
    'rofl/widgets/horizontallist',
    'rofl/widgets/label',
    'rofl/widgets/container',
    'rofl/lib/l10n',
    'application/widgets/button',
    'application/utils',
    'application/widgets/detail/iconbutton',
    'application/widgets/detail/episodeitem',
    'rofl/lib/utils',
    'antie/runtimecontext',
    'application/managers/feature'
], function (
    VerticalList,
    HorizontalList,
    Label,
    Container,
    L10N,
    KPNButton,
    AppUtils,
    IconButton,
    EpisodeItem,
    Utils,
    RuntimeContext,
    FeatureManager
) {
    'use strict';

    var l10n = L10N.getInstance(),
        application = RuntimeContext.getCurrentApplication(),
        featureManager = FeatureManager.getInstance();

    return VerticalList.extend({

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

            this.addClass('details-page-list');
            this._build(data);
        },

        /**
         * Builds elements.
         *
         * @param {Object} data - The detail data.
         */
        _build: function (data) {
            this._buildEpisodesList(data);

            // TODO: Renable once data is available.
            // this._buildCastLabel(data.getCastLabel() || '');
            // this._buildCastNames(data.getCast() || []);
            // this._buildSeasonsList(data, buttons);
        },

        /**
         * Builds presenter label element.
         *
         * @param {string} text - Cast label.
         */
        _buildCastLabel: function (text) {
            var castlabel = this._castlabel = new Label({ text: text, classNames: ['cast-label'] });

            this.appendChildWidget(castlabel);
        },

        /**
         * Builds presenter name element.
         *
         * @param {string} names - Presenter name.
         */
        _buildCastNames: function (names) {
            var text = names.join(', '),
                castnames = new Label({ text: text, classNames: ['cast-names'] });

            this.appendChildWidget(castnames);
        },

        /**
         * Builds element.
         *
         * @param {Array} episodes - The episodes to append.
         */
        _buildEpisodesList: function (episodes) {
            var now = application.getDate(),
                episodesList = this._episodeslist = new VerticalList(''),
                replayEnabled = featureManager.isReplayEnabled(),
                restricted,
                listelement,
                start,
                end,
                isLive,
                isCatchup,
                inactive;

            episodesList.addClass('episodes-list');

            Utils.each(episodes, function (data) {
                start = new Date(data.getStartTime() * 1000);
                end = new Date(data.getEndTime() * 1000);
                restricted = false;
                inactive = true;
                isCatchup = false;
                isLive = false;

                if (start > now) {
                    isCatchup = false;
                } else {
                    inactive = false;

                    if (replayEnabled) {

                        if (data.canCatchup() && data.canPlay()) {
                            isCatchup = true;
                        } else {
                            restricted = true;
                        }
                    }
                }

                if (start <= now && now <= end) {
                    isLive = true;
                    isCatchup = false;
                }

                listelement = new EpisodeItem('', {
                    restricted: restricted,
                    live: isLive,
                    catchup: isCatchup,
                    data: data
                });

                if (inactive) {
                    listelement.addClass('unactive');
                }

                episodesList.appendChildWidget(listelement);
                this.appendChildWidget(episodesList);
            }, this);
        },

        /**
         * Builds element.
         *
         * @param {Object} mockdata - Mocked data.
         */
        _buildSeasonsList: function (mockdata) {
            var seasonsList = this._seasonslist = new HorizontalList(),
                seasons = mockdata.seasons,
                seasonNumber = 0,
                seasonitem;

            seasonsList.addClass('seasons-list');


            Utils.each(seasons, function (seasonsData) {
                seasonNumber += 1;

                // build season item
                seasonitem = new KPNButton(l10n.get('details.season') + ' ' + seasonNumber, 'season' + seasonNumber);
                seasonitem.addClass('season-title');
                seasonsList.appendChildWidget(seasonitem);

                // get episodes list
                this._episodes = seasonsData.episodes;
            }, this);

            this.appendChildWidget(seasonsList);
        },

        /**
         * Returns episodes.
         *
         * @returns {Object} Episodes.
         */
        getEpisodeList: function () {
            return this._episodeslist;
        },


        /**
         * Dispose the widgets.
         */
        dispose: function () {

            Utils.each(this._episodeslist.getChildWidgets(), function (widget) {
                widget.dispose();
            });
        }
    });
});