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

import lombok.extern.slf4j.Slf4j;
import se.urmo.game.entities.ghost.Ghost;
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap;

@Slf4j
public class ScatterGhostMode extends AbstractGhostModeImpl {
    // Time in scatter mode before returning to chase
    private static final int SCATTER_DURATION = 7 * 60; // 7 seconds at 60 ticks/second
    private int scatterTimer = 0;

    public ScatterGhostMode(Ghost ghost, GhostStrategy scaterStrategy) {
        super(ghost, scaterStrategy, ghost.getLevelManager(), ghost.getAnimation());
    }

    @Override
    public void update(Ghost ghost, PacMan pacman, GameMap map) {
        // Update scatter timer
        updateScatterTimer(ghost);

        // Use common position update with scatter strategy
        updatePosition(ghost, pacman, map);
    }

    @Override
    public double getSpeed() {
        return Ghost.BASE_SPEED * levelManager.getGhostSpeed();
    }

    private void updateScatterTimer(Ghost ghost) {
        scatterTimer--;
        if (scatterTimer <= 0) {
            log.debug("Scatter mode timed out, returning to chase");
            ghost.requestModeChange(GhostMode.CHASE);
            scatterTimer = SCATTER_DURATION;
        }
    }

//    @Override
//    public void enter(Ghost ghost) {
//        log.debug("Entering scatter mode");
//
//        // Initialize scatter mode timer
//        scatterTimer = SCATTER_DURATION;
//    }

    /**
     * Reset the scatter timer (for extending scatter mode)
     */
    public void resetTimer() {
        scatterTimer = SCATTER_DURATION;
    }
}