/**
 * File customizer_shared.js.
 * Theme Customizer and Customizer Preview extensions.
 */

(function($) {
	// $(document).on('ready', function() {
	$(function() {
		// init js
		shared.setup();
	});
})(jQuery);

var shared = new function() {

	this.themeversion = undefined;
	this.mods = undefined;
	this.keys = undefined;

	this.setup = function() {
		// ver vars from customizer.php
		shared.themeversion = themeversion;
		shared.mods = mods;
		shared.keys = Object.keys(mods);
	}

	this.getDependencies = function() {	
		// search dependencies
		var dependencies = {};
		for (var i=0; i<shared.keys.length; i++) {
			var key = shared.keys[i];
			var value = shared.mods[key];
			// console.log(key, value);
			if (value['dependency'] != undefined) {
				dependencies = this.getDependency(value['mod'], value['dependency'], value['dependency'], dependencies);
			}
		}
		return dependencies;
	}
	this.getDependency = function(mod, dependency, dependency_full, dependencies) {
		// old dependency style (single element without value)
		if (typeof dependency === 'string' ) {
			if (dependencies[dependency] == undefined) dependencies[dependency] = [];
			dependencies[dependency].push( 
				{ 'mod': mod, 'dependency': dependency_full } 
			);
		}
		// single dependency
		else if (dependency['element'] != undefined) {
			if (dependencies[dependency['element']] == undefined) dependencies[dependency['element']] = [];
			dependencies[dependency['element']].push( 
				{ 'mod': mod, 'dependency': dependency_full } 
			);
		}
		// multi dependency
		else {
			if (dependency['operator'] == undefined) dependency['operator'] = 'and';
			for (var key of Object.keys(dependency)) {
				if (key != 'operator') {
					dependencies = this.getDependency(mod, dependency[key], dependency_full, dependencies);
				}
			}
		}
		return dependencies;
	}

	this.calcDependency = function(value, dependency) {
		var type = this.getModField(dependency['element'], 'type');
		// console.log(dependency['element']+": "+type);
		var display = false;
		if (dependency['value'] != undefined) {
			var dep_value = dependency['value'];
			if (!Array.isArray(dep_value)) dep_value = [ dep_value ];
			for (var j=0; j<dep_value.length; j++) {
				if (value == dep_value[j]) display = true;
			}
		}
		else if (dependency['not_value'] != undefined) {
			var dep_value = dependency['not_value'];
			if (!Array.isArray(dep_value)) dep_value = [ dep_value ];
			var hits = 0;
			for (var j=0; j<dep_value.length; j++) {
				if (value != dep_value[j]) hits++;
			}
			if (hits == dep_value.length) display = true;
		}
		// range text textarea
		else if (dependency['smaller'] != undefined) {
			var dep_value = dependency['smaller'];
			if (type == 'text' || type == 'textarea') { 
				if (value.length < dep_value) display = true; 
			}
			else if (type == 'range') { 
				if (value < dep_value) display = true; 
			}
		}
		else if (dependency['bigger'] != undefined) {
			var dep_value = dependency['bigger'];
			if (type == 'text' || type == 'textarea') { 
				if (value.length > dep_value) display = true; 
			}
			else if (type == 'range') { 
				if (value > dep_value) display = true; 
			}
		}
		return display;
	}

	this.getModKey = function(mod) {
		for (var i=0; i<shared.keys.length; i++) {
			if (shared.mods[shared.keys[i]]['mod'] == mod) 
				return shared.keys[i];
		}
		return false;
	}
	this.getModField = function(mod, field) {
		for (var i=0; i<shared.keys.length; i++) {
			if (shared.mods[shared.keys[i]]['mod'] == mod) 
				return shared.mods[shared.keys[i]][field];
		}
		return false;
	}

	this.hex2rgb = function(hex) {
		// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
		var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
		hex = hex.replace(shorthandRegex, function(m, r, g, b) {
			return r + r + g + g + b + b;
		});
		// decode hex
		var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
		return result ? {
			r: parseInt(result[1], 16),
			g: parseInt(result[2], 16),
			b: parseInt(result[3], 16)
		} : false;
	}
	this.rgb2rgba = function(rgb) {
		var tmp = rgb.split('(');
		tmp = tmp[1].split(')');
		tmp = tmp[0].split(',');
		if (tmp.length == 3) return { r: tmp[0], g: tmp[1], b: tmp[2], a: 1 };
		if (tmp.length == 4) return { r: tmp[0], g: tmp[1], b: tmp[2], a: tmp[3] };
		return false;
	}
	// this.rgb2hex = function(rgb) {
	// 	return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
	// }

};
