extends Node
signal pick_up(item_name, obj)

func _ready():
	pass

var items = {
	"s_dagger": false
}

var item_dialogue = {
	"s_dagger": {
		"item_fullname": "sacrificial dagger",
		"item_artname": "the sacrificial dagger",
		"item_msg": "You see ancient dagger a small piece of metal with nothing special about it except the feeling of uneasiness and understanding that this is not ordinary weapon, but something with different purpose than just defeating the enemy. Something to thank bloodthirsty gods? Or avoid their wrath.",
		"item_take_msg": "You fill freezing touch of the rough dagger at your hand. You feel the touch of ancient history, of many many veins opened, and lots of hearts pierced.",
		"item_leave_msg": "You left the dagger be."
	}
}

onready var item_scenes = {
	"s_dagger": load("res://scenes/weapons/sacrificial_dagger.tscn")
}
onready var item_collision_scenes = {
	"s_dagger": load("res://scenes/weapons/dagger_collision.tscn")
}
func equip(obj, item_name):
	if obj.has_meta("equipped"):
		return
	print("EQUIP ", item_name)
	assert(obj)
	assert(obj.has_meta("owner"))
	assert(item_scenes.has(item_name))
	var r = item_scenes[item_name].instance()
	var c = item_collision_scenes[item_name].instance()
	obj.add_child(r)
	r.add_child(c)
	r.transform = Transform()
	c.transform = Transform()
	print("EQUIP ", item_name, " OK", obj, r)
	obj.set_meta("equipped", item_name)
	c.set_meta("owner", obj.get_meta("owner"))
	c.set_meta("item_name", item_name)
func register_pick_up(m, obj, item_name):
	var mdata = {
		"method": "pick_up",
		"item": item_name,
		"item_obj": obj
	}
	markers.init_marker(self, m, mdata)

func pick_up_dialogue(item, item_obj):
	return
#	if controls.is_gui:
#		return
#	Dialogic.set_autosave(false)
#	var scene = "pick_up_item"
#	print("SCENE: ", scene)
#	controls.is_gui = true
#	var dialogue = Dialogic.start(scene, false)
#	Dialogic.set_variable("item_name", item)
#	Dialogic.set_variable("item_fullname", item_dialogue[item].item_fullname)
#	Dialogic.set_variable("item_artname", item_dialogue[item].item_artname)
#	Dialogic.set_variable("item_msg", item_dialogue[item].item_msg)
#	Dialogic.set_variable("item_take_msg", item_dialogue[item].item_take_msg)
#	Dialogic.set_variable("item_leave_msg", item_dialogue[item].item_leave_msg)
#	add_child(dialogue)
#	dialogue.connect("timeline_end", self, "finish_dialogue", [dialogue])
#	dialogue.connect("dialogic_signal", self, "signal_process", [dialogue])
#	dialogue.set_meta("item", item)
#	dialogue.set_meta("item_obj", item_obj)
func finish_dialogue(dname, d):
	controls.is_gui = false
	d.queue_free()

func signal_process(s, d):
	var item = d.get_meta("item")
	var item_obj = d.get_meta("item_obj")
	match s:
		"item_accepted":
			items[item] = true
			emit_signal("pick_up", item, item_obj)
		"item_rejected":
			pass
func pick_up(obj):
	if items.has(obj.item) && items[obj.item]:
		return
	if item_dialogue.has(obj.item):
		pick_up_dialogue(obj.item, obj.item_obj)
	else:
		items[obj.item] = true
		emit_signal("pick_up", obj.item, obj.item_obj)
