(function($) {
	'use strict';

	var pageUrl = typeof evgeOnboarding !== 'undefined' ? evgeOnboarding.page_url : '';
	var ajaxUrl = typeof evgeOnboarding !== 'undefined' ? evgeOnboarding.ajax_url : '';
	var nonce   = typeof evgeOnboarding !== 'undefined' ? evgeOnboarding.nonce : '';
	var strings = typeof evgeOnboarding !== 'undefined' ? evgeOnboarding.strings : {};

	var SPINNER_HTML = '<div class="evge-spinner-container"><div class="evge-spinner-circle"></div></div>';

	function startProcessing($btn) {
		if (!$btn || !$btn.length) return;
		$btn.attr('aria-busy', 'true');
		$btn.wrap('<div class="evge-processing-wrap-flex evge-is-processing"></div>');
		$btn.addClass('evge-fade').prop('disabled', true);
		$btn.before(SPINNER_HTML);
	}

	function stopProcessing($btn) {
		if (!$btn || !$btn.length) return;
		setTimeout(function() {
			var $wrap = $btn.closest('.evge-processing-wrap-flex');
			if ($wrap.length) {
				$wrap.find('.evge-spinner-container').remove();
				$btn.unwrap('.evge-processing-wrap-flex');
			}
			$btn.removeClass('evge-fade').prop('disabled', false).attr('aria-busy', 'false');
		}, 100);
	}

	function setMessage($el, text, isError) {
		$el.removeClass('evge-success evge-error').addClass(isError ? 'evge-error' : 'evge-success').text(text).show();
	}

	function goToStep(step) {
		if (pageUrl) {
			window.location.href = pageUrl + '&step=' + step;
		}
	}

	$(document).on('click', '.evge-onboarding-btn-continue', function(e) {
		e.preventDefault();
		var href = $(this).attr('href');
		if (href) {
			window.location.href = href;
		}
	});

	$(document).on('click', '.evge-onboarding-create-event', function() {
		var title = $('#evge-onboarding-event-title').val();
		title = (title && title.trim()) ? title.trim() : '';
		var date     = $('#evge-onboarding-event-start-date').val();
		var time     = $('#evge-onboarding-event-start-time').val();
		var endDate  = $('#evge-onboarding-event-end-date').val();
		var endTime  = $('#evge-onboarding-event-end-time').val();
		if (!date) {
			var t = new Date();
			t.setDate(t.getDate() + 1);
			date = t.toISOString().slice(0, 10);
		}
		if (!time) time = '09:00';
		if (!endDate) endDate = date;
		if (!endTime) endTime = '17:00';
		var timeFull   = time.length === 5 ? time + ':00' : time;
		var endTimeFull = endTime.length === 5 ? endTime + ':00' : endTime;
		var $btn = $(this);
		var $msg = $('.evge-onboarding-create-event-footer').next('.evge-onboarding-ajax-message');
		startProcessing($btn);
		setMessage($msg, strings.creating || 'Creating event…', false);
		$.post(ajaxUrl, {
			action: 'evge_onboarding_create_event',
			nonce: nonce,
			title: title,
			date: date,
			time: timeFull,
			end_date: endDate,
			end_time: endTimeFull
		}).done(function(res) {
			if (res.success) {
				setMessage($msg, strings.success || 'Success!', false);
				setTimeout(function() { goToStep(3); }, 800);
			} else {
				setMessage($msg, (res.data && res.data.message) || strings.error, true);
				stopProcessing($btn);
			}
		}).fail(function() {
			setMessage($msg, strings.error, true);
			stopProcessing($btn);
		});
	});

})(jQuery);
