extends Node2D

const TILE_SIZE = 64
const MAP_WIDTH = 11
const MAP_HEIGHT = 8

enum MaskType { NONE, TRIANGLE, CIRCLE, SQUARE }

# Exported scenes
@export var GuardScene: PackedScene
@export var MaskPickupScene: PackedScene
@export var EndScene: PackedScene
@export var floor_texture: Texture2D
@export var wall_texture: Texture2D

@onready var rooms_container = $Rooms

# 2D map storage
var map = []

# --------------- CUSTOM MAP LAYOUT ---------------
# 0 = empty room / NONE
# T = triangle guard
# C = circle guard
# S = square guard
# M = mask pickup
# E = end goal
var CUSTOM_MAP = [
	["0","0","0","0","0","S","0","T","0","0","0"],
	["#","0","#","#","C","#","#","0","S","C","E"],
	["0","0","#","0","0","0","#","#","0","#","#"],
	["0","#","0","#","#","0","0","0","#","0","MS"],
	["0","0","0","0","#","0","#","0","T","0","#"],
	["#","0","#","0","#","0","0","#","0","#","MT"],
	["MC","#","0","0","0","#","0","0","#","0","0"],
	["0","0","0","#","0","S","T","0","0","0","#"]
]
# -----------------------------------------------

func _ready():
	generate_map()

func clear_rooms_container():
		for child in rooms_container.get_children():
			child.queue_free()

func generate_map():
	# Clear previous map if any
	clear_rooms_container()
	map.clear()

	for y in range(MAP_HEIGHT):
		map.append([])
		for x in range(MAP_WIDTH):
			var tile = CUSTOM_MAP[y][x]

			# Defaults
			var guarded = false
			var mask_required_type = MaskType.NONE
			var sprite = Sprite2D.new()
			@warning_ignore("integer_division")
			sprite.position = Vector2(x, y) * TILE_SIZE + Vector2(TILE_SIZE/2, TILE_SIZE/2)
			if tile == "#":
				var wall = StaticBody2D.new()
				
				sprite.texture = wall_texture
				wall.add_child(sprite)
				var shape = CollisionShape2D.new()
				var rect = RectangleShape2D.new()
				rect.size = Vector2(TILE_SIZE, TILE_SIZE)
				shape.shape = rect
				shape.shape.size = Vector2(TILE_SIZE, TILE_SIZE)
				@warning_ignore("integer_division")
				shape.position = Vector2(TILE_SIZE/2, TILE_SIZE/2)
				wall.add_child(shape)
				
				wall.position = Vector2(x,y) * TILE_SIZE
				
				rooms_container.add_child(wall)
			else:
				sprite.texture = floor_texture
				$Rooms.add_child(sprite)
			
			var tex_size = sprite.texture.get_size()
			sprite.scale = Vector2(
				TILE_SIZE / tex_size.x,
				TILE_SIZE / tex_size.y
			)
			
			
			match tile:
				"0", "", "#":
					guarded = false
					mask_required_type = MaskType.NONE
				"T":
					guarded = true
					mask_required_type = MaskType.TRIANGLE
					spawn_guard(x, y, MaskType.TRIANGLE)
				"C":
					guarded = true
					mask_required_type = MaskType.CIRCLE
					spawn_guard(x, y, MaskType.CIRCLE)
				"S":
					guarded = true
					mask_required_type = MaskType.SQUARE
					spawn_guard(x, y, MaskType.SQUARE)
				"MC":
					var mask = MaskPickupScene.instantiate()
					mask.mask_type = MaskType.CIRCLE
				"MS":
					var mask = MaskPickupScene.instantiate()
					mask.mask_type = MaskType.SQUARE
				"MT":
					var mask = MaskPickupScene.instantiate()
					mask.mask_type = MaskType.TRIANGLE
				_:
					print('krrkrrkikiriri at ', x, y, ": ", tile)
			
			# Assign according to custom map
			

			# Save map info
			map[y].append({
				"guarded": guarded,
				"mask_required_type": mask_required_type
			})


# ---------------- SPAWN FUNCTIONS ----------------

func spawn_guard(x, y, mask_type):
	if GuardScene == null:
		print("ERROR: GuardScene not assigned!")
		return
	var guard = GuardScene.instantiate()
	rooms_container.add_child(guard)
	@warning_ignore("integer_division")
	guard.position = Vector2(x, y) * TILE_SIZE + Vector2(TILE_SIZE/2, TILE_SIZE/2)
	guard.z_index = 100
	if guard.has_method("set_guard_type"):
		guard.set_guard_type(mask_type)

func spawn_mask(x, y):
	if MaskPickupScene == null:
		print("ERROR: MaskPickupScene not assigned!")
		return
	var mask = MaskPickupScene.instantiate()
	rooms_container.add_child(mask)
	@warning_ignore("integer_division")
	mask.position = Vector2(x, y) * TILE_SIZE + Vector2(TILE_SIZE/2, TILE_SIZE/2)
	mask.z_index = 50

func spawn_end(x, y):
	if EndScene == null:
		print("ERROR: EndScene not assigned!")
		return
	var end_node = EndScene.instantiate()
	rooms_container.add_child(end_node)
	if end_node.has_method("set_tile_position"):
		end_node.set_tile_position(x, y)
	end_node.z_index = 50
