define(["jQuery","scripts/taskbutton","dom","libraryMenu","layoutManager","loading","listViewStyle","flexStyles","emby-itemscontainer","cardStyle","material-icons"],function($,taskButton,dom,libraryMenu,layoutManager,loading){"use strict";function resetTuner(page,id){var message=Globalize.translate("MessageConfirmResetTuner");require(["confirm"],function(confirm){confirm(message,Globalize.translate("HeaderResetTuner")).then(function(){loading.show(),ApiClient.resetLiveTvTuner(id).then(function(){loading.hide(),reload(page)})})})}function renderTuners(page,tuners){var html="";if(tuners.length){html+='
';for(var i=0,length=tuners.length;i',html+='live_tv',html+='
',html+='

',html+=tuner.Name,html+="

",html+='
',html+=tuner.SourceType,html+="
",html+='
',"RecordingTv"==tuner.Status?tuner.ChannelName?(html+='',html+=Globalize.translate("StatusRecordingProgram").replace("{0}",tuner.ChannelName),html+=""):html+=Globalize.translate("StatusRecording"):"LiveTv"==tuner.Status?tuner.ChannelName?(html+='',html+=Globalize.translate("StatusWatchingProgram").replace("{0}",tuner.ChannelName),html+=""):html+=Globalize.translate("StatusWatching"):html+=tuner.Status,html+="
",html+="
",tuner.CanReset&&(html+=''),html+="
"}html+=""}tuners.length?page.querySelector(".tunerSection").classList.remove("hide"):page.querySelector(".tunerSection").classList.add("hide");var elem=$(".tunerList",page).html(html);$(".btnResetTuner",elem).on("click",function(){var id=this.getAttribute("data-tunerid");resetTuner(page,id)})}function getServiceHtml(service){var html="";html+="
";var serviceUrl=service.HomePageUrl||"#";html+='

'+service.Name+"

";var versionHtml=service.Version||"Unknown";versionHtml+=service.HasUpdateAvailable?' '+Globalize.translate("LiveTvUpdateAvailable")+"":' '+Globalize.translate("LabelVersionUpToDate"),html+="

"+versionHtml+"

";var status=service.Status;return"Ok"==service.Status?status=''+status+"":(service.StatusMessage&&(status+=" ("+service.StatusMessage+")"),status=''+status+""),html+="

"+Globalize.translate("ValueStatus",status)+"

",html+="
"}function loadPage(page,liveTvInfo){liveTvInfo.IsEnabled?$(".liveTvStatusContent",page).show():$(".liveTvStatusContent",page).hide();var servicesToDisplay=liveTvInfo.Services.filter(function(s){return s.IsVisible});servicesToDisplay.length?$(".servicesSection",page).show():$(".servicesSection",page).hide(),$(".servicesList",page).html(servicesToDisplay.map(getServiceHtml).join(""));for(var tuners=[],i=0,length=liveTvInfo.Services.length;i',html+='
',html+='
',html+='
',html+='
',html+='
dvr
',html+="
",html+="
",html+='
',html+='',html+='
'+(device.FriendlyName||device.Type)+"
",html+='
',html+=device.Url||" ",html+="
",html+="
",html+="
",html+=""}function renderDevices(page,devices){var html=devices.map(getDeviceHtml).join("");page.querySelector(".devicesList").innerHTML=html}function deleteDevice(page,id){var message=Globalize.translate("MessageConfirmDeleteTunerDevice");require(["confirm"],function(confirm){confirm(message,Globalize.translate("HeaderDeleteDevice")).then(function(){loading.show(),ApiClient.ajax({type:"DELETE",url:ApiClient.getUrl("LiveTv/TunerHosts",{Id:id})}).then(function(){reload(page)})})})}function reload(page){loading.show(),ApiClient.getLiveTvInfo().then(function(liveTvInfo){loadPage(page,liveTvInfo)},function(){loadPage(page,{Services:[],IsEnabled:!0})})}function submitAddDeviceForm(page){page.querySelector(".dlgAddDevice").close(),loading.show(),ApiClient.ajax({type:"POST",url:ApiClient.getUrl("LiveTv/TunerHosts"),data:JSON.stringify({Type:$("#selectTunerDeviceType",page).val(),Url:$("#txtDevicePath",page).val()}),contentType:"application/json"}).then(function(){reload(page)},function(){Dashboard.alert({message:Globalize.translate("ErrorAddingTunerDevice")})})}function renderProviders(page,providers){var html="";if(providers.length){html+='
';for(var i=0,length=providers.length;i',html+='dvr',html+='",html+='',html+="
"}html+=""}var elem=$(".providerList",page).html(html);$(".btnOptions",elem).on("click",function(){var id=this.getAttribute("data-id");showProviderOptions(page,id,this)})}function showProviderOptions(page,providerId,button){var items=[];items.push({name:Globalize.translate("ButtonDelete"),id:"delete"}),items.push({name:Globalize.translate("MapChannels"),id:"map"}),require(["actionsheet"],function(actionsheet){actionsheet.show({items:items,positionTo:button}).then(function(id){switch(id){case"delete":deleteProvider(page,providerId);break;case"map":mapChannels(page,providerId)}})})}function mapChannels(page,providerId){require(["components/channelmapper/channelmapper"],function(channelmapper){new channelmapper({serverId:ApiClient.serverInfo().Id,providerId:providerId}).show()})}function deleteProvider(page,id){var message=Globalize.translate("MessageConfirmDeleteGuideProvider");require(["confirm"],function(confirm){confirm(message,Globalize.translate("HeaderDeleteProvider")).then(function(){loading.show(),ApiClient.ajax({type:"DELETE",url:ApiClient.getUrl("LiveTv/ListingProviders",{Id:id})}).then(function(){reload(page)},function(){reload(page)})})})}function getProviderName(providerId){switch(providerId=providerId.toLowerCase()){case"schedulesdirect":return"Schedules Direct";case"xmltv":return"Xml TV";case"emby":return"Emby Guide";default:return"Unknown"}}function getProviderConfigurationUrl(providerId){switch(providerId=providerId.toLowerCase()){case"xmltv":return"livetvguideprovider.html?type=xmltv";case"schedulesdirect":return"livetvguideprovider.html?type=schedulesdirect";case"emby":return"livetvguideprovider.html?type=emby"}}function addProvider(button){var menuItems=[];menuItems.push({name:"Schedules Direct",id:"SchedulesDirect"}),menuItems.push({name:"Xml TV",id:"xmltv"}),menuItems.push({name:Globalize.translate("ButtonOther"),id:"other"}),require(["actionsheet"],function(actionsheet){actionsheet.show({items:menuItems,positionTo:button,callback:function(id){"other"==id?Dashboard.alert({message:Globalize.translate("ForAdditionalLiveTvOptions")}):Dashboard.navigate(getProviderConfigurationUrl(id))}})})}function addDevice(button){Dashboard.navigate("livetvtuner.html")}function getTabs(){return[{href:"livetvstatus.html",name:Globalize.translate("TabDevices")},{href:"livetvsettings.html",name:Globalize.translate("TabSettings")},{href:"appservices.html?context=livetv",name:Globalize.translate("TabServices")}]}function showDeviceMenu(button,tunerDeviceId){var items=[];items.push({name:Globalize.translate("ButtonDelete"),id:"delete"}),items.push({name:Globalize.translate("ButtonEdit"),id:"edit"}),require(["actionsheet"],function(actionsheet){actionsheet.show({items:items,positionTo:button}).then(function(id){switch(id){case"delete":deleteDevice(dom.parentWithClass(button,"page"),tunerDeviceId);break;case"edit":Dashboard.navigate("livetvtuner.html?id="+tunerDeviceId)}})})}function onDevicesListClick(e){var card=dom.parentWithClass(e.target,"card");if(card){var id=card.getAttribute("data-id"),btnCardOptions=dom.parentWithClass(e.target,"btnCardOptions");btnCardOptions?showDeviceMenu(btnCardOptions,id):Dashboard.navigate("livetvtuner.html?id="+id)}}$(document).on("pageinit","#liveTvStatusPage",function(){var page=this;$(".btnAddDevice",page).on("click",function(){addDevice(this)}),$(".formAddDevice",page).on("submit",function(){return submitAddDeviceForm(page),!1}),$(".btnAddProvider",page).on("click",function(){addProvider(this)}),page.querySelector(".devicesList").addEventListener("click",onDevicesListClick)}).on("pageshow","#liveTvStatusPage",function(){libraryMenu.setTabs("livetvadmin",0,getTabs);var page=this;reload(page),taskButton({mode:"on",progressElem:page.querySelector(".refreshGuideProgress"),taskKey:"RefreshGuide",button:page.querySelector(".btnRefresh")})}).on("pagehide","#liveTvStatusPage",function(){var page=this;taskButton({mode:"off",progressElem:page.querySelector(".refreshGuideProgress"),taskKey:"RefreshGuide",button:page.querySelector(".btnRefresh")})})});