extends Node2D

const PRESENT_SCENE = preload("res://Events/Present.tscn")
const PRESENT_SELECT_SCENE = preload("res://UI/PresentSelect.tscn")

export (Array, Resource) var levels

onready var player = $Player
onready var mob_position = $MobPosition
onready var spawn_timer = $SpawnTimer
onready var events_icons = $EventsIcons
var enemy = null setget set_enemy

#export (Array, Array, PackedScene) var events
var events: Array = []
var current_level = 0

func _ready():
	Global.battle_node = self
	player.connect("dead", self, "player_dead")
	
	print(levels)
	if levels.size() > 0:
		new_level(levels[current_level])

func new_level(level):
	Audio.play_sound("NextLevel")
	events.clear()
	events = level.generate()
	print(events)
	
	events_icons.new_level()
	for event in events:
		if event.has("type"):
			var icon = events_icons.text_to_event_icon(event.type)
			events_icons.level.append(icon)
			events_icons.place_in_screen_center()
	$CanvasLayer/LevelNameContainer.start(current_level, level.level_name)
	spawn_timer.start(3)
	yield(spawn_timer, "timeout")
	next_event()

func player_dead():
	player = null

func enemy_dead():
	spawn_timer.start(1)
	yield(spawn_timer, "timeout")
	if player != null:
		next_event()

func set_enemy(mob):
	enemy = mob
	player.target_enemy = mob
	mob.target_enemy = player
	
	mob.connect("dead", self, "enemy_dead")
	mob.connect("dead", player, "target_enemy_dead")
	player.connect("dead", mob, "target_enemy_dead")

func next_event():
	if events.size() == 0:
		current_level += 1
		if current_level > levels.size()-1:
			new_level(levels[levels.size()-1])
		elif levels.size() > 0:
			new_level(levels[current_level])
		return
	
	var event: Dictionary = events.pop_front()
	events_icons.move_player()
	#if event.type = "entity":
	
	if event.type == "entity" or event.type == "boss":
		var mob = null
		var items = null
		var awards = null
		var stats = null
		
		if current_level >= levels.size()-1:
			stats = event.stats
			stats.max_health_multiplier = 1 + (float(current_level)/10)
			stats.damage_base = 1 + (float(current_level)/10)
		
		if event.has("enemy"):
			mob = event.enemy
		if event.has("items"):
			items = event.items
		if event.has("awards"):
			awards = event.awards
		if event.has("stats"):
			stats = event.stats
		
		load_mob(mob, items, awards, stats)
	elif event.type == "present":
		if event.has("items"):
			player.state = player.STATE.NO_INPUT
			load_present(event.items)
		else:
			next_event()

func load_mob(mob, items, awards, stats):
	if mob == null:
		return
	var mob_instance = mob.instance()
	mob_position.add_child(mob_instance)
	
	mob_instance.flip_h(true)
	mob_instance.appear_animation(10)
	
	self.enemy = mob_instance
	#print("--- ", mob_instance.name, " appears! ---")
	
	for item in items:
		mob_instance.item_add(item)
	
	if awards != null:
		mob_instance.awards = awards
	
	if stats != null:
		mob_instance.add_stats(stats)

func load_present(items):
	if items.size() == 0:
		return
	var inst = PRESENT_SCENE.instance()
	mob_position.add_child(inst)
	
	inst.appear_animation(20)
	yield(inst.tween, "tween_all_completed")
	
	var present_select_inst = PRESENT_SELECT_SCENE.instance()
	present_select_inst.items = items
	present_select_inst.connect("item_choosen", self, "present_item_choosen")
	
	$CanvasLayer/MarginContainer.add_child(present_select_inst)

func present_item_choosen(item):
	player.state = player.STATE.NORMAL
	player.item_add(item)
	
	for i in mob_position.get_children():
		i.queue_free()
	next_event()
