define(["loading","dialogHelper","imageLoader","emby-checkbox","emby-button","paper-icon-button-light","css!css/metadataeditor.css","emby-linkbutton"],function(loading,dialogHelper,imageLoader){"use strict";function getBaseRemoteOptions(){var options={};return options.itemId=currentItemId,options}function reloadBrowsableImages(page){loading.show();var options=getBaseRemoteOptions();options.type=browsableImageType,options.startIndex=browsableImageStartIndex,options.limit=browsableImagePageSize,options.IncludeAllLanguages=page.querySelector("#chkAllLanguages").checked;var provider=selectedProvider||"";provider&&(options.ProviderName=provider),ApiClient.getAvailableRemoteImages(options).then(function(result){renderRemoteImages(page,result,browsableImageType,options.startIndex,options.limit),page.querySelector("#selectBrowsableImageType").value=browsableImageType;var providersHtml=result.Providers.map(function(p){return'"}),selectImageProvider=page.querySelector("#selectImageProvider");selectImageProvider.innerHTML='"+providersHtml,selectImageProvider.value=provider,loading.hide()})}function renderRemoteImages(page,imagesResult,imageType,startIndex,limit){page.querySelector(".availableImagesPaging").innerHTML=getPagingHtml(startIndex,limit,imagesResult.TotalRecordCount);for(var html="",i=0,length=imagesResult.Images.length;ilimit;html+='
',html+='';var startAtDisplay=totalRecordCount?startIndex+1:0;return html+=startAtDisplay+"-"+recordsEnd+" of "+totalRecordCount,html+="",showControls&&(html+='
',html+='',html+='',html+="
"),html+="
"}function parentWithClass(elem,className){for(;!elem.classList||!elem.classList.contains(className);)if(elem=elem.parentNode,!elem)return null;return elem}function downloadRemoteImage(page,url,type,provider){var options=getBaseRemoteOptions();options.Type=type,options.ImageUrl=url,options.ProviderName=provider,loading.show(),ApiClient.downloadRemoteImage(options).then(function(){hasChanges=!0;var dlg=parentWithClass(page,"dialog");dialogHelper.close(dlg)})}function getDisplayUrl(url){return ApiClient.getUrl("Images/Remote",{imageUrl:url})}function getRemoteImageHtml(image,imageType){var html="";html+='
';var cssClass="remoteImage lazy";cssClass+="Backdrop"==imageType||"Art"==imageType||"Thumb"==imageType||"Logo"==imageType?" remoteBackdropImage":"Banner"==imageType?" remoteBannerImage":"Disc"==imageType?" remoteDiscImage":"Episode"==currentItemType?" remoteBackdropImage":"MusicAlbum"==currentItemType||"MusicArtist"==currentItemType?" remoteDiscImage":" remotePosterImage";var displayUrl=getDisplayUrl(image.ThumbnailUrl||image.Url);return html+='',html+="",html+='
',html+='
',html+=image.ProviderName,html+="
",(image.Width||image.Height||image.Language)&&(html+='
',image.Width&&image.Height?(html+=image.Width+" x "+image.Height,image.Language&&(html+=" • "+image.Language)):image.Language&&(html+=image.Language),html+="
"),null!=image.CommunityRating&&(html+='
',"Likes"==image.RatingType?html+=image.CommunityRating+(1==image.CommunityRating?" like":" likes"):image.CommunityRating?(html+=image.CommunityRating.toFixed(1),image.VoteCount&&(html+=" • "+image.VoteCount+(1==image.VoteCount?" vote":" votes"))):html+="Unrated",html+="
"),html+='',html+="
",html+="
"}function initEditor(page){page.querySelector("#selectBrowsableImageType").addEventListener("change",function(){browsableImageType=this.value,browsableImageStartIndex=0,selectedProvider=null,reloadBrowsableImages(page)}),page.querySelector("#selectImageProvider").addEventListener("change",function(){browsableImageStartIndex=0,selectedProvider=this.value,reloadBrowsableImages(page)}),page.querySelector("#chkAllLanguages").addEventListener("change",function(){browsableImageStartIndex=0,reloadBrowsableImages(page)}),page.addEventListener("click",function(e){var btnDownloadRemoteImage=parentWithClass(e.target,"btnDownloadRemoteImage");btnDownloadRemoteImage&&downloadRemoteImage(page,btnDownloadRemoteImage.getAttribute("data-imageurl"),btnDownloadRemoteImage.getAttribute("data-imagetype"),btnDownloadRemoteImage.getAttribute("data-imageprovider"))})}function showEditor(itemId,itemType){loading.show();var xhr=new XMLHttpRequest;xhr.open("GET","components/imagedownloader/imagedownloader.template.html",!0),xhr.onload=function(e){var template=this.response;currentItemId=itemId,currentItemType=itemType;var dlg=dialogHelper.createDialog({size:"fullscreen-border",lockScroll:!0,removeOnClose:!0}),theme="b";dlg.classList.add("ui-body-"+theme),dlg.classList.add("background-theme-"+theme);var html="";html+='

',html+='',html+='
'+Globalize.translate("HeaderSearch")+"
",html+="

",html+='
',html+=Globalize.translateDocument(template),html+="
",dlg.innerHTML=html,dlg.addEventListener("close",onDialogClosed),dialogHelper.open(dlg);var editorContent=dlg.querySelector(".editorContent");initEditor(editorContent),dlg.querySelector(".btnCloseDialog").addEventListener("click",function(){dialogHelper.close(dlg)}),reloadBrowsableImages(editorContent)},xhr.send()}function onDialogClosed(){loading.hide(),hasChanges?currentResolve():currentReject()}var currentItemId,currentItemType,currentResolve,currentReject,selectedProvider,hasChanges=!1,browsableImagePageSize=browserInfo.slow?6:30,browsableImageStartIndex=0,browsableImageType="Primary";return{show:function(itemId,itemType,imageType){return new Promise(function(resolve,reject){currentResolve=resolve,currentReject=reject,hasChanges=!1,browsableImageStartIndex=0,browsableImageType=imageType||"Primary",selectedProvider=null,showEditor(itemId,itemType)})}}});