define(["dialogHelper","globalize","userSettings","layoutManager","require","emby-scroller","emby-checkbox","emby-select","formDialogStyle","material-icons"],function(dialogHelper,globalize,userSettings,layoutManager,require){"use strict";function load(context){for(var chkIndicators=context.querySelectorAll(".chkIndicator"),i=0,length=chkIndicators.length;i<length;i++){var type=chkIndicators[i].getAttribute("data-type");"true"===chkIndicators[i].getAttribute("data-default")?chkIndicators[i].checked="false"!==userSettings.get("guide-indicator-"+type):chkIndicators[i].checked="true"===userSettings.get("guide-indicator-"+type)}context.querySelector(".chkColorCodedBackgrounds").checked="true"===userSettings.get("guide-colorcodedbackgrounds"),function(context){for(var value,items=userSettings.getLiveTvChannelSortOrders(globalize),html="",i=0,length=items.length;i<length;i++){var item=items[i];html+='<option value="'+item.value+'">'+item.name+"</option>",item.selected&&(value=item.value)}var select=context.querySelector(".selectChannelSort");select.innerHTML=html,select.value=value}(context)}function getOptionHtml(item){return'<option value="'+item.Id+'">'+item.Name+"</option>"}function loadChannelTags(context,apiClient){apiClient.getLiveTvChannelTags({SortBy:"SortName",SortOrder:"Ascending",EnableImages:!1,EnableUserData:!1,EnableTotalRecordCount:!1}).then(function(result){result.Items.length?context.querySelector(".fldTags").classList.remove("hide"):context.querySelector(".fldTags").classList.add("hide");var select,items,selectTags=context.querySelector(".selectTags");select=selectTags,items=result.Items,select.innerHTML=items.map(getOptionHtml).join("");var channelTagIds=(channelTagIds=userSettings.get("guide-tagids"))?channelTagIds.split(","):[];selectTags.setValues(channelTagIds)})}return{show:function(options,apiClient){return new Promise(function(resolve,reject){var settingsChanged=!1;require(["text!./guide-settings.template.html"],function(template){var dialogOptions={removeOnClose:!0,scrollY:!1};layoutManager.tv?dialogOptions.size="fullscreen":dialogOptions.size="small";var dlg=dialogHelper.createDialog(dialogOptions);dlg.classList.add("formDialog");var html="";html+=globalize.translateDocument(template,"sharedcomponents"),dlg.innerHTML=html,dlg.addEventListener("change",function(){settingsChanged=!0}),dlg.addEventListener("close",function(){!function(context){for(var chkIndicators=context.querySelectorAll(".chkIndicator"),i=0,length=chkIndicators.length;i<length;i++){var type=chkIndicators[i].getAttribute("data-type");userSettings.set("guide-indicator-"+type,chkIndicators[i].checked)}userSettings.set("guide-colorcodedbackgrounds",context.querySelector(".chkColorCodedBackgrounds").checked),userSettings.set(userSettings.getLiveTvChannelSortSettingsKey(),context.querySelector(".selectChannelSort").value),userSettings.set("guide-tagids",context.querySelector(".selectTags").getValues().join(","))}(dlg),function(context,options){for(var categories=[],chkCategorys=context.querySelectorAll(".chkCategory"),i=0,length=chkCategorys.length;i<length;i++){var type=chkCategorys[i].getAttribute("data-type");chkCategorys[i].checked&&categories.push(type)}4<=categories.length&&categories.push("series"),categories.push("all"),options.categories=categories}(dlg,options),(settingsChanged?resolve:reject)()}),dlg.querySelector(".btnCancel").addEventListener("click",function(){dialogHelper.close(dlg)}),load(dlg),loadChannelTags(dlg,apiClient),function(context,options){for(var selectedCategories=options.categories||[],chkCategorys=context.querySelectorAll(".chkCategory"),i=0,length=chkCategorys.length;i<length;i++){var type=chkCategorys[i].getAttribute("data-type");chkCategorys[i].checked=!selectedCategories.length||-1!==selectedCategories.indexOf(type)}}(dlg,options),dialogHelper.open(dlg)})})}}});