define(["events","dom","playbackManager","pluginManager","connectionManager","userSettings","inputManager","apphost"],function(events,dom,playbackManager,pluginManager,connectionManager,userSettings,inputManager,appHost){"use strict";var isPlayingVideo,lastFunctionalEvent=0;function getCurrentPlugin(){var apiClient=connectionManager.currentApiClient();apiClient&&apiClient.isLoggedIn();for(var option=function(){try{return userSettings.screensaver()}catch(err){return null}}(),plugins=pluginManager.ofType("screensaver"),i=0,length=plugins.length;i<length;i++){var plugin=plugins[i];if(plugin.id===option)return plugin}return null}function ScreenSaverManager(){this.onInputFn=function(e){e.preventDefault(),this.hide()}.bind(this),this.hideFn=this.hide.bind(this),events.on(userSettings,"change",function(e,name){"screensaver"===name&&this.resetInterval()}.bind(this)),events.on(connectionManager,"localusersignedin",function(e){lastFunctionalEvent=Date.now(),this.resetInterval()}.bind(this)),events.on(connectionManager,"localusersignedout",function(e){lastFunctionalEvent=Date.now(),this.resetInterval()}.bind(this))}function onReject(){}return events.on(playbackManager,"playbackstart",function(e,player,state){(isPlayingVideo=player.isLocalPlayer&&state.NowPlayingItem&&"Audio"!==state.NowPlayingItem.MediaType)&&(lastFunctionalEvent=Date.now())}),events.on(playbackManager,"playbackstop",function(e,stopInfo){isPlayingVideo&&!stopInfo.nextMediaType&&(lastFunctionalEvent=Date.now(),isPlayingVideo=!1)}),events.on(appHost,"pause",function(){lastFunctionalEvent=Date.now()}),events.on(appHost,"resume",function(){lastFunctionalEvent=Date.now()}),ScreenSaverManager.prototype.resetInterval=function(){getCurrentPlugin()?this.setInterval():this.clearInterval()},ScreenSaverManager.prototype.setInterval=function(){this.interval||appHost.supports("screensaver")&&(this.interval=setInterval(function(){if(!this.isShowing()&&!(Date.now()-lastFunctionalEvent<3e5||isPlayingVideo)){var doc=document;return"hidden"!==doc.visibilityState&&(!!doc.hasFocus()&&void(inputManager.idleTime()<3e5||this.show()))}}.bind(this),1e4))},ScreenSaverManager.prototype.clearInterval=function(){var interval=this.interval;interval&&(this.interval=null,clearInterval(interval))},ScreenSaverManager.prototype.showScreenSaver=function(screensaver){var onInputFn;this.activeScreenSaver||(console.log("Showing screensaver "+screensaver.name),screensaver.show(),this.activeScreenSaver=screensaver,this.removeHideEventListeners(),this.addHideEventListeners(),onInputFn=this.onInputFn,inputManager.on(window,onInputFn))},ScreenSaverManager.prototype.isShowing=function(){return null!=this.activeScreenSaver},ScreenSaverManager.prototype.show=function(){this.isShowing()||getCurrentPlugin()&&require(["registrationServices"]).then(function(deps){return deps[0].validateFeature("screensaver",{showDialog:!1})}).then(function(){var screensaver=getCurrentPlugin();screensaver&&this.showScreenSaver(screensaver)}.bind(this),onReject)},ScreenSaverManager.prototype.addHideEventListeners=function(){var hideFn=this.hideFn;dom.addEventListener(window,"click",hideFn,{capture:!0,passive:!0}),dom.addEventListener(window,"mousemove",hideFn,{capture:!0,passive:!0})},ScreenSaverManager.prototype.removeHideEventListeners=function(){var hideFn=this.hideFn;dom.removeEventListener(window,"click",hideFn,{capture:!0,passive:!0}),dom.removeEventListener(window,"mousemove",hideFn,{capture:!0,passive:!0})},ScreenSaverManager.prototype.hide=function(){var onInputFn,screensaver=this.activeScreenSaver;screensaver&&(this.activeScreenSaver=null,console.log("Hiding screensaver"),screensaver.hide(),console.log("unbind "+Date.now()),this.removeHideEventListeners(),onInputFn=this.onInputFn,inputManager.off(window,onInputFn))},new ScreenSaverManager});