define(["dom","appRouter","dialogHelper","globalize","layoutManager","mediaInfo","connectionManager","require","loading","datetime","recordingFields","events","emby-checkbox","emby-button","emby-input","paper-icon-button-light","formDialogStyle","css!./recordingcreator","material-icons","emby-scroller"],function(dom,appRouter,dialogHelper,globalize,layoutManager,mediaInfo,connectionManager,require,loading,datetime,RecordingFields,events){"use strict";var closeAction;function closeDialog(elem){dialogHelper.close(elem.closest(".dialog"))}function renderRecording(context,defaultTimer,program,apiClient,refreshRecordingStateOnly){var imgUrl,imageContainer,formDialogFooter,now;refreshRecordingStateOnly||(imgUrl=function(item,apiClient,imageHeight){var imageTags=item.ImageTags||{};return item.PrimaryImageTag&&(imageTags.Primary=item.PrimaryImageTag),imageTags.Primary?apiClient.getImageUrl(item.Id,{type:"Primary",maxHeight:imageHeight,tag:item.ImageTags.Primary}):imageTags.Thumb?apiClient.getImageUrl(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||"",formDialogFooter=context.querySelector(".formDialogFooter"),(now=new Date)>=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()}function reload(context,recordingFields,refreshItem,refreshRecordingStateOnly){loading.show();var program=recordingFields.getProgram(),apiClient=connectionManager.getApiClient(program),promises=[apiClient.getNewLiveTvTimerDefaults({programId:program.Id})];refreshItem&&promises.push(apiClient.getLiveTvProgram(program.Id,apiClient.getCurrentUserId())),Promise.all(promises).then(function(responses){responses[0];refreshItem&&(program=responses[1],recordingFields.refresh(program)),renderRecording(context,0,program,apiClient,refreshRecordingStateOnly)})}return{show:function(item){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="";html+=globalize.translateDocument(template,"sharedcomponents"),dlg.innerHTML=html;var context,recordingFields=new RecordingFields({parent:dlg.querySelector(".recordingFields"),program:item});function onRecordingChanged(){reload(dlg,recordingFields,!0,!0)}(context=dlg).querySelector(".btnPlay").addEventListener("click",function(){closeAction="play",closeDialog(this)}),context.querySelector(".btnCancel").addEventListener("click",function(){closeAction=null,closeDialog(this)}),context.querySelector(".btnManageSeriesRecording").addEventListener("click",function(){closeAction="manageseries",closeDialog(this)}),reload(dlg,recordingFields,!0),events.on(recordingFields,"recordingchanged",onRecordingChanged),dialogHelper.open(dlg).then(function(){events.off(recordingFields,"recordingchanged",onRecordingChanged);var action,item,updatedProgram=recordingFields.getProgram(),hasChanged=recordingFields.hasChanged();recordingFields.destroy(),item=updatedProgram,"play"!==(action=closeAction)?"manageseries"===action&&appRouter.showItem({Type:"SeriesTimer",Id:item.SeriesTimerId,ServerId:item.ServerId}):require(["playbackManager"],function(playbackManager){playbackManager.play({ids:[item.ChannelId],serverId:item.ServerId})}),(recordingFields&&hasChanged?resolve:reject)()})})})}}});