define(["loading","layoutManager","focusManager","connectionManager","appRouter","shell","appSettings","apphost","globalize","emby-scroller","emby-input","emby-linkbutton","material-icons","emby-button"],function(loading,layoutManager,focusManager,connectionManager,appRouter,shell,appSettings,appHost,globalize){"use strict";return function(view,params){var currentPinInfo,currentInterval,enablePinLogin=layoutManager.tv&&appHost.supports("externallinkdisplay");function showPinErrorMessage(key){var pinMessage=view.querySelector(".pinMessage");pinMessage.classList.remove("hide"),pinMessage.innerHTML=globalize.translate(""+key)}function createPin(){currentPinInfo=null,loading.show(),view.querySelector(".pinMessage").classList.add("hide"),view.querySelector(".pinCodeValue").innerHTML="&nbsp;",stopPolling(),connectionManager.createPin().then(function(result){currentPinInfo=result,view.querySelector(".pinCodeValue").innerHTML=result.Pin,currentInterval=setInterval(pollPinStatus,3e3),loading.hide()},function(){currentPinInfo=null,loading.hide(),showPinErrorMessage("CreatePinErrorMessage")})}function pollPinStatus(){currentPinInfo&&connectionManager.getPinStatus(currentPinInfo).then(function(pinStatus){pinStatus.IsConfirmed?(stopPolling(),loading.show(),appSettings.enableAutoLogin(!0),connectionManager.exchangePin(currentPinInfo).then(function(){connectionManager.connect().then(function(result){loading.hide(),"ConnectSignIn"===result.State?appRouter.showItem({Type:"AddServer"}):appRouter.handleConnectionResult(result)})})):pinStatus.IsExpired&&(stopPolling(),showPinErrorMessage("PinExpiredMessage"))})}function stopPolling(){currentInterval&&(clearInterval(currentInterval),currentInterval=null)}view.addEventListener("viewbeforeshow",function(e){e.detail.isRestored||(appHost.supports("externallinks")?(view.querySelector(".terms").innerHTML=globalize.translate("EmbyLoginTerms",'<a is="emby-linkbutton" class="lnkTerms button-link" href="https://emby.media/terms" target="_blank">',"</a>"),view.querySelector(".pinCodeHeader").innerHTML=globalize.translate("ConnectPinCodeHeader",'<a is="emby-linkbutton" class="lnkPinSignIn button-link" href="https://emby.media/pin" target="_blank">https://emby.media/pin</a>')):appHost.supports("externallinkdisplay")?(view.querySelector(".terms").innerHTML=globalize.translate("EmbyLoginTerms","",""),view.querySelector(".pinCodeHeader").innerHTML=globalize.translate("ConnectPinCodeHeader","https://emby.media/pin")):(view.querySelector(".terms").innerHTML=globalize.translate("EmbyLoginTerms","",""),view.querySelector(".pinCodeHeader").innerHTML=globalize.translate("ConnectPinCodeHeader","")),enablePinLogin?(view.querySelector(".pinLogin").classList.remove("hide"),view.querySelector(".newUsers").classList.add("hide"),view.querySelector(".manualLoginForm").classList.add("hide"),createPin()):(view.querySelector(".pinLogin").classList.add("hide"),view.querySelector(".newUsers").classList.remove("hide"),view.querySelector(".manualLoginForm").classList.remove("hide")))}),view.addEventListener("viewshow",function(e){e.detail.isRestored||(enablePinLogin?focusManager.focus(view.querySelector(".btnNewPin")):focusManager.autoFocus(view))}),view.querySelector(".btnSkipConnect").addEventListener("click",function(e){loading.show(),connectionManager.connect({enableAutoLogin:appSettings.enableAutoLogin()}).then(function(result){loading.hide(),"ConnectSignIn"===result.State?appRouter.showItem({Type:"AddServer"}):appRouter.handleConnectionResult(result)})}),view.querySelector(".btnSignup").addEventListener("click",function(){appHost.supports("connectsignup")?appRouter.show("/startup/connectsignup.html"):shell.openUrl("https://emby.media/community/index.php?app=core&module=global&section=register")}),view.querySelector(".btnNewPin").addEventListener("click",createPin),view.addEventListener("viewhide",function(e){stopPolling()}),view.querySelector(".manualLoginForm").addEventListener("submit",function(e){return loading.show(),connectionManager.loginToConnect(view.querySelector(".txtUser").value,view.querySelector(".txtPassword").value).then(function(){loading.hide(),appRouter.showSelectServer()},function(){loading.hide(),require(["alert"],function(alert){alert({text:globalize.translate("MessageInvalidUser"),title:globalize.translate("HeaderSignInError")})}),view.querySelector(".txtUser").value=""}),e.preventDefault(),e.stopPropagation(),!1})}});