define(["events","serverNotifications","connectionManager","globalize","emby-button"],function(events,serverNotifications,connectionManager,globalize){"use strict";return function(options){var pollInterval,button=options.button,serverId=ApiClient.serverId();function pollTasks(){connectionManager.getApiClient(serverId).getScheduledTasks({IsEnabled:!0}).then(updateTasks)}function updateTasks(tasks){var progress,lastResult,task=tasks.filter(function(t){return t.Key===options.taskKey})[0];options.panel&&(task?options.panel.classList.remove("hide"):options.panel.classList.add("hide")),task&&("Idle"===task.State?button.removeAttribute("disabled"):button.setAttribute("disabled","disabled"),button.setAttribute("data-taskid",task.Id),progress=(task.CurrentProgressPercentage||0).toFixed(1),options.progressElem&&(options.progressElem.value=progress,"Running"===task.State?options.progressElem.classList.remove("hide"):options.progressElem.classList.add("hide")),options.lastResultElem&&("Failed"===(lastResult=task.LastExecutionResult?task.LastExecutionResult.Status:"")?options.lastResultElem.html('<span style="color:#FF0000;">('+globalize.translate("LabelFailed")+")</span>"):"Cancelled"===lastResult?options.lastResultElem.html('<span style="color:#0026FF;">('+globalize.translate("LabelCancelled")+")</span>"):"Aborted"===lastResult?options.lastResultElem.html('<span style="color:#FF0000;">'+globalize.translate("LabelAbortedByServerShutdown")+"</span>"):options.lastResultElem.html(lastResult)))}function onButtonClick(){var id,taskId=this.getAttribute("data-taskid");id=taskId,connectionManager.getApiClient(serverId).startScheduledTask(id).then(pollTasks)}function onScheduledTasksUpdate(e,apiClient,info){apiClient.serverId()===serverId&&updateTasks(info)}function onPollIntervalFired(){connectionManager.getApiClient(serverId).isMessageChannelOpen()||pollTasks()}options.panel&&options.panel.classList.add("hide"),"off"===options.mode?(button.removeEventListener("click",onButtonClick),events.off(serverNotifications,"ScheduledTasksInfo",onScheduledTasksUpdate),connectionManager.getApiClient(serverId).stopMessageListener("ScheduledTasksInfo"),pollInterval&&clearInterval(pollInterval)):(button.addEventListener("click",onButtonClick),pollTasks(),function(){var apiClient=connectionManager.getApiClient(serverId);pollInterval&&clearInterval(pollInterval),apiClient.startMessageListener("ScheduledTasksInfo","1000,1000"),pollInterval=setInterval(onPollIntervalFired,1e4)}(),events.on(serverNotifications,"ScheduledTasksInfo",onScheduledTasksUpdate))}});