extends Node2D

enum BRIGHTNESS {
	NORMAL,
	DIM,
	DARK,
	NOLIGHT	
}

const brightnessdict = {
	BRIGHTNESS.NORMAL : Color(1, 1, 1),
	BRIGHTNESS.DIM : Color(0.6, 0.6, 0.6),
	BRIGHTNESS.DARK : Color(0.3, 0.3, 0.3),
	BRIGHTNESS.NOLIGHT : Color(0, 0, 0)
}

var trans = preload("res://objects/objects/transition/RoomTransition.tscn")
var wallspike = preload("res://objects/objects/spikes/wallspikedown.tscn")
var spikefloor = preload("res://objects/objects/spikes/floorspike.tscn")
var turret = preload("res://objects/objects/gun/Turret.tscn")
var flaskspawner = preload("res://objects/objects/flask/FlaskSpawner.tscn")
var flaskdisposer = preload("res://objects/objects/flask/FlaskDisposer.tscn")

export var room_size = Rect2(0,0,0,0)
export var room_name = ""
export(String, MULTILINE) var tile_properties = ""
export(game.areas) var room_area = game.areas.NONE
export(BRIGHTNESS) var light = BRIGHTNESS.NORMAL

var tile_dict = []

var object_types = {
	"transition down" : 0,
	"transition left" : 1,
	"transition right" : 2,
	"transition up" : 3,
	"wallspike down" : 4,
	"floor spike" : 5,
	"turret left" : 6,
	"flask down" : 7,
	"flask disposer down" : 8
}

var cam_limit = Rect2(0,0,0,0)


func _ready():
	$CanvasModulate.color = brightnessdict[light]
	
	parse_properties()
	size_init()
	ceil_init()
	object_init()
	player_init()
	

func ceil_init():
	#Take ceiling tiles and put them on the correct layer
	var tiles = get_node("Floor and Wall").get_used_cells_by_id(0)
	print("Making Ceiling...")
	for tile in tiles:
		$Foreground.set_cell(tile.x,tile.y,0)
		$Foreground.update_bitmask_area(Vector2(tile.x,tile.y))
		
		
func size_init():
	#Detect the size of the room based on placed tiles
	if room_size == Rect2(0,0,0,0):
		room_size = get_node("Floor and Wall").get_used_rect()
		print("Room detected to be "+str(room_size))
		
	#Autotile on the outer left edge of the room
	for outside_tile in room_size.end.y:
		if $"Floor and Wall".get_cellv(Vector2(room_size.position.x,outside_tile)) == 0:
			$"Floor and Wall".set_cellv(Vector2(room_size.position.x-1,outside_tile),0)
			$"Floor and Wall".update_bitmask_area(Vector2(room_size.position.x-1,outside_tile))
			
	#Autotile on the outer right edge of the room
	for outside_tile in room_size.end.y:
		if $"Floor and Wall".get_cellv(Vector2(room_size.end.x-1,outside_tile)) == 0:
			$"Floor and Wall".set_cellv(Vector2(room_size.end.x,outside_tile),0)
			$"Floor and Wall".update_bitmask_area(Vector2(room_size.end.x,outside_tile))
			
	#Autotile on the outer bottom edge of the room
	for outside_tile in room_size.end.x:
		if $"Floor and Wall".get_cellv(Vector2(outside_tile,room_size.end.y-1)) == 0:
			$"Floor and Wall".set_cellv(Vector2(outside_tile,room_size.end.y),0)
			$"Floor and Wall".update_bitmask_area(Vector2(outside_tile,room_size.end.y))
			
	#Autotile on the outer top edge of the room
	for outside_tile in room_size.end.x:
		if $"Floor and Wall".get_cellv(Vector2(outside_tile,room_size.position.y)) == 0:
			$"Floor and Wall".set_cellv(Vector2(outside_tile,room_size.position.y-1),0)
			$"Floor and Wall".update_bitmask_area(Vector2(outside_tile,room_size.position.y+1))
	
	#Set the camera boundaries based on room size
	$Camera.limit_bottom = room_size.end.y*16
	$Camera.limit_right = room_size.end.x*16
	$Camera.limit_top = room_size.position.y*16
	$Camera.limit_left = room_size.position.x*16
	
	cam_limit = Rect2(room_size.position.x*16, room_size.position.y*16, room_size.end.x*16, room_size.end.y*16)


func object_init():
	#Initialize objects based on placed tiles
	#Down Transition (Destination, Custom x, Custom y)
	var transdown = get_node("Object Placer").get_used_cells_by_id(object_types["transition down"])
	for tile in transdown:
		var tempdest = "Test/TestRoom"
		var tempx = 0
		var tempy = 0
		for item in tile_dict:
			if int(item[0]) == tile.x and int(item[1]) == tile.y:
				if item.size() > 2:
					tempdest = item[2]
				if item.size() > 3:
					tempx = item[3]
				if item.size() > 4:
					tempy = item[4]
		var newtrans = trans.instance()
		newtrans.setup(0, tempdest, tempx, tempy)
		newtrans.position.x = tile.x*16
		newtrans.position.y = tile.y*16
		$FObjects.add_child(newtrans)
	
	#Left Transition (Destination, Custom x, Custom y)
	var transleft = get_node("Object Placer").get_used_cells_by_id(object_types["transition left"])
	for tile in transleft:
		var tempdest = "Test/TestRoom"
		var tempx = 0
		var tempy = 0
		for item in tile_dict:
			if int(item[0]) == tile.x and int(item[1]) == tile.y:
				if item.size() > 2:
					tempdest = item[2]
				if item.size() > 3:
					tempx = item[3]
				if item.size() > 4:
					tempy = item[4]
		var newtrans = trans.instance()
		newtrans.setup(2, tempdest, tempx, tempy)
		newtrans.position.x = tile.x*16
		newtrans.position.y = tile.y*16
		$FObjects.add_child(newtrans)
		
	#Right Transition (Destination, Custom x, Custom y)
	var transright = get_node("Object Placer").get_used_cells_by_id(object_types["transition right"])
	for tile in transright:
		var tempdest = "Test/TestRoom"
		var tempx = 0
		var tempy = 0
		for item in tile_dict:
			if int(item[0]) == tile.x and int(item[1]) == tile.y:
				if item.size() > 2:
					tempdest = item[2]
				if item.size() > 3:
					tempx = item[3]
				if item.size() > 4:
					tempy = item[4]
		var newtrans = trans.instance()
		newtrans.setup(3, tempdest, tempx, tempy)
		newtrans.position.x = tile.x*16
		newtrans.position.y = tile.y*16
		$FObjects.add_child(newtrans)
	
	#Up transition (Destination, Custom x, Custom y)
	var transup = get_node("Object Placer").get_used_cells_by_id(object_types["transition up"])
	for tile in transup:
		var tempdest = "Test/TestRoom"
		var tempx = 0
		var tempy = 0
		for item in tile_dict:
			if int(item[0]) == tile.x and int(item[1]) == tile.y:
				if item.size() > 2:
					tempdest = item[2]
				if item.size() > 3:
					tempx = item[3]
				if item.size() > 4:
					tempy = item[4]
		var newtrans = trans.instance()
		newtrans.setup(1, tempdest, tempx, tempy)
		newtrans.position.x = tile.x*16
		newtrans.position.y = tile.y*16
		$FObjects.add_child(newtrans)
		
	#Down Wall Spike (No settings)
	var spikedown = get_node("Object Placer").get_used_cells_by_id(object_types["wallspike down"])
	for tile in spikedown:
		var newtrans = wallspike.instance()
		newtrans.position.x = tile.x*16
		newtrans.position.y = (tile.y*16)-16
		$BObjects.add_child(newtrans)	
	
	#Floor Spike (No Settings)
	var floorspike = get_node("Object Placer").get_used_cells_by_id(object_types["floor spike"])
	for tile in floorspike:
		var newspike = spikefloor.instance()
		newspike.position.x = tile.x*16
		newspike.position.y = tile.y*16
		$BObjects.add_child(newspike)
		
	#Left Turret (No Settings)
	var lturret = get_node("Object Placer").get_used_cells_by_id(object_types["turret left"])
	for tile in lturret:
		var newturret = turret.instance()
		newturret.position.x = tile.x*16 + 8
		newturret.position.y = tile.y*16
		$Objects.add_child(newturret)		
		
	#Flask Spawner Down (No Settings)
	var dflask = get_node("Object Placer").get_used_cells_by_id(object_types["flask down"])
	for tile in dflask:
		var newflask = flaskspawner.instance()
		newflask.position.x = tile.x*16 + 8
		newflask.position.y = tile.y*16 + 8
		newflask.direction = newflask.dir.DOWN
		$Objects.add_child(newflask)
		
	var dflaskdispose = get_node("Object Placer").get_used_cells_by_id(object_types["flask disposer down"])
	for tile in dflaskdispose:
		var newflask = flaskdisposer.instance()
		newflask.position.x = tile.x*16 + 8
		newflask.position.y = tile.y*16 + 8
		$Objects.add_child(newflask)
	
	#Hide the object tilemap so the game looks nice
	$"Object Placer".hide()
	
	
	
func get_brightness():
	return light
	
func _input(_event): #Hide tiles when key is pressed
	#Toggle hide tiles when "T" is pressed
	if Input.is_action_just_pressed("Hide Tiles"):
		$"Floor and Wall".visible = !$"Floor and Wall".visible
		$Foreground.visible = !$Foreground.visible
		
	#Toggle camera limit when "C" is pressed
	if Input.is_action_just_pressed("Camera Limit"):
		if $Camera.limit_top == cam_limit.position.y:
			$Camera.limit_top = -10000
			$Camera.limit_bottom = 10000
			$Camera.limit_left = -10000
			$Camera.limit_right = 10000
		else:
			$Camera.limit_top = cam_limit.position.y
			$Camera.limit_bottom = cam_limit.end.y
			$Camera.limit_left = cam_limit.position.x
			$Camera.limit_right = cam_limit.end.x
		
		
func parse_properties():
	#Parse the room's tile properties for use in object_init()
	var temp_tile = tile_properties.split("\n")
	for item in temp_tile:
		var temp_props = item.split(",")
		tile_dict.append(temp_props)
		
		
func player_init():
	#Place the player based on the previous room transition they entered
	if has_node("Objects/Player"):
		var transitions = $FObjects.get_children()
		for transition in transitions:
			if transition.is_in_group("transition"):
				#Ignore transitions if a custom position is set
				if game.enterx != 0 and game.entery != 0:
					if abs(transition.position.x-game.enterx) > 30 or abs(transition.position.y-game.entery) > 30:
						continue
						
				if transition.direction == 1 and game.enterdir == 0:
					$Objects/Player.global_position = transition.get_global_position() + transition.get_midpoint()
					$Objects/Player.position.y += 40
					
				if transition.direction == 0 and game.enterdir == 1:
					$Objects/Player.global_position = transition.get_global_position() + transition.get_midpoint()
					$Objects/Player.position.y -= 40
					
				if transition.direction == 2 and game.enterdir == 3:
					$Objects/Player.global_position = transition.get_global_position() + transition.get_midpoint()
					$Objects/Player.position.x += 40
					
				if transition.direction == 3 and game.enterdir == 2:
					$Objects/Player.global_position = transition.get_global_position() + transition.get_midpoint()
					$Objects/Player.position.x -= 40
