Source: models/channel.js

define('application/models/channel', [
    'antie/class',
    'rofl/lib/utils',
    'application/managers/api'
], function (
    Abstract,
    Utils,
    ApiManager
) {
    'use strict';

    var Channel,
        imageEndpoint = ApiManager.getImageAPI('logo/') + '{{externalId}}/{{size}}.png';

    Channel = Abstract.extend({

        /**
         * Initialises the channel model.
         *
         * @param {Object} data - Init data.
         */
        init: function (data) {
            var metadata = data.metadata;

            this._id = metadata.channelId;
            this._name = metadata.channelName;
            this._number = metadata.orderId;
            this._assetId = this._getMasterAssetId(data.assets);
            this._externalId = metadata.externalId;
        },

        /**
         * Gets the identifier.
         *
         * @returns {number|string|null} - The ID or null if not set.
         */
        getId: function () {
            return this._id;
        },

        /**
         * Gets the external identifier.
         *
         * @returns {number|string|null} - The ID or null if not set.
         */
        getExternalId: function () {
            return this._externalId;
        },

        /**
         * Gets the label.
         *
         * @returns {string|null} - The name or null if not set.
         */
        getName: function () {
            return this._name;
        },

        /**
         * Get master asset id.
         *
         * @returns {number|null} - The number or null if not set.
         */
        getAssetId: function () {
            return this._assetId;
        },

        /**
         * Gets the image.
         *
         * @param {string} [size] - The image size. Optional. Possible values: 128, 256, 384 and 512.
         * @returns {string} - The image url.
         */
        getImage: function (size) {
            size = size || '256';

            return Utils.formatTemplate(imageEndpoint, {
                size: size,
                externalId: this._externalId
            });
        },

        /**
         * Gets the master asset ID.
         *
         * @param {Array} [assets] - The assets available for the specific channel.
         * @returns {number} - The master asset ID.
         * @private
         */
        _getMasterAssetId: function (assets) {
            var assetId,
                i;

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

            return assetId;
        },

        /**
         * Returns the channel number.
         *
         * @returns {number|null} - The channel number or null if not set.
         */
        getNumber: function () {
            return this._number;
        }
    });

    return Channel;
});