define(["appRouter","globalize","appHeader","loading","appSettings","focusManager","connectionManager","emby-scroller","emby-select","emby-button","emby-input","emby-textarea","emby-checkbox"],function(appRouter,globalize,appHeader,loading,appSettings,focusManager,connectionManager){"use strict";return function(view,params){var player,isNewPlayer;function onMediaTypeChange(e){for(var mediaType=this.value,fields=view.querySelectorAll(".mediaTypeField"),i=0,length=fields.length;i<length;i++){var fld=fields[i];fld.getAttribute("data-mediatype")===mediaType?fld.classList.remove("hide"):fld.classList.add("hide")}}function save(){player.mediaType=view.querySelector(".selectMediaType").value,player.path=view.querySelector(".txtPath").value;var args=view.querySelector(".txtArguments").value.trim();player.arguments=args?args.split("\n"):[];var chkVideoTypes=view.querySelectorAll(".videoType");for(i=0,length=chkVideoTypes.length;i<length;i++){var chkVideoType=chkVideoTypes[i];player["videotype-"+chkVideoType.getAttribute("data-type")]=chkVideoType.checked}var players=getPlayers();if(isNewPlayer)player.id=(new Date).getTime().toString(),players.push(player);else{for(var index=-1,i=0,length=players.length;i<length;i++)if(players[i].id===player.id){index=i;break}-1===index?players.push(player):players[i]=player}player.gameSystem=view.querySelector(".selectGameSystem").value,appSettings.set("externalplayers",JSON.stringify(players)),isNewPlayer&&appRouter.back()}function getPlayers(){return JSON.parse(appSettings.get("externalplayers")||"[]")}view.addEventListener("viewshow",function(e){var isRestored=e.detail.isRestored;appHeader.setTitle(globalize.translate("ExternalPlayer")),loading.hide(),isRestored||(function(){player=null,params.id&&(player=getPlayers().filter(function(p){return p.id===params.id})[0]);player?isNewPlayer=!1:player={"videotype-stream":!(isNewPlayer=!0),"videotype-file":!1}}(),function(){isNewPlayer?view.querySelector(".btnSave").classList.remove("hide"):view.querySelector(".btnSave").classList.add("hide");var selectMediaType=view.querySelector(".selectMediaType");selectMediaType.value=player.mediaType||"Video",onMediaTypeChange.call(selectMediaType),view.querySelector(".txtPath").value=player.path||"",view.querySelector(".txtArguments").value=(player.arguments||[]).join("\n");for(var chkVideoTypes=view.querySelectorAll(".videoType"),i=0,length=chkVideoTypes.length;i<length;i++){var chkVideoType=chkVideoTypes[i];"3d"===chkVideoType.getAttribute("data-type")?chkVideoType.checked=!0===player["videotype-"+chkVideoType.getAttribute("data-type")]:chkVideoType.checked=!1!==player["videotype-"+chkVideoType.getAttribute("data-type")]}!function(value){connectionManager.currentApiClient().getItems(null,{EnableTotalRecordCount:!1,IncludeItemTypes:"GameSystem",Recursive:!0}).then(function(result){var selectGameSystem=view.querySelector(".selectGameSystem");selectGameSystem.innerHTML=result.Items.map(function(g){return'<option value="'+g.Id+'">'+g.Name+"</option>"}).join(""),value&&(selectGameSystem.value=player.gameSystem)})}(player.gameSystem)}(),focusManager.autoFocus(view))}),view.addEventListener("viewbeforehide",function(e){isNewPlayer||(view.querySelector("form").checkValidity()?save():e.preventDefault())}),view.querySelector("form").addEventListener("submit",function(e){return save(),e.preventDefault(),!1}),view.querySelector(".selectMediaType").addEventListener("change",onMediaTypeChange)}});