define(["dialogHelper","globalize","layoutManager","mediaInfo","apphost","connectionManager","require","loading","datetime","recordingFields","events","emby-checkbox","emby-button","emby-input","paper-icon-button-light","css!./../formdialog","css!./recordingcreator","material-icons","emby-scroller"],function(dialogHelper,globalize,layoutManager,mediaInfo,appHost,connectionManager,require,loading,datetime,recordingFields,events){"use strict";var currentDialog,closeAction,currentRecordingFields;function closeDialog(){dialogHelper.close(currentDialog)}function reload(context,programId,serverId,refreshRecordingStateOnly){loading.show();var apiClient=connectionManager.getApiClient(serverId),promise1=apiClient.getNewLiveTvTimerDefaults({programId:programId}),promise2=apiClient.getLiveTvProgram(programId,apiClient.getCurrentUserId());Promise.all([promise1,promise2]).then(function(responses){responses[0];var program=responses[1];!function(context,defaultTimer,program,apiClient,refreshRecordingStateOnly){if(!refreshRecordingStateOnly){var imgUrl=function(item,apiClient,imageHeight){var imageTags=item.ImageTags||{};return item.PrimaryImageTag&&(imageTags.Primary=item.PrimaryImageTag),imageTags.Primary?apiClient.getScaledImageUrl(item.Id,{type:"Primary",maxHeight:imageHeight,tag:item.ImageTags.Primary}):imageTags.Thumb?apiClient.getScaledImageUrl(item.Id,{type:"Thumb",maxHeight:imageHeight,tag:item.ImageTags.Thumb}):null}(program,apiClient,200),imageContainer=context.querySelector(".recordingDialog-imageContainer");imgUrl?(imageContainer.innerHTML='<img src="'+imgUrl+'" loading="lazy" class="recordingDialog-img" />',imageContainer.classList.remove("hide")):(imageContainer.innerHTML="",imageContainer.classList.add("hide")),context.querySelector(".recordingDialog-itemName").innerHTML=program.Name,context.querySelector(".formDialogHeaderTitle").innerHTML=program.Name,context.querySelector(".itemGenres").innerHTML=(program.Genres||[]).join(" / "),context.querySelector(".itemOverview").innerHTML=program.Overview||"";var formDialogFooter=context.querySelector(".formDialogFooter"),now=new Date;now>=datetime.parseISO8601Date(program.StartDate,!0)&&now<datetime.parseISO8601Date(program.EndDate,!0)?formDialogFooter.classList.remove("hide"):formDialogFooter.classList.add("hide"),context.querySelector(".itemMiscInfoPrimary").innerHTML=mediaInfo.getPrimaryMediaInfoHtml(program)}context.querySelector(".itemMiscInfoSecondary").innerHTML=mediaInfo.getSecondaryMediaInfoHtml(program,{}),loading.hide()}(context,0,program,apiClient,refreshRecordingStateOnly)})}return{show:function(itemId,serverId){return new Promise(function(resolve,reject){closeAction=null,loading.show(),require(["text!./recordingcreator.template.html"],function(template){var dialogOptions={removeOnClose:!0,scrollY:!1};layoutManager.tv?dialogOptions.size="fullscreen":dialogOptions.size="small";var dlg=dialogHelper.createDialog(dialogOptions);dlg.classList.add("formDialog"),dlg.classList.add("recordingDialog");var html="";function onRecordingChanged(){reload(dlg,itemId,serverId,!0)}html+=globalize.translateDocument(template,"sharedcomponents"),dlg.innerHTML=html,(currentDialog=dlg).addEventListener("close",function(){events.off(currentRecordingFields,"recordingchanged",onRecordingChanged),function(action,programId,serverId){"play"!==action||require(["playbackManager"],function(playbackManager){var apiClient=connectionManager.getApiClient(serverId);apiClient.getLiveTvProgram(programId,apiClient.getCurrentUserId()).then(function(item){playbackManager.play({ids:[item.ChannelId],serverId:serverId})})})}(closeAction,itemId,serverId),currentRecordingFields&&currentRecordingFields.hasChanged()?resolve():reject()}),function(context){context.querySelector(".btnPlay").addEventListener("click",function(){closeAction="play",closeDialog()}),context.querySelector(".btnCancel").addEventListener("click",function(){closeAction=null,closeDialog()})}(dlg),reload(dlg,itemId,serverId),currentRecordingFields=new recordingFields({parent:dlg.querySelector(".recordingFields"),programId:itemId,serverId:serverId}),events.on(currentRecordingFields,"recordingchanged",onRecordingChanged),dialogHelper.open(dlg)})})}}});