define(["userSettingsBuilder","appStorage","loading","skinManager","emby-linkbutton"],function(userSettingsBuilder,appStorage,loading,skinManager){"use strict";function fillThemes(select,isDashboard){select.innerHTML=skinManager.getThemes().map(function(t){var value=t.id;return t.isDefault&&!isDashboard?value="":t.isDefaultServerDashboard&&isDashboard&&(value=""),'<option value="'+value+'">'+t.name+"</option>"}).join("")}return function(view,params){function loadForm(page,user){user.Policy.IsAdministrator?page.querySelector(".selectDashboardThemeContainer").classList.remove("hide"):page.querySelector(".selectDashboardThemeContainer").classList.add("hide"),userSettingsInstance.setUserInfo(userId,ApiClient).then(function(){var selectTheme=page.querySelector("#selectTheme"),selectDashboardTheme=page.querySelector("#selectDashboardTheme");fillThemes(selectTheme),fillThemes(selectDashboardTheme,!0),userSettingsLoaded=!0,page.querySelector(".chkDisplayMissingEpisodes").checked=user.Configuration.DisplayMissingEpisodes||!1,page.querySelector("#chkThemeSong").checked=userSettingsInstance.enableThemeSongs(),page.querySelector("#selectBackdrop").value=appStorage.getItem("enableBackdrops-"+user.Id)||"0",page.querySelector("#selectLanguage").value=userSettingsInstance.language()||"",selectDashboardTheme.value=userSettingsInstance.dashboardTheme()||"",selectTheme.value=userSettingsInstance.appTheme()||"",loading.hide()})}function refreshGlobalUserSettings(){require(["userSettings"],function(userSettings){userSettings.importFrom(userSettingsInstance)})}function saveUser(page,user){return user.Configuration.DisplayMissingEpisodes=page.querySelector(".chkDisplayMissingEpisodes").checked,userSettingsLoaded&&(AppInfo.supportsUserDisplayLanguageSetting&&userSettingsInstance.language(page.querySelector("#selectLanguage").value),userSettingsInstance.enableThemeSongs(page.querySelector("#chkThemeSong").checked),userSettingsInstance.dashboardTheme(page.querySelector("#selectDashboardTheme").value),userSettingsInstance.appTheme(page.querySelector("#selectTheme").value),userId===Dashboard.getCurrentUserId()&&(skinManager.setTheme(userSettingsInstance.appTheme()||"dark"),refreshGlobalUserSettings())),appStorage.setItem("enableBackdrops-"+user.Id,page.querySelector("#selectBackdrop").value),ApiClient.updateUserConfiguration(user.Id,user.Configuration)}function save(page){AppInfo.enableAutoSave||loading.show(),ApiClient.getUser(userId).then(function(user){saveUser(page,user).then(function(){loading.hide(),AppInfo.enableAutoSave||require(["toast"],function(toast){toast(Globalize.translate("SettingsSaved"))})},function(){loading.hide()})})}var userSettingsLoaded,userId=params.userId||Dashboard.getCurrentUserId(),userSettingsInstance=new userSettingsBuilder;view.querySelector(".displayPreferencesForm").addEventListener("submit",function(e){return save(view),e.preventDefault(),!1}),AppInfo.enableAutoSave?view.querySelector(".btnSave").classList.add("hide"):view.querySelector(".btnSave").classList.remove("hide"),view.addEventListener("viewshow",function(){var page=this;loading.show(),ApiClient.getUser(userId).then(function(user){loadForm(page,user)}),AppInfo.supportsUserDisplayLanguageSetting?page.querySelector(".languageSection").classList.remove("hide"):page.querySelector(".languageSection").classList.add("hide")}),view.addEventListener("viewbeforehide",function(){var page=this;AppInfo.enableAutoSave&&save(page)})}});