let CompreJunto = (function() {
	"use strict";

	let DOM = {};

	function getUrlBase(url) {
		myurl = (window.location);
		splited_url = (myurl.toString().split("/"));
		final_url = splited_url[0] + "//" + splited_url[2];
		if (isLocalhost(DOM.$modulo.ownerDocument .location.hostname)) {
			final_url = splited_url[0] + "//" + splited_url[2] + "/" + splited_url[3];
		} else {
			final_url = splited_url[0] + "//" + splited_url[2];
		}

		return final_url + "/" + url;
	};


	function isLocalhost(hostname) {
		if (hostname == 'localhost') {
			return true
		} else {
			exploded = hostname.split('.');
			for (var k = 0; k < exploded.length; k++) {
				if (isNaN(exploded[k])) {
					return false;
				}
			}
		}
		return true;
	};

	function cacheDom() {
		try {
			DOM.$modulo = (document.querySelector('.comprejunto-container')) ? document.querySelector('.comprejunto-container') : parent.document.querySelector('.comprejunto-container');
		} catch (error) {
			console.log(error);
		}
	}

	function iniciar() {
		cacheDom();
	}

	function loadSelects(element) {
		var product = jQuery(element).data('product');
		var form = jQuery(element).closest('form')
		var selects = jQuery(form).find('select');
		var key = jQuery(element).data('key');
		var next = jQuery(selects).get(key+1);
		var rule = jQuery(form).data('rule');
		var last = (next == undefined)? true : false;
		var attributes = [];
		jQuery(selects).each(function() {
			if(jQuery(this).data('key') <= key) {
				attributes.push(jQuery(this).data('attribute')+':'+jQuery(this).val());
			} else {
				attributes.push(jQuery(this).data('attribute')+':null');
				return false;
			}
		});
		var configurables = [];
		jQuery(element).closest('[data-info=rule-container]').find('[data-info=product-container]').each(function() {
			if(jQuery(this).find('[name=child_id]').val()) {
				var prod_id = jQuery(this).find('select').first().data('product');
				if(prod_id != product)
					configurables.push(prod_id+':'+jQuery(this).find('[name=child_id]').val());
			}
		});
		jQuery.ajax({
			url: getUrlBase('comprejunto/comprejunto/variacoes'),
			type: "POST",
			data: {
				product_id : product,
				options : attributes,
				last : last,
				rule : rule,
				configurables : configurables
			},
			beforeSend: function() {
				if(jQuery(element).val()) {
					jQuery(element).css('border-color', '');
				}
				jQuery(selects).each(function() {
					if(jQuery(this).is(':visible'))
						jQuery(this).prop('disabled', true);
				});
			},
			error: function() {
				jQuery(selects).each(function() {
					if(jQuery(this).is(':visible'))
						jQuery(this).prop('disabled', false);
				});
			}
		}).done(function(data){
			if(data.request) {
				if(!last) {
					jQuery(next).html(data.result);
					jQuery(next).closest('div').show();
				} else {
					var container = jQuery(element).closest('[data-info=product-container]');
					jQuery(container).find('[data-info=image]').html(data.child_picture);
					jQuery(container).find('[data-info=name]').html(data.child_name);
					jQuery(container).find('[data-info=price]').html(data.child_price);
					jQuery(container).find('[data-info=rule-price]').html(data.child_comprejunto);
					jQuery(container).find('input[name=child_id]').val(data.child_id);
					var totals = jQuery(container).closest('[data-info=rule-container]').find('[data-info=totals-container]');
					jQuery(totals).html(data.totals);
				}
			}
			jQuery(selects).each(function() {
				if(jQuery(this).is(':visible'))
					jQuery(this).prop('disabled', false);
			});
		});
	}

	function validate(element) {
		var container = jQuery(element).closest('[data-info=rule-container]');
		var valid = true;
		jQuery(container).find('[data-info=product-container]').each(function() {
			jQuery(this).find('select').each(function() {
				if(jQuery(this).is(":visible") && !jQuery(this).val()) {
					valid &= false;
					jQuery(this).css('border-color', 'red');
				} else {
					jQuery(this).css('border-color', '');
				}
			});
		});
		if(valid) {
			jQuery(element).closest('form').submit();
		}
	}

	return {
		iniciar: iniciar,
		loadSelects: loadSelects,
		validate: validate
	};
}());

jQuery(document).ready(function () {
	CompreJunto.iniciar();
});