extends Piece

class_name Mage

enum Upgrade {
	# Add any specific upgrades for Mage here
}

@onready var game_node = get_tree().root.get_node("Game")

func _init(is_white_param: bool, position_param: Vector2):
	super._init(is_white_param, position_param)

func get_valid_moves(board_state: Array, pos: Vector2) -> Array[Vector2]:
	var valid_moves: Array[Vector2] = []
	var sel_x = pos.x
	var sel_y = pos.y

	# Add moves based on the provided logic
	if (sel_x + 1) < 8 and (sel_y + 1) < 8:
		if is_empty(board_state, sel_x + 1, sel_y + 1):
			valid_moves.append(Vector2(sel_x + 1, sel_y + 1))
	if (sel_x - 1) >= 0 and (sel_y + 1) < 8:
		if is_empty(board_state, sel_x - 1, sel_y + 1):
			valid_moves.append(Vector2(sel_x - 1, sel_y + 1))
	if (sel_x + 1) < 8 and (sel_y - 1) >= 0:
		if is_empty(board_state, sel_x + 1, sel_y - 1):
			valid_moves.append(Vector2(sel_x + 1, sel_y - 1))
	if (sel_x - 1) >= 0 and (sel_y - 1) >= 0:
		if is_empty(board_state, sel_x - 1, sel_y - 1):
			valid_moves.append(Vector2(sel_x - 1, sel_y - 1))

	if (sel_x + 1) < 8:
		if is_empty(board_state, sel_x + 1, sel_y):
			valid_moves.append(Vector2(sel_x + 1, sel_y))
	if (sel_x - 1) >= 0:
		if is_empty(board_state, sel_x - 1, sel_y):
			valid_moves.append(Vector2(sel_x - 1, sel_y))

	if (sel_y + 1) < 8:
		if is_empty(board_state, sel_x, sel_y + 1):
			valid_moves.append(Vector2(sel_x, sel_y + 1))
	if (sel_y - 1) >= 0:
		if is_empty(board_state, sel_x, sel_y - 1):
			valid_moves.append(Vector2(sel_x, sel_y - 1))

	return valid_moves

func get_valid_shots(board_state: Array, pos: Vector2) -> Array[Vector2]:
	var valid_shots: Array[Vector2] = []
	var sel_x = pos.x
	var sel_y = pos.y

	# Valid shots diagonally down and right
	for x in range(1, 8):
		var new_x = sel_x + x
		var new_y = sel_y + x
		if new_x < 8 and new_y < 8:
			if is_empty(board_state, new_x, new_y):
				pass
			elif is_opposite(board_state, new_x, new_y):
				valid_shots.append(Vector2(new_x, new_y))
				break
			else:
				break
		else:
			break

	# Repeat for diagonally up and left
	for x in range(1, 8):
		var new_x = sel_x - x
		var new_y = sel_y - x
		if new_x >= 0 and new_y >= 0:
			if is_empty(board_state, new_x, new_y):
				pass
			elif is_opposite(board_state, new_x, new_y):
				valid_shots.append(Vector2(new_x, new_y))
				break
			else:
				break
		else:
			break

	# Repeat for diagonally down and left
	for x in range(1, 8):
		var new_x = sel_x - x
		var new_y = sel_y + x
		if new_x >= 0 and new_y < 8:
			if is_empty(board_state, new_x, new_y):
				pass
			elif is_opposite(board_state, new_x, new_y):
				valid_shots.append(Vector2(new_x, new_y))
				break
			else:
				break
		else:
			break

	# Repeat for diagonally up and right
	for x in range(1, 8):
		var new_x = sel_x + x
		var new_y = sel_y - x
		if new_x < 8 and new_y >= 0:
			if is_empty(board_state, new_x, new_y):
				pass
			elif is_opposite(board_state, new_x, new_y):
				valid_shots.append(Vector2(new_x, new_y))
				break
			else:
				break
		else:
			break

	return valid_shots

func shoot_projectile(x: int, y: int):
	game_node.shoot_projectile(x, y)

func is_empty(board_state: Array, x: int, y: int) -> bool:
	return board_state[x][y] == null

func is_opposite(board_state: Array, x: int, y: int) -> bool:
	return board_state[x][y] != null and board_state[x][y].is_white != is_white