// source --> https://blomstrandeting.se/wp-content/plugins/woocommerce-gateway-swish-ecommerce/assets/js/checkout.js?ver=7.0.9 
jQuery(document).ready(function($) {

var redlight_swish_ecommerce_payment_check = {

	check_payment_status: true,
	showPopUp: function () {
		if (!$('redlight-swish-popup_wraper').hasClass('active')) {
			$('redlight-swish-popup_wraper').addClass('active').fadeIn('slow');
		}
	},

	checkPayment: function () {
		$.ajax({
			type: 'POST',
			url: swish_params.swish_ajax,
			data: {
				action: 'swish_ajax_payment_check',
				order_id: swish_params.swish_order_id,
				nonce: swish_params.nonce,
			},
			success: function (data) {
				//console.log(order_details);
				data = JSON.parse(data);
				console.log(data)
				if (data.result == "success") {
					$('.swish-button').hide()
					redlight_swish_ecommerce_payment_check.check_payment_status = false;
					var html = '<div class="swish-info">'+ swish_params.payment_succesful + '</div>';
					html += '<div class="woocommerce-info">'+ swish_params.succesful_payment_redirect_message +'</div>';
					$('#redlight-swish-popup_wraper .redlight-swish-popup_message').html(html);
					setTimeout(function () {
						window.location.href = data.redirect;
					}, 5000);
				} else if (data.result == "error") {
					$('.swish-button').hide()
					if (!$('#redlight-swish-popup_wraper .woocommerce-info').length) {
						redlight_swish_ecommerce_payment_check.check_payment_status = false;
						var html = '<div class="swish-info">'+swish_params.payment_failed + '</div> <a class="button" href="'+ swish_params.swish_shop_url +'">' + swish_params.return_to_checkout + '</a>';
						html += '<div class="woocommerce-info">'+ swish_params.redirect_message + ' <span class="count">10</span> '+ swish_params.seconds +'</div>';
						$('#redlight-swish-popup_wraper .redlight-swish-popup_message').html(html);
						redlight_swish_ecommerce_payment_check.count_start();
					} else {
						console.log('Something happend', data);
					}
				} else {
					if (data.errorCode && data.errorMessage) {

						redlight_swish_ecommerce_payment_check.check_payment_status = false;
						if ($('#redlight-swish-popup_wraper .woocommerce-error').length) {
							$('#redlight-swish-popup_wraper .woocommerce-error').text(order_details.body.errorMessage);
						} else {
							var html = '<div class="woocommerce-error">' + order_details.body.errorMessage + '</div>';
							$('#redlight-swish-popup_wraper .redlight-swish-popup_message').html(html);
						}

						var html = '<h5 class="return-to-shop"><a class="button" href="' + swish_params.swish_shop_url + '">' + swish_params.return_to_checkout + '</a></h5>';
						html += '<div class="woocommerce-info">' + swish_params.redirect_message + ' <span class="count">10</span> ' + swish_params.seconds + '</div>';
						$('#redlight-swish-popup_wraper .woocommerce-error').after(html);
						redlight_swish_ecommerce_payment_check.count_start();

						/*setTimeout(function(){
							$('#popup_wraper').removeClass('active').css('display', 'none');
						}, 20000)*/
					} else {
						$('#redlight-swish-popup_wraper .woocommerce-error').remove();
						redlight_swish_ecommerce_payment_check.check_payment_status = true;
						$('#redlight-swish-popup_wraper').addClass('active').css('display', 'block');
					}
				}
			},
		});

	},

	count_start: function () {

		redlight_swish_ecommerce_payment_check.showPopUp();

		var start_from = 10,
			ends_to = 0,
			decrement = 1,
			redirect_url = swish_params.swish_shop_url;

		var interval_id = setInterval(function () {
			if (start_from <= ends_to) {
				if (redirect_url) {
					window.location.href = redirect_url;
					clearInterval(interval_id);
				}
				return;
			}
			start_from -= decrement;
			$('#redlight-swish-popup_wraper .woocommerce-info .count').text(start_from);
			console.log(start_from);
		}, 1000);

	},

	managePopup: function () {
		if(redlight_swish_ecommerce_payment_check.ongoingTransaction()){
			redlight_swish_ecommerce_payment_check.showPopUp();
			redlight_swish_ecommerce_payment_check.checkPayment();
		}
	},

	swish_event_list: function () {

		/*updated checkout*/
		jQuery(document).on('updated_checkout', function () {
			redlight_swish_ecommerce_payment_check.managePopup();
		});

	},
	ongoingTransaction: function () {

		var retVal = false;

		if (!redlight_swish_ecommerce_payment_check.check_payment_status) {
			return retVal;
		}

		if (typeof swish_params.swish_order_id === "string" && swish_params.swish_order_id != "0") {
			retVal = true;
		}
		return retVal;
	},

	payment_check: function () {
		setInterval(function () {
			if (redlight_swish_ecommerce_payment_check.ongoingTransaction()) {
				redlight_swish_ecommerce_payment_check.managePopup();
			}
		}, 3000);
	},

	init: function () {
		redlight_swish_ecommerce_payment_check.swish_event_list();
		redlight_swish_ecommerce_payment_check.managePopup();
		redlight_swish_ecommerce_payment_check.payment_check();
	},
}

redlight_swish_ecommerce_payment_check.init();
});