extends Spatial

var building_destruction_scene = preload("res://scenes/destruction.tscn")

onready var map = get_node("/root/Level/Map")
onready var characters = get_node("/root/Level/Map/Characters")
onready var money = get_node("/root/Level/Map/Money")
onready var utils = get_node("/root/Level/Map/Utils")

func move_to_tile(position : Vector3):
	var tile_cell = map.get_tile_cell(position)
	var building_cell = map.get_building_cell(position)
	var world_position = utils.grid_to_world_position(position)
	
	# tile to move should be connected with current character tile
	if not map.are_tiles_connected(utils.world_to_grid_position(characters.current_character_obj().translation), 
								   position, 
								   map.current_player_tile()):
		return
	
	# can't go on tile, blocked with >= level, except characters with MAX_LEVEL
	if map.tile_block_level(position, characters.current_player) >= characters.current_character_obj().level and \
	   characters.current_character_obj().level != characters.current_character_obj().MAX_LEVEL:
		return
	
	if tile_cell == GridMap.INVALID_CELL_ITEM:
		return
	
	# move without action (to current player tile)
	if tile_cell == map.current_player_tile() and \
	   building_cell == GridMap.INVALID_CELL_ITEM:
		
		characters.current_character_obj().translation = world_position
		
		if characters.destroy_other_player_characters(position, characters.current_player) > 0:
			characters.set_next_character()
			utils.spawn_on_position(building_destruction_scene, position)
	
	# move with action (to tile of other player)
	elif tile_cell != GridMap.INVALID_CELL_ITEM and \
		 tile_cell != map.current_player_tile() and \
		 map.has_neighbour_tile_cell(position, map.current_player_tile()):
		
		map.set_tile_cell(position, map.current_player_tile())
		
		characters.current_character_obj().translation = world_position
		
		if building_cell != GridMap.INVALID_CELL_ITEM or \
		   characters.destroy_other_player_characters(position, characters.current_player) > 0:
			utils.spawn_on_position(building_destruction_scene, position)
		
		if building_cell != GridMap.INVALID_CELL_ITEM:
			money.add_money_to_nearest_other_house(position, 
											 map.current_player_tile(), 
											 money.houses_money[utils.position_to_string(position)])
			money.destroy_house(position)
			
		map.set_building_cell(position, GridMap.INVALID_CELL_ITEM)
		
		characters.turn_characters_into_rogues()
		
		characters.set_next_character()

func make_turn(position : Vector3):
	if map.is_connected_to_house(position, map.current_player_tile()):
		var mouse_position_character_and_player_ids = characters.find_character_and_player_ids_on(position)
		if map.get_tile_cell(position) == map.current_player_tile() and \
		   mouse_position_character_and_player_ids != null and \
		   mouse_position_character_and_player_ids[0] == characters.current_player and \
		   not characters.is_current_player_character_used(mouse_position_character_and_player_ids[1]):
			characters.switch_character_to(mouse_position_character_and_player_ids[0], 
										   mouse_position_character_and_player_ids[1])
		elif characters.is_current_character_exist():
			move_to_tile(position)

func upgrade_character_on_position(position : Vector3):
	var character_on_position = characters.find_character_on(position)
	if character_on_position != null and \
	   map.get_tile_cell(position) == map.current_player_tile() and \
	   money.count_connected_money(position, map.current_player_tile()) \
		 >= money.CHARACTER_UPGRADE_COSTS[character_on_position.level] and \
	   character_on_position.upgrade():
		money.spend_connected_money(position, map.current_player_tile(), money.CHARACTER_COST)

func spawn_character(position : Vector3):	
	money.spend_connected_money(position, map.current_player_tile(), money.CHARACTER_COST)
	
	characters.spawn_current_player_character(position)
	
	characters.switch_character_to(characters.current_player, \
								   characters.current_player_characters_count() - 1)
		
	if characters.destroy_other_player_characters(position, characters.current_player) > 0:
		characters.set_next_character()
		utils.spawn_on_position(building_destruction_scene, position)

func handle_spawn(position : Vector3):
	if map.get_tile_cell(position) == map.current_player_tile():
		var character_and_player_ids_on_position = characters.find_character_and_player_ids_on(position)
		if character_and_player_ids_on_position == null: # try spawn character
			if map.get_building_cell(position) == GridMap.INVALID_CELL_ITEM and \
			   map.is_connected_to_house(position, map.current_player_tile()) and \
			   money.count_connected_money(position, map.current_player_tile()) >= money.CHARACTER_COST:
				spawn_character(position)
		else: # try merge characters
			if characters.is_current_character_exist() and \
			   character_and_player_ids_on_position[0] == characters.current_player:
					characters.try_merge_current_character_with(character_and_player_ids_on_position[1])
	else: # TODO: spawn and move character
		pass

func _physics_process(_delta):
	if Input.is_action_just_released("turn"):
		make_turn(utils.world_to_grid_position(utils.mouse_position()))
	
	if Input.is_action_just_released("upgrade"):
		upgrade_character_on_position(utils.world_to_grid_position(utils.mouse_position()))
		
	if Input.is_action_just_released("pass"):
		characters.set_next_player()
		
	if Input.is_action_just_released("spawn"):
		handle_spawn(utils.world_to_grid_position(utils.mouse_position()))
