extends Piece

class_name Pawn

var Game = preload("res://scripts/game.gd")

enum Upgrade { DOUBLE_MOVE, DIAGONAL_MOVE }

var tile_size = 64 # Make this global?...
@onready var opponent_shader = load("res://shaders/BlackShader.shader")

func _init(is_white_param: bool, position_param: Vector2):
	super._init(is_white_param, position_param)
	# Testing upgrades
	upgrades.append(Upgrade.DOUBLE_MOVE)
	upgrades.append(Upgrade.DIAGONAL_MOVE)


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

	# TODO: Add En passant move
	
	if is_white:
		# Check if the pawn is at the starting position
		if (y == 7 or Upgrade.DOUBLE_MOVE in upgrades) and board_state[x][y - 1] == null:
			valid_moves.append(Vector2(x, y - 1))
			if board_state[x][y - 2] == null:
				valid_moves.append(Vector2(x, y - 2))
		elif board_state[x][y - 1] == null: 
			valid_moves.append(Vector2(x, y - 1))

		# Check if the pawn can capture an enemy piece
		if x > 0 and y > 0 and board_state[x - 1][y - 1] != null and not board_state[x - 1][y - 1].is_white:
			valid_moves.append(Vector2(x - 1, y - 1))
		if x < 7 and y > 0 and board_state[x + 1][y - 1] != null and not board_state[x + 1][y - 1].is_white:
			valid_moves.append(Vector2(x + 1, y - 1))
		
		# Check if the pawn can move diagonally without capturing
		if Upgrade.DIAGONAL_MOVE in upgrades:
			if x > 0 and y > 0 and board_state[x - 1][y - 1] == null:
				valid_moves.append(Vector2(x - 1, y - 1))
			if x < 7 and y > 0 and board_state[x + 1][y - 1] == null:
				valid_moves.append(Vector2(x + 1, y - 1))

		# Check for promotion
		if y == 0:
			board_state[x][y] = Queen.new(is_white, Vector2(x, y))



	else:
		if y < 1 and board_state[x][y + 1] == null:
			valid_moves.append(Vector2(x, y + 1))
			if board_state[x][y + 2] == null:
				valid_moves.append(Vector2(x, y + 2))
		elif board_state[x][y + 1] == null: 
			valid_moves.append(Vector2(x, y + 1))

		if x > 0 and y < 7 and board_state[x - 1][y + 1] != null and board_state[x - 1][y + 1].is_white:
			valid_moves.append(Vector2(x - 1, y + 1))
		if x < 7 and y < 7 and board_state[x + 1][y + 1] != null and board_state[x + 1][y + 1].is_white:
			valid_moves.append(Vector2(x + 1, y + 1))

		# Check for promotion
		# if y == 7:
		# 	board_state[x][y] = Queen.new(is_white, Vector2(x, y))

	return valid_moves