(function ($, document, window) {
function resetTuner(page, id) {
var message = Globalize.translate('MessageConfirmResetTuner');
Dashboard.confirm(message, Globalize.translate('HeaderResetTuner'), function (confirmResult) {
if (confirmResult) {
Dashboard.showLoadingMsg();
ApiClient.resetLiveTvTuner(id).done(function () {
Dashboard.hideLoadingMsg();
reload(page);
});
}
});
}
function renderTuners(page, tuners) {
var html = '';
for (var i = 0, length = tuners.length; i < length; i++) {
var tuner = tuners[i];
html += '
';
if (tuner.ProgramName) {
html += tuner.ProgramName;
}
html += '
';
html += '
';
html += tuner.Clients.join(' ');
html += '
';
html += '
';
html += '';
html += '
';
html += '
';
}
var elem = $('.tunersResultBody', page).html(html).parents('.tblTuners').table("refresh").trigger('create');
$('.btnResetTuner', elem).on('click', function () {
var id = this.getAttribute('data-tunerid');
resetTuner(page, id);
});
}
function getServiceHtml(service) {
var html = '';
html += '
';
var serviceUrl = service.HomePageUrl || '#';
html += '
';
var status = service.Status;
if (service.Status == 'Ok') {
status = '' + status + '';
} else {
if (service.StatusMessage) {
status += ' (' + service.StatusMessage + ')';
}
status = '' + status + '';
}
html += '
';
return html;
}
function loadPage(page, liveTvInfo) {
if (liveTvInfo.IsEnabled) {
$('.liveTvStatusContent', page).show();
} else {
$('.liveTvStatusContent', page).hide();
}
var servicesToDisplay = liveTvInfo.Services.filter(function (s) {
return s.IsVisible;
});
if (servicesToDisplay.length) {
$('.servicesSection', page).show();
} else {
$('.servicesSection', page).hide();
}
$('.servicesList', page).html(servicesToDisplay.map(getServiceHtml).join('')).trigger('create');
var tuners = [];
for (var i = 0, length = liveTvInfo.Services.length; i < length; i++) {
for (var j = 0, numTuners = liveTvInfo.Services[i].Tuners.length; j < numTuners; j++) {
tuners.push(liveTvInfo.Services[i].Tuners[j]);
}
}
renderTuners(page, tuners);
ApiClient.getNamedConfiguration("livetv").done(function (config) {
renderDevices(page, config.TunerHosts);
renderProviders(page, config.ListingProviders);
});
Dashboard.hideLoadingMsg();
}
function renderDevices(page, devices) {
var html = '';
if (devices.length) {
html += '
';
for (var i = 0, length = devices.length; i < length; i++) {
var device = devices[i];
var href = 'livetvtunerprovider-' + device.Type + '.html?id=' + device.Id;
html += '';
html += '';
html += '';
html += '';
html += '
';
html += '';
html += '';
html += '';
html += '';
}
html += '
';
}
var elem = $('.devicesList', page).html(html);
$('.btnDeleteDevice', elem).on('click', function () {
var id = this.getAttribute('data-id');
deleteDevice(page, id);
});
}
function deleteDevice(page, id) {
var message = Globalize.translate('MessageConfirmDeleteTunerDevice');
Dashboard.confirm(message, Globalize.translate('HeaderDeleteDevice'), function (confirmResult) {
if (confirmResult) {
Dashboard.showLoadingMsg();
ApiClient.ajax({
type: "DELETE",
url: ApiClient.getUrl('LiveTv/TunerHosts', {
Id: id
})
}).done(function () {
reload(page);
});
}
});
}
function reload(page) {
Dashboard.showLoadingMsg();
ApiClient.getLiveTvInfo().done(function (liveTvInfo) {
loadPage(page, liveTvInfo);
});
}
function submitAddDeviceForm(page) {
page.querySelector('.dlgAddDevice').close();
Dashboard.showLoadingMsg();
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl('LiveTv/TunerHosts'),
data: JSON.stringify({
Type: $('#selectTunerDeviceType', page).val(),
Url: $('#txtDevicePath', page).val()
}),
contentType: "application/json"
}).done(function () {
reload(page);
}).fail(function () {
Dashboard.alert({
message: Globalize.translate('ErrorAddingTunerDevice')
});
});
}
function renderProviders(page, providers) {
var html = '';
if (providers.length) {
html += '
';
for (var i = 0, length = providers.length; i < length; i++) {
var provider = providers[i];
html += '';
html += '';
html += '';
html += '';
html += '