extends Node

var naampje = "je moeder"
var dna = []
# const DNA_SIZE = 173;
const TRAITS = {
	"Arm": [1, 4, 5],
	"Leg": [2, 1, 2],
	"Head": [1, 2, 2, 2, 1],
	"Skin": [3, 3, 3, 3],
	"Penis": [2]
}
const TIERS_STRING = "12   3    1 23 12 3 4 51  2  3  4  1 "
const TRAIT_STRING = "Arm       Leg  Head    Skin        Penis"
const SKIN_COLORS = [
	Color("#000000"),
	Color("#ff0000"),
	Color("#0000ff"),
	Color("#ffff00"),
	Color("#ffffff"),
	Color("#888888"),
	Color("#ff0088"),
	Color("#ff8800"),
	Color("#8800ff"),
	Color("#0088ff"),
	Color("#880088"),
	Color("#333333")
]

var SKIN_MATERIALS = [
	SpatialMaterial.new()
]

func initialize():
	# Make one boolean per thing we need
	for i in range(TRAITS.size()):
		for j in range(TRAITS.values()[i].size()):
			for x in range(TRAITS.values()[i][j]):
				if randf() > 0.1:
					dna.append(false)
				else:
					dna.append(true)

func sum(a):
	var sum = 0
	for i in range(a.size()):
		sum += a[i]
	return sum

func range_trait(name):
	var start = 0
	var end = 0
	for i in TRAITS:
		if i != name:
			start += sum(TRAITS[i])
		else:
			print(TRAITS[i])
			end = start+sum(TRAITS[i])
			break
	return range(start, end)

func describe():
	var TRAIT_DESC = "Has a %s trait of tier %d."
	var LATENT_DESC = "    latent tier %d trait."
	var out = ""
	var d = 0
	var found_trait = false
	for i in range(TRAITS.size()):
		found_trait = false
		for j in range(TRAITS.values()[i].size()):
			for x in range(TRAITS.values()[i][j]):
				if !found_trait && dna[d]:
					out += TRAIT_DESC % [TRAITS.keys()[i], j+1] + '\n'
					found_trait = true
				elif found_trait && dna[d]:
					out += LATENT_DESC % [j+1] + '\n'
				d += 1
	var strand = ""
	for i in range(dna.size()):
		if dna[i]:
			strand += "1"
		else:
			strand += "0"	
	out += strand + '\n'
	out += TIERS_STRING + '\n'
	out += TRAIT_STRING + '\n'
	return out

func set_color():
	var j = 0
	var x = 0
	for i in range_trait("Skin"):
		if dna[i]:
			x = j
			break
		j += 1	
	var mesh = get_node("Node/Node 2/Armature/Mesh")
	var mat = mesh.get_surface_material(0).duplicate()
	mat.albedo_color = SKIN_COLORS[x]
	# mesh.set_surface_material(0, mat)
	SKIN_MATERIALS[0].albedo_color = SKIN_COLORS[x]
	mesh.set_surface_material(0, SKIN_MATERIALS[0])
	print(SKIN_COLORS[x])

func _ready():
	initialize()
	#Register the selection event
	get_node("Area").connect('input_event', get_parent(), 'select_a_bitch', [get_index()])
	pass
