define(["events","browser","loading","playbackManager","appRouter","globalize","apphost"],function(events,browser,loading,playbackManager,appRouter,globalize,appHost){"use strict";var currentItem;function mirrorIfEnabled(item){if(item?currentItem=item:item=currentItem,item){if(item.IsFolder&&"Series"!==item.Type&&"MusicAlbum"!==item.Type&&"MusicArtist"!==item.Type)return;var currentPlayer=playbackManager.getCurrentPlayer();currentPlayer&&!currentPlayer.isLocalPlayer&&playbackManager.displayContent({ItemName:item.Name,ItemId:item.Id,ItemType:item.Type},currentPlayer)}}function emptyCallback(){}return document.addEventListener("viewshow",function(e){var item=(e.detail.state||{}).item;item&&item.ServerId&&mirrorIfEnabled(item)}),events.on(playbackManager,"pairing",function(e){loading.show()}),events.on(playbackManager,"paired",function(e){loading.hide()}),events.on(playbackManager,"pairerror",function(e){loading.hide()}),{show:function(button){var playerInfo,currentPlayerInfo=playbackManager.getPlayerInfo();if(currentPlayerInfo&&!currentPlayerInfo.isLocalPlayer)return playerInfo=currentPlayerInfo,void require(["dialog"],function(dialog){!function(dialog,playerInfo){var items=[],currentDeviceName=playerInfo.deviceName||playerInfo.name,options={title:currentDeviceName};items.push({name:globalize.translate("HeaderRemoteControl"),id:"remote"}),items.push({name:globalize.translate("Disconnect"),id:"disconnect"}),items.push({name:globalize.translate("Cancel"),id:"cancel",type:"cancel"}),options.buttons=items,dialog(options).then(function(result){return"remote"===result?appRouter.showNowPlaying():("disconnect"===result&&function(currentDeviceName){-1!==playbackManager.getSupportedCommands().indexOf("EndSession")?require(["dialog"],function(dialog){var menuItems=[];menuItems.push({name:globalize.translate("Yes"),id:"yes"}),menuItems.push({name:globalize.translate("No"),id:"no"}),dialog({buttons:menuItems,text:globalize.translate("ConfirmEndPlayerSession",currentDeviceName)}).then(function(id){switch(id){case"yes":playbackManager.getCurrentPlayer().endSession(),playbackManager.setDefaultPlayerActive();break;case"no":playbackManager.setDefaultPlayerActive()}},emptyCallback)}):playbackManager.setDefaultPlayerActive()}(currentDeviceName),Promise.resolve())},emptyCallback)}(dialog,playerInfo)});var currentPlayerId=currentPlayerInfo?currentPlayerInfo.id:null;loading.show(),playbackManager.getTargets().then(function(targets){var menuItems=targets.map(function(t){var target,name=t.name;return t.appName&&t.appName!==t.name&&(name+=" - "+t.appName),{name:name,id:t.id,selected:currentPlayerId===t.id,secondaryText:(target=t).user?target.user.Name:null,icon:function(target){switch(target.deviceType||"tv"){case"smartphone":return"&#xE32C;";case"tablet":return"&#xE32F;";case"tv":return"&#xE333;";case"cast":return"&#xE307;";case"desktop":return"&#xE30A;";default:return"&#xE333;"}}(t)}});require(["actionsheet"],function(actionsheet){loading.hide();var menuOptions={title:globalize.translate("HeaderPlayOn"),items:menuItems,positionTo:button,positionY:"bottom",resolveOnClick:!0,iconRight:!1};browser.chrome&&!appHost.supports("castmenuhashchange")&&(menuOptions.enableHistory=!1),actionsheet.show(menuOptions).then(function(id){var target=targets.filter(function(t){return t.id===id})[0];playbackManager.trySetActivePlayer(target.playerName,target),mirrorIfEnabled()},emptyCallback)})})}}});