<?php
add_filter( 'wp_enqueue_scripts', 'my_login_hook' );
function my_login_hook() {
	if(page_id() === 222) {
		$url = current_url();
		if (basename($url) === 'login') {
			redirect('http://zinomedia.de/terminsnipe/?login');
		}
		else if (basename($url) === 'register') {
			redirect('http://zinomedia.de/terminsnipe/?register');
		}

		if(isset($_GET["login"])) {
			if ( is_user_logged_in() ) {
				redirect('http://zinomedia.de/terminsnipe/dash/account/');
			}			
			else {
				my_login_modal($url);
				clickModal('#login-modal');
				remove_url_parameter();
			}				
		}
		else if(isset($_GET["register"])) {
			if (is_user_logged_in()) {
				redirect('http://zinomedia.de/terminsnipe/dash/account/');
			}				
			else {
				clickModal('#register-modal');
			}				
		}
		else if(isset($_GET["frlo"])) {		
			clickModal('#frlo-modal');
			remove_url_parameter();
		}
		else if(isset($_GET["ca"])) {		
			clickModal('#created-appointment-modal');
			remove_url_parameter();
		}
		else if(isset($_GET["snake"])) {		
			clickModal('#game-snake-modal');
			remove_url_parameter();
		}
		else if(isset($_GET["fa"])) {		
			clickModal('#found-appointment-modal');
			remove_url_parameter();
			my_js_confetti();
		}
		else if(isset($_GET["ty"])) {		
			clickModal('#thank-you-modal');
			remove_url_parameter();
		}
		else if(isset($_GET["im"])) {		
			clickModal('#impressum-modal');
			remove_url_parameter();
		}
		else if(isset($_GET["uccr"])) {
			my_uccr();
			clickModal('#cancellation-refund-modal');
			remove_url_parameter();
		}
		else if(isset($_GET["kontakt"])) {		
			clickModal('#kontakt-modal');
			remove_url_parameter();
		}
		else if(isset($_GET["fcs"])) {
			my_fcs_js_manipulate();
			clickModal('#fcs-modal');			
			remove_url_parameter();
			my_js_confetti();
		}
	}
}

function my_js_confetti() {
	?>
	<script>
	// START JAVASCRIPT		
		document.addEventListener('DOMContentLoaded', function() {
			wait_for_element('confettijs', 'makeItRain', null);
		}, false);		
	// END JAVASCRIPT
	</script>				
	<?php
}

function my_fcs_js_manipulate() {
	?>
	<script>
	// START JAVASCRIPT		
		document.addEventListener('DOMContentLoaded', function() {
			jQuery( "#neue-terminsuche" ).click(function() {				
				jQuery('.uabb-modal-close').click();
			});
		}, false);		
	// END JAVASCRIPT
	</script>				
	<?php
}

function my_login_modal($url) {
	$parts = parse_url($url);
	parse_str($parts['query'], $query);
	
	if(	$query['error'] ) {
		my_login_modal_js_manipulate($query['error']);
	}
}

function my_login_modal_js_manipulate($error) {
	?>
	<script>
	// START JAVASCRIPT		
		document.addEventListener('DOMContentLoaded', function() {
			let error= "<?php echo $error ?>";
			console.log('Error received: ' + error);			
			if( error == 'ERROR: Your username or password was incorrect' )
				error = 'FEHLER: Deine E-Mail oder dein Passwort sind inkorrekt.';				
			
			jQuery('#login-modal-overlay .mp_login_form').prepend('<div class="mp_wrapper"><div class="mepr_error" id="mepr_jump">' + error + '</div></div>');		
		}, false);				
	// END JAVASCRIPT
	</script>				
	<?php
}

function my_uccr() {
	if(	isset($_GET['price']) && isset($_GET['bbr']) ) {
		$balance = mycred_return_users_balance();
		my_uccr_js_manipulate($_GET['price'], $balance, $_GET['bbr']);
	}
}

function my_uccr_js_manipulate($price, $balance, $balance_before) {
	?>
	<script>
	// START JAVASCRIPT		
		document.addEventListener('DOMContentLoaded', function() {
			let price= "<?php echo $price ?>";
			let balance = "<?php echo $balance ?>";
			let balance_before = "<?php echo $balance_before ?>";
			jQuery('#balance-before')[0].textContent = balance_before;
			jQuery('#refund-amount')[0].textContent = price;
			jQuery('#user-balance')[0].textContent = balance;			
			jQuery( "#neue-terminsuche-starten" ).click(function() {				
				jQuery('.uabb-modal-close').click();
			});		
		}, false);				
	// END JAVASCRIPT
	</script>				
	<?php
}

function clickModal($modal) {
	?>
	<script>
	// START JAVASCRIPT
		document.addEventListener('DOMContentLoaded', function() { 
			jQuery("<?php echo $modal ?>").ready(function() {
				jQuery("<?php echo $modal ?>").click()
			});				
		}, false);				
	// END JAVASCRIPT
	</script>				
	<?php
}

