define(["connectionManager", "actionsheet", "playbackManager", "globalize", "qualityoptions"], function(connectionManager, actionsheet, playbackManager, globalize, qualityoptions) {
	"use strict";

	function numberToString(value) {
		try {
			return new Intl.NumberFormat(globalize.getCurrentLocales(), {
				style: "decimal"
			}).format(value)
		} catch (err) {
			return console.log("Error in NumberFormat: " + err), value
		}
	}
	function showWithUser(options, player, user) {
		var currentAspectRatioId, currentAspectRatio, supportedCommands = playbackManager.getSupportedCommands(player),
			menuItems = []; - 1 !== supportedCommands.indexOf("SetAspectRatio") && (currentAspectRatioId = playbackManager.getAspectRatio(player), currentAspectRatio = playbackManager.getSupportedAspectRatios(player).filter(function(i) {
			return i.id === currentAspectRatioId
		})[0], menuItems.push({
			name: globalize.translate("HeaderAspectRatio"),
			id: "aspectratio",
			asideText: currentAspectRatio ? currentAspectRatio.name : null
		}));
		var secondaryQualityText, repeatMode, rate, currentSubtitleStream, currentMediaSource = playbackManager.currentMediaSource(player);
		return user && user.Policy.EnableVideoPlaybackTranscoding && currentMediaSource && currentMediaSource.SupportsTranscoding && -1 !== supportedCommands.indexOf("SetMaxStreamingBitrate") && (secondaryQualityText = function(player) {
			var videoStream = playbackManager.currentMediaSource(player).MediaStreams.filter(function(stream) {
				return "Video" === stream.Type
			})[0],
				videoWidth = videoStream ? videoStream.Width : null,
				options = qualityoptions.getVideoQualityOptions({
					currentMaxBitrate: playbackManager.getMaxStreamingBitrate(player),
					isAutomaticBitrateEnabled: playbackManager.enableAutomaticBitrateDetection(player),
					videoWidth: videoWidth,
					enableAuto: !0
				}),
				selectedOption = (options.map(function(o) {
					var opt = {
						name: o.name,
						id: o.bitrate,
						asideText: o.secondaryText
					};
					return o.selected && (opt.selected = !0), opt
				}), options.filter(function(o) {
					return o.selected
				}));
			if (!selectedOption.length) return null;
			var state, text = (selectedOption = selectedOption[0]).name;
			return selectedOption.autoText && ((state = playbackManager.getPlayerState(player)).PlayState && "Transcode" !== state.PlayState.PlayMethod ? text += " - &#x5A92;&#x4F53;&#x6E90;&#x76F4;&#x63A5;&#x4F20;&#x8F93;" : text += " - " + selectedOption.autoText), text
		}(player), menuItems.push({
			name: globalize.translate("Quality"),
			id: "quality",
			asideText: secondaryQualityText
		})), "Video" === options.mediaType && -1 !== supportedCommands.indexOf("SetRepeatMode") && currentMediaSource.RunTimeTicks && (repeatMode = playbackManager.getRepeatMode(player), menuItems.push({
			name: globalize.translate("HeaderRepeatMode"),
			id: "repeatmode",
			asideText: "RepeatNone" === repeatMode ? globalize.translate("None") : globalize.translate("" + repeatMode)
		})), -1 !== supportedCommands.indexOf("SetPlaybackRate") && currentMediaSource.RunTimeTicks && (rate = playbackManager.getPlaybackRate(player), menuItems.push({
			name: globalize.translate("Speed"),
			id: "speed",
			asideText: 1 === rate ? globalize.translate("Normal") : numberToString(rate)
		})), options.stats && player.isLocalPlayer && menuItems.push({
			name: globalize.translate("StatsForNerds"),
			id: "stats",
			asideText: null
		}), "Video" !== options.mediaType || -1 === supportedCommands.indexOf("SetSubtitleOffset") || (currentSubtitleStream = playbackManager.getSubtitleStream(player)) && currentSubtitleStream.IsExternal && ("External" === currentSubtitleStream.DeliveryMethod || "Hls" === currentSubtitleStream.DeliveryMethod) && menuItems.push({
			name: globalize.translate("HeaderSubtitleOffset"),
			id: "subtitleoffset",
			asideText: playbackManager.getSubtitleOffset(player) + " ms"
		}), actionsheet.show({
			items: menuItems,
			positionTo: options.positionTo,
			positionY: "top"
		}).then(function(id) {
			return function(id, options, player) {
				switch (id) {
				case "quality":
					return function(player, btn) {
						var videoStream = playbackManager.currentMediaSource(player).MediaStreams.filter(function(stream) {
							return "Video" === stream.Type
						})[0],
							videoWidth = videoStream ? videoStream.Width : null,
							options = qualityoptions.getVideoQualityOptions({
								currentMaxBitrate: playbackManager.getMaxStreamingBitrate(player),
								isAutomaticBitrateEnabled: playbackManager.enableAutomaticBitrateDetection(player),
								videoWidth: videoWidth,
								enableAuto: !0
							}),
							menuItems = options.map(function(o) {
								var opt = {
									name: o.name,
									id: o.bitrate,
									asideText: o.secondaryText
								};
								return o.selected && (opt.selected = !0), opt
							}),
							selectedId = (selectedId = options.filter(function(o) {
								return o.selected
							})).length ? selectedId[0].bitrate : null;
						return actionsheet.show({
							items: menuItems,
							positionTo: btn,
							positionY: "top",
							title: globalize.translate("Quality")
						}).then(function(id) {
							var bitrate = parseInt(id);
							bitrate !== selectedId && playbackManager.setMaxStreamingBitrate({
								enableAutomaticBitrateDetection: !bitrate,
								maxBitrate: bitrate
							}, player)
						})
					}(player, options.positionTo);
				case "aspectratio":
					return function(player, btn) {
						var currentId = playbackManager.getAspectRatio(player),
							menuItems = playbackManager.getSupportedAspectRatios(player).map(function(i) {
								return {
									id: i.id,
									name: i.name,
									selected: i.id === currentId
								}
							});
						return actionsheet.show({
							items: menuItems,
							positionTo: btn,
							positionY: "top",
							title: globalize.translate("HeaderAspectRatio")
						}).then(function(id) {
							return id ? (playbackManager.setAspectRatio(id, player), Promise.resolve()) : Promise.reject()
						})
					}(player, options.positionTo);
				case "repeatmode":
					return function(player, btn) {
						var menuItems = [],
							currentValue = playbackManager.getRepeatMode(player);
						return menuItems.push({
							name: globalize.translate("RepeatAll"),
							id: "RepeatAll",
							selected: "RepeatAll" === currentValue
						}), menuItems.push({
							name: globalize.translate("RepeatOne"),
							id: "RepeatOne",
							selected: "RepeatOne" === currentValue
						}), menuItems.push({
							name: globalize.translate("None"),
							id: "RepeatNone",
							selected: "RepeatNone" === currentValue
						}), actionsheet.show({
							items: menuItems,
							positionTo: btn,
							title: globalize.translate("HeaderRepeatMode")
						}).then(function(mode) {
							mode && playbackManager.setRepeatMode(mode, player)
						})
					}(player, options.positionTo);
				case "speed":
					return function(player, btn) {
						for (var menuItems = [], currentValue = playbackManager.getPlaybackRate(player), values = [.25, .5, .75, 1, 1.25, 1.5, 1.75, 2], i = 0, length = values.length; i < length; i++) {
							var value = values[i];
							menuItems.push({
								name: 1 === value ? globalize.translate("Normal") : numberToString(value),
								id: value.toString(),
								selected: currentValue === value
							})
						}
						return actionsheet.show({
							items: menuItems,
							positionTo: btn,
							positionY: "top",
							title: globalize.translate("Speed")
						}).then(function(result) {
							result && playbackManager.setPlaybackRate(result, player)
						})
					}(player, options.positionTo);
				default:
					return options.onOption && options.onOption(id), Promise.resolve()
				}
				return Promise.reject()
			}(id, options, player)
		})
	}
	return {
		show: function(options) {
			var player = options.player,
				currentItem = playbackManager.currentItem(player);
			return currentItem && currentItem.ServerId ? connectionManager.getApiClient(currentItem.ServerId).getCurrentUser().then(function(user) {
				return showWithUser(options, player, user)
			}) : showWithUser(options, player, null)
		}
	}
});
