jQuery(function () {
	let samsMobileNavigation = new SamsMobileNavigation();
	samsMobileNavigation.init();

});

class SamsMobileNavigation {
	constructor() {
		let categoryNavigationListItemSelector = 'ul#categoryNavigation>li';
		this.$categoryNavigationWrapper = jQuery('#categoryNavigationWrapper');
		this.$subCategoryListItems = jQuery(categoryNavigationListItemSelector);
		let categorySubNavigationListSelector = categoryNavigationListItemSelector + '>.subCategoryWrapper>ul.subCategoryList';

		this.$hideWhenSmallScreenNavigationIsShown = jQuery('#innerPageWrapper, #footerWrapper');
		this.$categoryNavigationLinks = jQuery(categoryNavigationListItemSelector + '>.categoryTitle');
		this.$categorySubNavigationLists = jQuery(categorySubNavigationListSelector);
		this.$burger = jQuery('#responsiveBurger');
		this.currentScreenSizeType = null;
	}

	init() {
		this.currentScreenSizeType = this.queryCurrentScreenSizeType();
		this.initEvents();
		if (this.currentScreenSizeType === 'SMALL') {
			this.$subCategoryListItems.filter('.active').addClass('visible');
		}
	};

	initEvents() {
		jQuery(window).resize(this.onResize.bind(this));

		this.$burger.click(this.onResponsiveBurgerClick.bind(this));
		this.$categoryNavigationLinks.click(this.onCategoryNavigationLinkClick.bind(this));
		jQuery('body').click(this.onBodyClick.bind(this));
	};

	onBodyClick(e) {
		if (this.$categoryNavigationWrapper.is('.static') && !jQuery(e).closest('#categoryNavigationWrapper').length) {
			// hide if clicked outside static wrapper
			this.$categoryNavigationWrapper.removeClass('static');
		}
	}

	onCategoryNavigationLinkClick(e) {
		let $target = jQuery(e.currentTarget);
		let $subNav = $target.siblings('.subCategoryWrapper').find('>ul');
		if (!$subNav.length || !$subNav.children().length) {
			return true; // no or empty subnav
		}
		if ($subNav.length && $subNav.children().length && $subNav.closest('li').is('.visible')) {
			return true; // already statically shown - allow second click to go through
		}

		this.hideAllSubNavigation();
		this.showSubNav($subNav);
		this.$categoryNavigationWrapper.addClass('static');
		return false;
	};

	showSubNav($subNav) {
		$subNav.closest('li').addClass('visible');
	}


	hideAllSubNavigation() {
		this.$subCategoryListItems.removeClass('visible');
	};

	onResponsiveBurgerClick(e) {
		let $wrapper = this.$categoryNavigationWrapper;
		if ($wrapper.is(':visible')) {
			$wrapper.hide();
			$wrapper.removeClass('static active');
			this.$burger.removeClass('active');
			this.$hideWhenSmallScreenNavigationIsShown.show();
		} else {
			this.$burger.addClass('active');
			$wrapper.addClass('static active');
			$wrapper.show();
			this.$hideWhenSmallScreenNavigationIsShown.hide();
		}
		e.stopPropagation();
		return false;
	};

	onResize() {
		let newScreenSizeType = this.queryCurrentScreenSizeType();
		if (this.currentScreenSizeType !== newScreenSizeType) {
			if (newScreenSizeType == 'FULL') {
				this.hideAllSubNavigation();
			} else {

			}
		}
		this.currentScreenSizeType = newScreenSizeType;
	};

	queryCurrentScreenSizeType() {
		return this.queryIfSmallScreen() ? 'SMALL' : 'FULL';
	};

	queryIfSmallScreen() {
		return this.$burger.is(':visible');
	};
};
