import { DungeonGenerator } from '../dungeon/DungeonGenerator.js';

/**
 * GameEngine for Manual Dungeon Construction with Player Movement
 */
export class GameEngine {
    constructor() {
        this.dungeon = new DungeonGenerator();
        this.player = null;
        this.selectedEntity = null;
        this.isRunning = false;

        // Callbacks
        this.onEntityUpdate = null;
        this.onEntityMove = null;
        this.onEntitySelect = null;
        this.onPathChange = null;
    }

    startMission(missionConfig) {
        console.log('[GameEngine] Starting mission:', missionConfig.name);
        this.dungeon.startDungeon(missionConfig);

        // Create player at center of first tile
        this.createPlayer(1.5, 2.5); // Center of 2x6 corridor

        this.isRunning = true;
    }

    createPlayer(x, y) {
        this.player = {
            id: 'p1',
            name: 'Barbarian',
            x: Math.floor(x),
            y: Math.floor(y),
            texturePath: '/assets/images/dungeon1/standees/barbaro.png'
        };

        if (this.onEntityUpdate) {
            this.onEntityUpdate(this.player);
        }

        console.log('[GameEngine] Player created at', this.player.x, this.player.y);
    }

    onCellClick(x, y) {
        // If no player selected, select player on click
        if (!this.selectedEntity && this.player && x === this.player.x && y === this.player.y) {
            this.selectedEntity = this.player;
            if (this.onEntitySelect) {
                this.onEntitySelect(this.player.id, true);
            }
            console.log('[GameEngine] Player selected');
            return;
        }

        // If player selected, move to clicked cell
        if (this.selectedEntity === this.player) {
            if (this.canMoveTo(x, y)) {
                this.movePlayer(x, y);
            } else {
                console.log('[GameEngine] Cannot move there');
            }
        }
    }

    canMoveTo(x, y) {
        // Check if cell is walkable (occupied by a tile)
        return this.dungeon.grid.isOccupied(x, y);
    }

    movePlayer(x, y) {
        // Simple direct movement (no pathfinding for now)
        const path = [{ x, y }];

        this.player.x = x;
        this.player.y = y;

        if (this.onEntityMove) {
            this.onEntityMove(this.player, path);
        }

        // Deselect after move
        this.selectedEntity = null;
        if (this.onEntitySelect) {
            this.onEntitySelect(this.player.id, false);
        }

        console.log('[GameEngine] Player moved to', x, y);
    }

    isPlayerAdjacentToDoor(doorExit) {
        if (!this.player) return false;

        const dx = Math.abs(this.player.x - doorExit.x);
        const dy = Math.abs(this.player.y - doorExit.y);

        // Adjacent means distance of 1 in one direction and 0 in the other
        return (dx === 1 && dy === 0) || (dx === 0 && dy === 1);
    }

    update(time) {
        // Minimal update loop
    }
}
