extends CanvasLayer

func openmenu():
#	get_tree().paused = true
	Engine.time_scale = 0.01
	$Control2.show()
	$Control/VSeparator/Label.text = 'Choose a power:'
	$Control/VSeparator/ItemList.clear()
	var array = []
	var text = Powerups.menbros.keys() + Powerups.corpo.keys() + Powerups.apendice.keys()
	var superdict = {}
	for z in Powerups.menbros.keys(): superdict[z] = Powerups.menbros[z]
	for z in Powerups.corpo.keys(): superdict[z] = Powerups.corpo[z]
	for x in 5:
		while text.has('null'): text.erase('null')
		text.shuffle()
		array.append(text[0])
		text.remove(0)
	for x in array:
		$Control/VSeparator/ItemList.add_item(x)
	
	for x in 5:
		$Control2/VSeparator/buttons.get_child(x).id = x
		$Control2/VSeparator/buttons.get_child(x).set_texture( superdict[array[x]]['icone'] )
		$Control2/VSeparator/buttons.get_child(x).text = array[x]
		var buff = 0
		if Powerups.menbros.has(array[x]): buff = 4
		if Powerups.corpo.has(array[x]): buff = 8
		$Control2/VSeparator/buttons.get_child(x).collayer = buff
		$Control2/VSeparator/buttons.get_child(x).col()
		$Control2/VSeparator/buttons.get_child(x).posswitch = true
	
#	Powerups.dict
	
	for x in get_tree().get_nodes_in_group('cleaning'):
		x.queue_free()
	
	for x in $Control2/VSeparator/menbros.get_child_count():
		var opt = Powerups.dict['menbros'+str($Control2/VSeparator/menbros.get_child(x).id)]
		if opt != Powerups.menbros.null:
			addoccuptslot(opt,x,$Control2/VSeparator/menbros.get_child(x))
	for x in $Control2/VSeparator/corpo.get_child_count():
		var opt = Powerups.dict['corpo'+str($Control2/VSeparator/corpo.get_child(x).id)]
		if opt != Powerups.corpo.null:
			addoccuptslot(opt,x,$Control2/VSeparator/corpo.get_child(x))


func addoccuptslot(opt,x,arg):
	var sprite = Sprite.new()
	sprite.texture = opt.icone
	sprite.position += Vector2(32,32)
	var label = Label.new()
	label.rect_position.y = 16
	label.rect_position.x = 16
	label.text = str(opt.level)
	sprite.add_child(label)
	arg.add_child(sprite)
	sprite.add_to_group('cleaning')

func _on_bacteria_level_up():
	openmenu()
	pass # Replace with function body.

var power = ''
var dict = ''

func _on_ItemList_item_activated(index):
	if $Control/VSeparator/Label.text == 'Choose a power:':
		print(index)
		power = $Control/VSeparator/ItemList.get_item_text(index)
		if Powerups.menbros.has(power): dict = 'menbros'
		if Powerups.corpo.has(power): dict = 'corpo'
		if Powerups.apendice.has(power): dict = 'apendice'
		$Control/VSeparator/ItemList.clear()
		for x in Powerups.dictslots[dict]:
			$Control/VSeparator/ItemList.add_item(Powerups.dict[dict + str(x)].name)
		$Control/VSeparator/Label.text = 'put on slot:'
	elif $Control/VSeparator/Label.text == 'put on slot:':
		print(index)
		var dict2 = dict + str(index)
#		if not Powerups.has(dict2): return
#		if not Powerups.has(dict): return
		if Powerups.dict[dict2] == Powerups[dict][power]:
			print('upgrading power')
			Powerups[dict][power]['level'] += 1
			for x in Powerups[dict][power]['upgrade'].keys():
				 Powerups[dict][power][x] += Powerups[dict][power]['upgrade'][x]
		Powerups.dict[dict2] = Powerups[dict][power]
		get_parent().get_node("bacteria")._load()
		print(dict2)
		print(power)
#		get_tree().paused = false
		emit_signal('close_menu')
		Engine.time_scale = 1
		$Control2.hide()
	pass # Replace with function body.

signal close_menu

var buff1 = 0
var buff2 = 0

var lock = []

#func _physics_process(delta):
#	print(lock)

func _on_Button_button_down():
	_on_ItemList_item_activated(buff1)
	_on_ItemList_item_activated(buff2)
	pass # Replace with function body.
