package se.urmo.game.entities.ghost.strategy;

import se.urmo.game.entities.ghost.Ghost;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap;
import se.urmo.game.util.Direction;

import java.awt.Point;
import java.util.List;
import java.util.Random;

public class FearStrategy implements GhostStrategy {
    private final Random random = new Random();

    @Override
    public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
        // Frightened ghosts do not target Pacman.
        // Instead, they pick a random adjacent valid tile.
        Point ghostPos = ghost.getPosition().asPoint();
        List<Direction> neighbors = map.directionAlternatives(ghost, ghostPos.x, ghostPos.y);

        if (neighbors.isEmpty()) {
            return ghost.getPosition().asPoint(); // stuck
        }

        //Transform directions to actual Points
        List<Point> potentialTargets = neighbors.stream()
                .map(d -> new Point(
                        (int) (ghost.getPosition().x + d.dx * GameMap.MAP_TILESIZE),
                        (int) (ghost.getPosition().y + d.dy * GameMap.MAP_TILESIZE))).toList();

        // Pick a random valid neighbor
        return potentialTargets.get(random.nextInt(neighbors.size()));
    }
}
