extends Node

# Message Bus

# Signals
#signal published()

# Variables
#var Mercur = load("res://entity/Mercur/Mercur.tscn").instance() # mail god
var current_ref_id = -1
var topicDict = {}
var approached_topic = "approached"
#var approached_handler = load("res://Events/approached/Approach.tscn").instance()
var died_topic = "died"
#var died_handler = load("res://Events/died/Died.tscn").instance()
var duration = "duration"
var earned_xp_topic = "xp_earned"
#var earned_xp_handler = load("res://Events/xp_changed/EarnedXP.tscn").instance()
var equipped_skill_topic = "skill_equipped"
#var equipped_skill_handler = load("res://Events/skill_equipped/EquipSkill.tscn").instance()
var gained_level_topic = "level_gained"
#var gained_level_handler = load("res://Events/xp_changed/GainLevel.tscn").instance()
var interacted_topic = "interacted"
#var interacted_handler = load("res://Events/character_interacted/Interact.tscn").instance()
var item_picked_up_topic = "item_picked_up"
#var learned_item_handler = load("res://Events/item_picked_up/LearnItem/LearnItem.tscn").instance()
var learned_skill_topic = "skill_learned"
#var learned_skill_handler = load("res://Events/skill_learned/LearnSkill.tscn").instance()
var moved_handler = load("res://events/moved/Char.tscn").instance()
var moved_topic = "moved"

func designate_ref_id():
	current_ref_id += 1
	return current_ref_id

# Take data + hand off to Event Handlers subscribed to event
func publish(topic, data):
	if !topicDict.has(topic):
#		print_debug("publish - unknown topic: " + topic)
		return
	for handler in topicDict[topic]:
		handler.handle(data)

# Handler begins listening for topic 
func subscribe(topic, handler):
	if !topicDict.has(topic):
#		print_debug("subscribe - new topic: " + topic)
		topicDict[topic] = []
		#return
	topicDict[topic].append(handler)
	handler.connect("tree_exited", self, "_on_handler_tree_exited", [topic, handler])

# Handler no longer listens for topic
func unsubscribe(topic, handler):
	if !topicDict.has(topic):
#		print_debug("Cannot unsubscribe handler that does not exist")
		return
	topicDict[topic].erase(handler)

func _on_handler_tree_exited(topic, handler):
	unsubscribe(topic, handler)

# if this character exits tree
func _on_tree_exited():
	#unsubscribe(died_topic, died_handler)
	#unsubscribe(earned_xp_topic, earned_xp_handler)
	#unsubscribe(gained_level_topic, gained_level_handler)
	#subscribe(gained_level_topic, gained_level_handler)
	#unsubscribe(item_picked_up_topic, learned_item_handler)
	#unsubscribe(learned_skill_topic, learned_skill_handler)
	unsubscribe(moved_topic, moved_handler)

func _ready():
	#subscribe(died_topic, died_handler)
	#subscribe(earned_xp_topic, earned_xp_handler)
	#subscribe(gained_level_topic, gained_level_handler)
	#subscribe(interacted_topic, interacted_handler)
	#subscribe(item_picked_up_topic, learned_item_handler)
	#subscribe(learned_skill_topic, learned_skill_handler)
	subscribe(moved_topic, moved_handler)
#	connect("tree_exited", self, "_on_tree_exited")

