define('application/models/halo/servicemessage', [
'rofl/models/api/abstract',
'rofl/lib/utils',
'application/managers/session'
], function (
Abstract,
Utils,
SessionManager
) {
'use strict';
var endpoint = '/api/generalcontent/instance/search',
sessionManager = SessionManager.getInstance(),
canShow = function (date) {
var now = new Date(),
oneHour = 60 * 60 * 1000;
return now - date >= oneHour;
};
return Abstract.extend({
init: function init () {
this._enabled = false;
},
/**
* Resolves the endpoint.
*
* @param {Object} params - The endpoint params.
* @returns {string} - The endpoint.
*/
resolveEndpoint: function (params) {
return Utils.formatTemplate(endpoint, params);
},
/**
* Transform data from API to Application for a Create method.
*
* @param {*} data - The data to be transformed.
* @returns {Object} Transformed data.
*/
transformFromCreate: function (data) {
var values = Utils.getNested(data, 'items', '0', 'values');
if (!values) {
return this;
}
this._serviceTitle = Utils.getNested(values, 'alert_service_message_title_label_smarttv', 'nl-NL');
this._serviceText = Utils.getNested(values, 'alert_service_message_decsription_label_smarttv', 'nl-NL');
this._enabled = !!values['alert_service_message_enabled_smarttv'];
return this;
},
/**
* Returns true if the service message is enabled and can be shown.
*
* @returns {boolean} - True if the message can and should be shown.
*/
isEnabled: function () {
var lastShown = sessionManager.getLastServiceMessageShown() || false;
if (this._enabled && (!lastShown || canShow(lastShown))) {
sessionManager.setLastServiceMessageShown();
return true;
}
return false;
},
/**
* Returns the service title.
*
* @returns {string} - The service title.
*/
getServiceTitle: function () {
return this._serviceTitle;
},
/**
* Returns the service text.
*
* @returns {string} - The service text.
*/
getServiceText: function () {
return this._serviceText;
}
});
});