extends Resource
class_name Level

export (String) var level_name
export (int, 3, 15) var level_length = 3
export (Array, PackedScene) var mobs_available
export (int) var mobs_in_row_min = 2
export (int) var mobs_in_row_max = 3
export (Dictionary) var add_mob_stats: Dictionary
export (Array, Resource) var mob_items_have
export (Array, Resource) var mob_items_random
export (int) var mob_items_random_count = 1
export (Array, Resource) var present_items_available
export (int) var present_items_min = 2
export (int) var present_items_max = 2
export (Array, PackedScene) var bosses_available
export (Dictionary) var add_boss_stats: Dictionary
export (Array, Resource) var boss_items_have
export (Array, Resource) var boss_items_random
export (int) var boss_items_random_count = 1
export (Array, Resource) var boss_awards

func generate():
	var level = []
	var mob_last_spawned = 0
	#var present_last_spawned = 0
	var random
	
	for i in range(level_length):
		level.append({})
		var event = level[i]
		
		if i == level_length-1:
			event.type = "boss"
			event.enemy = get_random_element(bosses_available)
			event.items = []
			event.awards = []
			for item in boss_items_have:
				event.items.append(item)
			
			for _times in boss_items_random_count:
				var item = get_random_element(boss_items_random)
				event.items.append(item)
			
			for item in boss_awards:
				event.awards.append(item)
				
			event.stats = add_boss_stats
			continue
		random = "entity"#get_random_element(["entity", "present"])
		if mob_last_spawned >= mobs_in_row_max:
			random = "present"
			mob_last_spawned = 0
		elif mob_last_spawned >= mobs_in_row_min and randf() < 0.5:
			random = "present"
			mob_last_spawned = 0
		
		if random == "entity":
			mob_last_spawned += 1
		
		event.type = random
		if event.type == "entity":
			event.enemy = get_random_element(mobs_available)
			event.items = []
			for item in mob_items_have:
				event.items.append(item)
			
			for _times in mob_items_random_count:
				var item = get_random_element(mob_items_random)
				event.items.append(item)
				
			event.stats = add_mob_stats
		elif event.type == "present" and present_items_available.size() != 0:
			event.items = []
			var items_available_temp: Array = present_items_available.duplicate()
			var times_max = min(present_items_max, present_items_available.size())
			for _times in rand_range(present_items_min, times_max):
				items_available_temp.shuffle()
				event.items.append(items_available_temp.pop_front())
	
	return level

func get_random_element(array: Array):
	var a = array.duplicate()
	if a.size() > 0:
		a.shuffle()
		return a[0]
	else:
		return null
