AUI.add(

	'com-placecube-cookieconsent',

	function(A) {

		var PPID = 'com_placecube_cookieconsent_web_portlet_CookieConsentPortlet';

		var CookieConsent = {

			initCookieConsentForm: function() {

				$(document).on('click', '.btn-cookie-consent', function(e) {
					event.preventDefault();

					var consentResponse = $(e.target).attr('data-cookie-consent');

					var selected = $('input[type="radio"][name="measure-website-use"]:checked');
					if (selected.length > 0) {
						consentResponse = selected.val();
					}
					A.CookieConsent.updateConsent(consentResponse);

				});
			},
			
			configureDoNotTrack: function(cookiesAccepted){
				var doNotTrack = cookiesAccepted == 'false';
				window['ac_client_disable_tracking'] = doNotTrack;
			},

			updateConsent: function(consentResponse) {

				var url = Liferay.PortletURL.createResourceURL();
				url.setPortletId(PPID);
				url.setResourceId("/cookie-consent/update-consent");
				url.setParameter("consentGiven", consentResponse);

				A.io.request(url.toString(), {

					on: {

						success: function() {
							A.CookieConsent.configureDoNotTrack(consentResponse);
							if($(".cookie-consent-updated-banner").length > 0){
								$(".cookie-consent-updated-banner").show();
								$(window).scrollTop(0);
							} else {
								location.reload();
							}
						}

					}

				});

			},
			onFocusCookieHideAction: function(namespace){
				$('#' + namespace + 'cookie-consent-close').on('keydown', function(e) {
					if(e.keyCode == 13) {
						$('.cookie-consent__modal-mode').addClass('hide');
						sessionStorage.setItem("isCookieModalClosed", true);
					}

				})
				let isCookieModalClosed = (sessionStorage.getItem("isCookieModalClosed") === 'true');

				if (!isCookieModalClosed) {
					$('.cookie-consent__modal-mode').removeClass('hide');
				}

			},

			initCookieHideAction: function(namespace){
				$('#' + namespace + 'cookie-consent-close').on('click', function() {
					$('.cookie-consent__modal-mode').addClass('hide');
					sessionStorage.setItem("isCookieModalClosed", true);
				})

				let isCookieModalClosed = (sessionStorage.getItem("isCookieModalClosed") === 'true');

				if (!isCookieModalClosed) {
					$('.cookie-consent__modal-mode').removeClass('hide');
				}
			}

		};

		A.CookieConsent = CookieConsent;
	},
	'',
	{
		requires: ['aui-io', 'liferay-portlet-url']
	}
);


function addUserFeedbackEventListener(portletId, resourceCommandName, nameSpace){
	$('#portlet_' + portletId + ' .ccew-user-feedback-button').on('click', function(){
		saveUserFeedback(portletId, resourceCommandName, this.getAttribute('ccew-data-feedback'), nameSpace)
	});
}

function saveUserFeedback(portletId, resourceCommandName, feedbackValue, nameSpace){

		
	AUI().use('aui-io-request','liferay-portlet-url', function(A){
		let url = Liferay.PortletURL.createResourceURL();
		let currentURL = Liferay.currentURL;
		url.setParameter("feedbackValue", feedbackValue);
		url.setParameter("currentURL", currentURL);
	
		url.setPortletId(portletId);
		url.setResourceId(resourceCommandName);
	
		A.io.request(url.toString(), {
			method: 'post',
			on: {
					success: function() {
						showMessage();
				}
			}
		});
	});
}

function showMessage(){
	$('.ccew-user-feedback-icons-wrapper').hide();

	$('.ccew-user-feedback-article').removeClass('hide');
}
