import { ROOMS, CORRIDORS, L_SHAPES, T_JUNCTIONS } from './TileDefinitions.js';

/**
 * DungeonDeck - Manages the deck of dungeon tiles
 * 
 * New approach: Deck contains abstract tile types, not specific tiles
 * When a type is drawn, we select a fitting variant from available options
 */

export class DungeonDeck {
    constructor() {
        this.deck = [];
        this.discardPile = [];
        this.shuffleDeck();
    }

    shuffleDeck() {
        // Create a deck with abstract tile types
        this.deck = [
            // 8 rooms 4x4 (abstract)
            { type: 'room_4x4' },
            { type: 'room_4x4' },
            { type: 'room_4x4' },
            { type: 'room_4x4' },
            { type: 'room_4x4' },
            { type: 'room_4x4' },
            { type: 'room_4x4' },
            { type: 'room_4x4' },

            // 4 rooms 4x6 (abstract)
            { type: 'room_4x6' },
            { type: 'room_4x6' },
            { type: 'room_4x6' },
            { type: 'room_4x6' },

            // 12 corridors (abstract)
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },
            { type: 'corridor' },

            // 10 L-shapes (abstract) - increased from 6
            { type: 'L' },
            { type: 'L' },
            { type: 'L' },
            { type: 'L' },
            { type: 'L' },
            { type: 'L' },
            { type: 'L' },
            { type: 'L' },
            { type: 'L' },
            { type: 'L' },

            // 8 T-junctions (abstract) - increased from 4
            { type: 'T' },
            { type: 'T' },
            { type: 'T' },
            { type: 'T' },
            { type: 'T' },
            { type: 'T' },
            { type: 'T' },
            { type: 'T' }
        ];

        // Fisher-Yates shuffle
        for (let i = this.deck.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [this.deck[i], this.deck[j]] = [this.deck[j], this.deck[i]];
        }
        console.log("Dungeon Deck Shuffled:", this.deck.length, "cards");
    }

    drawCard() {
        if (this.deck.length === 0) {
            console.warn("Deck empty! Reshuffling discards...");
            if (this.discardPile.length === 0) {
                console.error("No cards left!");
                return null;
            }
            this.deck = [...this.discardPile];
            this.discardPile = [];
            this.shuffleDeck();
        }

        const card = this.deck.pop();
        this.discardPile.push(card);
        return card;
    }

    /**
     * Draw a compatible abstract tile type
     * @param {string} originTileType - Type of the origin tile ('room', 'corridor', 'L', 'T')
     * @param {number} maxAttempts - Maximum number of cards to try
     * @returns {object|null} - Abstract tile card or null if none found
     */
    drawCompatibleCard(originTileType, maxAttempts = 10) {
        const validTypes = this.getCompatibleTypes(originTileType);

        for (let i = 0; i < maxAttempts && this.deck.length > 0; i++) {
            const card = this.drawCard();

            // Check if this abstract type is compatible
            if (validTypes.includes(card.type)) {
                return card;
            }

            // Put incompatible card back at the bottom of the deck
            this.deck.unshift(card);
            this.discardPile.pop();
        }

        console.warn(`Could not find compatible tile for ${originTileType} after ${maxAttempts} attempts`);
        return null;
    }

    /**
     * Get compatible tile types for a given origin type
     * @param {string} originType - Type of the origin tile
     * @returns {string[]} - Array of compatible abstract tile types
     */
    getCompatibleTypes(originType) {
        const connectionRules = {
            'room': ['room_4x4', 'room_4x6', 'corridor'],
            'corridor': ['room_4x4', 'room_4x6', 'corridor', 'L', 'T'],
            'L': ['corridor'],
            'T': ['corridor']
        };

        return connectionRules[originType] || [];
    }
}

export const dungeonDeck = new DungeonDeck();
