define(["connectionManager","loading","appRouter","apphost","globalize","emby-linkbutton","emby-button","emby-checkbox","emby-input","emby-scroller","css!./profile"],function(connectionManager,loading,appRouter,appHost,globalize){"use strict";var currentFile;function reloadUser(page,apiClient,userId){loading.show(),apiClient.getUser(userId).then(function(user){page.querySelector(".username").innerHTML=user.Name;var imageUrl,uploadUserImage=page.querySelector(".uploadUserImage");uploadUserImage.value="",uploadUserImage.dispatchEvent(new CustomEvent("change",{})),appRouter.setTitle(user.Name);var fldImage=page.querySelector(".fldImage");user.PrimaryImageTag?(imageUrl=apiClient.getUserImageUrl(user.Id,{height:200,tag:user.PrimaryImageTag,type:"Primary"}),fldImage.innerHTML='<img style="border-radius:.3em;width:7em;margin-right:1em;" src="'+imageUrl+'" />'):fldImage.innerHTML='<i class="md-icon" style="font-size:6em;margin-right:.25em;">person</i>',fldImage.classList.remove("hide");apiClient.getCurrentUser().then(function(loggedInUser){appHost.supports("fileinput")&&(loggedInUser.Policy.IsAdministrator||user.Policy.EnableUserPreferenceAccess)?(page.querySelector(".newImageForm").classList.remove("hide"),user.PrimaryImageTag?page.querySelector(".btnDeleteImage").classList.remove("hide"):page.querySelector(".btnDeleteImage").classList.add("hide")):(page.querySelector(".newImageForm").classList.add("hide"),page.querySelector(".btnDeleteImage").classList.add("hide"))}),loading.hide()})}function displayFileError(text){require(["toast"],function(toast){toast(globalize.translate(text))})}function onFileReaderError(evt){switch(loading.hide(),evt.target.error.code){case evt.target.error.NOT_FOUND_ERR:displayFileError("FileNotFound");break;case evt.target.error.NOT_READABLE_ERR:displayFileError("FileReadError");break;case evt.target.error.ABORT_ERR:break;default:displayFileError("FileReadError")}}function onFileReaderAbort(evt){loading.hide(),displayFileError("FileReadCancelled")}function onImageDragOver(e){return e.preventDefault(),!(e.originalEvent.dataTransfer.dropEffect="Copy")}return function(view,params){var apiClient=connectionManager.getApiClient(params.serverId),userId=params.userId;reloadUser(view,apiClient,userId),view.querySelector(".userImageDropZone").addEventListener("dragOver",onImageDragOver),view.querySelector(".btnDeleteImage").addEventListener("click",function(){require(["confirm"],function(confirm){confirm({title:globalize.translate("HeaderDeleteImage"),text:globalize.translate("ConfirmDeleteImage"),confirmText:globalize.translate("Delete"),primary:"cancel"}).then(function(){loading.show(),apiClient.deleteUserImage(userId,"primary").then(function(){loading.hide(),reloadUser(view,apiClient,userId)})})})}),view.querySelector(".btnBrowse").addEventListener("click",function(){view.querySelector(".uploadUserImage").click()}),view.querySelector(".newImageForm").addEventListener("submit",function(e){var file=currentFile;return file&&("image/png"!==file.type&&"image/jpeg"!==file.type&&"image/jpeg"!==file.type||(loading.show(),apiClient.uploadUserImage(userId,"Primary",file).then(function(){loading.hide(),reloadUser(view,apiClient,userId)}),e.preventDefault())),!1}),view.querySelector(".uploadUserImage").addEventListener("change",function(e){!function(page,files){var file=files[0];if(!file||!file.type.match("image.*"))return page.querySelector(".userImageOutput").innerHTML="",page.querySelector(".fldUpload").classList.add("hide"),currentFile=null;currentFile=file;var reader=new FileReader;reader.onerror=onFileReaderError,reader.onloadstart=function(){page.querySelector(".fldUpload").classList.add("hide")},reader.onabort=onFileReaderAbort,reader.onload=function(e){var html=['<img style="max-width:100%;max-height:100%;" src="',e.target.result,'" title="',escape(file.name),'"/>'].join("");page.querySelector(".userImageOutput").innerHTML=html,page.querySelector(".fldUpload").classList.remove("hide")},reader.readAsDataURL(file)}(view,e.target.files)})}});