<%--
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */
--%>

<%@ include file="/init.jsp" %>

<c:choose>
	<c:when test="<%= themeDisplay.isSignedIn() %>">
		<span class="user-avatar-link">
			<liferay-product-navigation:personal-menu
				size="lg"
				user="<%= user %>"
			/>

			<%
			int notificationsCount = GetterUtil.getInteger(request.getAttribute(ProductNavigationUserPersonalBarWebKeys.NOTIFICATIONS_COUNT));
			%>

			<c:if test="<%= notificationsCount > 0 %>">

				<%
				String notificaitonsPortletId = PortletProviderUtil.getPortletId(UserNotificationEvent.class.getName(), PortletProvider.Action.VIEW);

				String notificationsURL = PersonalApplicationURLUtil.getPersonalApplicationURL(request, notificaitonsPortletId);
				%>

				<aui:a href="<%= (notificationsURL != null) ? notificationsURL : null %>">
					<clay:badge
						cssClass="panel-notifications-count"
						displayType="danger"
						label="<%= String.valueOf(notificationsCount) %>"
					/>
				</aui:a>
			</c:if>
		</span>
	</c:when>
	<c:otherwise>
		<span class="sign-in text-default" role="presentation">
			<aui:icon
				cssClass="sign-in text-default"
				data='<%=
					HashMapBuilder.<String, Object>put(
						"redirect", String.valueOf(PortalUtil.isLoginRedirectRequired(request))
					).build()
				%>'
				image="user"
				label="sign-in"
				markupView="lexicon"
				url="<%= themeDisplay.getURLSignIn() %>"
			/>
		</span>

		<aui:script sandbox="<%= true %>">
			var signInLink = document.querySelector('.sign-in > a');

			if (signInLink && signInLink.dataset.redirect === 'false') {
				var signInURL = '<%= themeDisplay.getURLSignIn() %>';

				var modalSignInURL = Liferay.Util.addParams(
					'windowState=exclusive',
					signInURL
				);

				var setModalContent = function (html) {
					var modalBody = document.querySelector('.liferay-modal-body');

					if (modalBody) {
						var fragment = document
							.createRange()
							.createContextualFragment(html);

						modalBody.innerHTML = '';

						modalBody.appendChild(fragment);
					}
				};

				var loading = false;
				var redirect = false;
				var html = '';
				var modalOpen = false;

				var fetchModalSignIn = function () {
					if (loading || html) {
						return;
					}

					loading = true;

					Liferay.Util.fetch(modalSignInURL)
						.then(function (response) {
							return response.text();
						})
						.then(function (response) {
							if (!loading) {
								return;
							}

							loading = false;

							if (!response) {
								redirect = true;

								return;
							}

							html = response;

							if (modalOpen) {
								setModalContent(response);
							}
						})
						.catch(function () {
							redirect = true;
						});
				};

				signInLink.addEventListener('mouseover', fetchModalSignIn);
				signInLink.addEventListener('focus', fetchModalSignIn);

				signInLink.addEventListener('click', function (event) {
					event.preventDefault();

					if (redirect) {
						Liferay.Util.navigate(signInURL);

						return;
					}

					Liferay.Util.openModal({
						bodyHTML: html ? html : '<span class="loading-animation">',
						height: '400px',
						onClose: function () {
							loading = false;
							redirect = false;
							html = '';
							modalOpen = false;
						},
						onOpen: function () {
							modalOpen = true;

							if (html && document.querySelector('.loading-animation')) {
								setModalContent(html);
							}
						},
						size: 'md',
						title: '<liferay-ui:message key="sign-in" />',
					});
				});
			}
		</aui:script>
	</c:otherwise>
</c:choose>