define(["require", "browser", "layoutManager", "focusManager", "globalize", "loading", "connectionManager", "homeSections", "dom", "events", "listViewStyle", "emby-select", "emby-checkbox"], function(require, browser, layoutManager, focusManager, globalize, loading, connectionManager, homeSections, dom, events) {
	"use strict";
	var numConfigurableSections = 7;

	function getLandingScreenOptionsHtml(type, userValue) {
		return function(type) {
			var list = [];
			return "movies" === type ? (list.push({
				name: globalize.translate("Movies"),
				value: "movies",
				isDefault: !0
			}), list.push({
				name: globalize.translate("Suggestions"),
				value: "suggestions"
			}), list.push({
				name: globalize.translate("Favorites"),
				value: "favorites"
			}), list.push({
				name: globalize.translate("Genres"),
				value: "genres"
			}), list.push({
				name: globalize.translate("Collections"),
				value: "collections"
			}), list.push({
				name: globalize.translate("Folders"),
				value: "folders"
			})) : "musicvideos" === type ? (list.push({
				name: globalize.translate("Videos"),
				value: "videos",
				isDefault: !0
			}), list.push({
				name: globalize.translate("Folders"),
				value: "folders"
			})) : "tvshows" === type ? (list.push({
				name: globalize.translate("Shows"),
				value: "shows",
				isDefault: !0
			}), list.push({
				name: globalize.translate("Suggestions"),
				value: "suggestions"
			}), list.push({
				name: globalize.translate("Favorites"),
				value: "favorites"
			}), list.push({
				name: globalize.translate("Genres"),
				value: "genres"
			}), list.push({
				name: globalize.translate("Folders"),
				value: "folders"
			})) : "music" === type || "audiobooks" === type ? (list.push({
				name: globalize.translate("Suggestions"),
				value: "suggestions",
				isDefault: !0
			}), list.push({
				name: globalize.translate("Albums"),
				value: "albums"
			}), list.push({
				name: globalize.translate("HeaderAlbumArtists"),
				value: "albumartists"
			}), list.push({
				name: globalize.translate("Artists"),
				value: "artists"
			}), list.push({
				name: globalize.translate("Playlists"),
				value: "playlists"
			}), list.push({
				name: globalize.translate("Genres"),
				value: "genres"
			}), list.push({
				name: globalize.translate("Folders"),
				value: "folders"
			})) : "livetv" === type && (list.push({
				name: globalize.translate("Suggestions"),
				value: "suggestions",
				isDefault: !0
			}), list.push({
				name: globalize.translate("Guide"),
				value: "guide"
			}), list.push({
				name: globalize.translate("Channels"),
				value: "channels"
			})), list
		}(type).map(function(o) {
			var selectedHtml = userValue === o.value || o.isDefault && !userValue ? " selected" : "";
			return '<option value="' + (o.isDefault ? "" : o.value) + '"' + selectedHtml + ">" + o.name + "</option>"
		}).join("")
	}
	function renderPerLibrarySettings(context, user, userViews, userSettings) {
		for (var elem = context.querySelector(".perLibrarySettings"), html = "", i = 0, length = userViews.length; i < length; i++) html +=
		function(item, user, userSettings) {
			var isChecked, idForLanding, userValue, prefix, html = "";
			return "Channel" !== item.Type && "boxsets" !== item.CollectionType && "playlists" !== item.CollectionType || (isChecked = -1 === (user.Configuration.MyMediaExcludes || []).indexOf(item.Id), html += "<div>", html += "<label>", html += '<input type="checkbox" is="emby-checkbox" class="chkIncludeInMyMedia" data-folderid="' + item.Id + '"' + (isChecked ? ' checked="checked"' : "") + "/>", html += "<span>" + globalize.translate("DisplayInMyMedia") + "</span>", html += "</label>", html += "</div>"), -1 === ["playlists", "livetv", "boxsets", "channels"].indexOf(item.CollectionType || "") && (isChecked = -1 === user.Configuration.LatestItemsExcludes.indexOf(item.Id), html += '<label class="fldIncludeInLatest">', html += '<input type="checkbox" is="emby-checkbox" class="chkIncludeInLatest" data-folderid="' + item.Id + '"' + (isChecked ? ' checked="checked"' : "") + "/>", html += "<span>" + globalize.translate("DisplayInOtherHomeScreenSections") + "</span>", html += "</label>"), html = html && '<div class="checkboxListContainer">' + html + "</div>", "movies" !== item.CollectionType && "tvshows" !== item.CollectionType && "musicvideos" !== item.CollectionType && "music" !== item.CollectionType && "audiobooks" !== item.CollectionType && "livetv" !== item.CollectionType || (html += '<div class="selectContainer">', html += '<select is="emby-select" class="selectLanding" data-folderid="' + (idForLanding = "livetv" === item.CollectionType ? item.CollectionType : item.Id) + '" label="' + globalize.translate("LabelDefaultScreen") + '">', userValue = userSettings.get("landing-" + idForLanding), html += getLandingScreenOptionsHtml(item.CollectionType, userValue), html += "</select>", html += "</div>"), html && (prefix = "", prefix += '<div class="verticalSection">', prefix += '<h2 class="sectionTitle">', prefix += item.Name, html = (prefix += "</h2>") + html, html += "</div>"), html
		}(userViews[i], user, userSettings);
		elem.innerHTML = html
	}
	function loadForm(context, user, userSettings, apiClient) {
		context.querySelector(".chkHidePlayedFromLatest").checked = user.Configuration.HidePlayedInLatest || !1, function(context, userSettings) {
			for (var i = 1; i <= 7; i++) {
				var select = context.querySelector(".selectHomeSection" + i),
					defaultValue = homeSections.getDefaultSection(i - 1),
					option = select.querySelector("option[value=" + defaultValue + "]") || select.querySelector('option[value=""]'),
					userValue = userSettings.get("homesection" + (i - 1));
				option.value = "", select.value = userValue !== defaultValue && userValue ? userValue : ""
			}
			context.querySelector(".selectTVHomeScreen").value = userSettings.get("tvhome") || ""
		}(context, userSettings);
		var promise1 = apiClient.getUserViews({
			IncludeHidden: !0
		}, user.Id);
		Promise.all([promise1]).then(function(responses) {
			!
			function(context, result) {
				var html = "";
				html += result.Items.map(function(view) {
					var currentHtml = "";
					return currentHtml += '<div class="listItem viewItem" data-viewid="' + view.Id + '">', currentHtml += '<i class="md-icon listItemIcon">&#xE2C8;</i>', currentHtml += '<div class="listItemBody">', currentHtml += "<div>", currentHtml += view.Name, currentHtml += "</div>", currentHtml += "</div>", currentHtml += '<button type="button" is="paper-icon-button-light" class="btnViewItemUp btnViewItemMove autoSize" title="' + globalize.translate("Up") + '"><i class="md-icon">&#xE316;</i></button>', currentHtml += '<button type="button" is="paper-icon-button-light" class="btnViewItemDown btnViewItemMove autoSize" title="' + globalize.translate("Down") + '"><i class="md-icon">&#xE313;</i></button>', currentHtml += "</div>"
				}).join(""), context.querySelector(".viewOrderList").innerHTML = html
			}(context, responses[0]), renderPerLibrarySettings(context, user, responses[0].Items, userSettings), loading.hide()
		})
	}
	function onSectionOrderListClick(e) {
		var viewItem, next, prev, target = e.target.closest(".btnViewItemMove");
		!target || (viewItem = target.closest(".viewItem")) && (viewItem.closest(".paperList"), target.classList.contains("btnViewItemDown") ? (next = viewItem.nextSibling) && (viewItem.parentNode.removeChild(viewItem), next.parentNode.insertBefore(viewItem, next.nextSibling)) : (prev = viewItem.previousSibling) && (viewItem.parentNode.removeChild(viewItem), prev.parentNode.insertBefore(viewItem, prev)))
	}
	function getCheckboxItems(selector, context, isChecked) {
		for (var inputs = context.querySelectorAll(selector), list = [], i = 0, length = inputs.length; i < length; i++) inputs[i].checked === isChecked && list.push(inputs[i]);
		return list
	}
	function save(instance, context, userId, userSettings, apiClient, enableSaveConfirmation) {
		loading.show(), apiClient.getUser(userId).then(function(user) {
			(function(context, user, userSettingsInstance, apiClient) {
				user.Configuration.HidePlayedInLatest = context.querySelector(".chkHidePlayedFromLatest").checked, user.Configuration.LatestItemsExcludes = getCheckboxItems(".chkIncludeInLatest", context, !1).map(function(i) {
					return i.getAttribute("data-folderid")
				}), user.Configuration.MyMediaExcludes = getCheckboxItems(".chkIncludeInMyMedia", context, !1).map(function(i) {
					return i.getAttribute("data-folderid")
				});
				for (var viewItems = context.querySelectorAll(".viewItem"), orderedViews = [], i = 0, length = viewItems.length; i < length; i++) orderedViews.push(viewItems[i].getAttribute("data-viewid"));
				user.Configuration.OrderedViews = orderedViews, userSettingsInstance.set("tvhome", context.querySelector(".selectTVHomeScreen").value), userSettingsInstance.set("homesection0", context.querySelector(".selectHomeSection1").value), userSettingsInstance.set("homesection1", context.querySelector(".selectHomeSection2").value), userSettingsInstance.set("homesection2", context.querySelector(".selectHomeSection3").value), userSettingsInstance.set("homesection3", context.querySelector(".selectHomeSection4").value), userSettingsInstance.set("homesection4", context.querySelector(".selectHomeSection5").value), userSettingsInstance.set("homesection5", context.querySelector(".selectHomeSection6").value), userSettingsInstance.set("homesection6", context.querySelector(".selectHomeSection7").value);
				var selectLandings = context.querySelectorAll(".selectLanding");
				for (i = 0, length = selectLandings.length; i < length; i++) {
					var selectLanding = selectLandings[i];
					userSettingsInstance.set("landing-" + selectLanding.getAttribute("data-folderid"), selectLanding.value)
				}
				return apiClient.updateUserConfiguration(user.Id, user.Configuration)
			})(context, user, userSettings, apiClient).then(function() {
				loading.hide(), enableSaveConfirmation && require(["toast"], function(toast) {
					toast(globalize.translate("SettingsSaved"))
				}), events.trigger(instance, "saved")
			}, function() {
				loading.hide()
			})
		})
	}
	function onSubmit(e) {
		var self = this,
			apiClient = connectionManager.getApiClient(self.options.serverId),
			userId = self.options.userId,
			userSettings = self.options.userSettings;
		return userSettings.setUserInfo(userId, apiClient).then(function() {
			var enableSaveConfirmation = self.options.enableSaveConfirmation;
			save(self, self.options.element, userId, userSettings, apiClient, enableSaveConfirmation)
		}), e && e.preventDefault(), !1
	}
	function onChange(e) {
		var fldIncludeInLatest, chkIncludeInMyMedia = e.target.closest(".chkIncludeInMyMedia");
		!chkIncludeInMyMedia || (fldIncludeInLatest = chkIncludeInMyMedia.closest(".verticalSection").querySelector(".fldIncludeInLatest")) && (chkIncludeInMyMedia.checked ? fldIncludeInLatest.classList.remove("hide") : fldIncludeInLatest.classList.add("hide"))
	}
	function HomeScreenSettings(options) {
		!
		function(options, self) {
			for (var i = 1; i <= numConfigurableSections; i++) options.element.querySelector(".selectHomeSection" + i).setLabel(globalize.translate("LabelHomeScreenSectionValue", i));
			options.element.querySelector(".viewOrderList").addEventListener("click", onSectionOrderListClick), options.element.querySelector("form").addEventListener("submit", onSubmit.bind(self)), options.element.addEventListener("change", onChange), options.enableSaveButton && options.element.querySelector(".btnSave").classList.remove("hide"), layoutManager.tv && !browser.netcast ? options.element.querySelector(".selectTVHomeScreenContainer").classList.remove("hide") : options.element.querySelector(".selectTVHomeScreenContainer").classList.add("hide"), self.loadData(options.autoFocus)
		}(this.options = options, this)
	}
	return HomeScreenSettings.prototype.loadData = function(autoFocus) {
		var context = this.options.element;
		loading.show();
		var userId = this.options.userId,
			apiClient = connectionManager.getApiClient(this.options.serverId),
			userSettings = this.options.userSettings;
		apiClient.getUser(userId).then(function(user) {
			(userId === apiClient.getCurrentUserId() ? Promise.resolve() : userSettings.setUserInfo(userId, apiClient)).then(function() {
				loadForm(context, user, userSettings, apiClient), autoFocus && focusManager.autoFocus(context)
			})
		})
	}, HomeScreenSettings.prototype.submit = function() {
		onSubmit.call(this)
	}, HomeScreenSettings.prototype.destroy = function() {
		this.options = null
	}, HomeScreenSettings
});
