import * as THREE from 'three';
import { state } from '../state.js';
import { getRandomColor, seededRandom } from '../utils.js';

export function createBookshelf(x, z, rotationY, uniqueSeed) {
    state.seed = uniqueSeed; // Reset seed for this specific shelf instance

    const shelfHeight = 2.2;
    const shelfDepth = 0.35;
    const shelfWidth = 1.2;
    const numShelves = 6;
    const woodThickness = 0.04;
    const woodColor = 0x5c4033; // Darker, richer wood

    const shelfGroup = new THREE.Group();
    shelfGroup.position.set(x, 0, z);
    shelfGroup.rotation.y = rotationY;

    const woodMaterial = new THREE.MeshPhongMaterial({ color: woodColor, shininess: 30 });

    // 1. Build Frame (Hollow box)
    // Back Panel
    const backGeo = new THREE.BoxGeometry(shelfWidth, shelfHeight, woodThickness);
    const backPanel = new THREE.Mesh(backGeo, woodMaterial);
    backPanel.position.set(0, shelfHeight / 2, -shelfDepth / 2 + woodThickness / 2);
    backPanel.castShadow = true;
    backPanel.receiveShadow = true;
    shelfGroup.add(backPanel);

    // Side Panels (Left & Right)
    const sideGeo = new THREE.BoxGeometry(woodThickness, shelfHeight, shelfDepth);
    const leftSide = new THREE.Mesh(sideGeo, woodMaterial);
    leftSide.position.set(-shelfWidth / 2 + woodThickness / 2, shelfHeight / 2, 0);
    leftSide.castShadow = true;
    leftSide.receiveShadow = true;
    shelfGroup.add(leftSide);

    const rightSide = new THREE.Mesh(sideGeo, woodMaterial);
    rightSide.position.set(shelfWidth / 2 - woodThickness / 2, shelfHeight / 2, 0);
    rightSide.castShadow = true;
    rightSide.receiveShadow = true;
    shelfGroup.add(rightSide);

    // Top & Bottom Panels
    const topBottomGeo = new THREE.BoxGeometry(shelfWidth, woodThickness, shelfDepth);
    const bottomPanel = new THREE.Mesh(topBottomGeo, woodMaterial);
    bottomPanel.position.set(0, woodThickness / 2, 0);
    bottomPanel.receiveShadow = true;
    shelfGroup.add(bottomPanel);

    const topPanel = new THREE.Mesh(topBottomGeo, woodMaterial);
    topPanel.position.set(0, shelfHeight - woodThickness / 2, 0);
    topPanel.castShadow = true;
    shelfGroup.add(topPanel);

    state.landingSurfaces.push(topPanel);

    // 2. Individual Shelves & Books
    const internalHeight = shelfHeight - (2 * woodThickness);
    const shelfSpacing = internalHeight / numShelves;
    const internalWidth = shelfWidth - (2 * woodThickness);

    for (let i = 0; i < numShelves; i++) {
        const currentShelfY = woodThickness + (i * shelfSpacing);

        // Shelf board (skip for the very bottom one as we have a bottom panel)
        if (i > 0) {
            const shelfBoard = new THREE.Mesh(
                new THREE.BoxGeometry(internalWidth, woodThickness, shelfDepth - woodThickness), // Slightly shallower to fit inside back panel
                woodMaterial
            );
            shelfBoard.position.set(0, currentShelfY, woodThickness / 2); // Offset forward slightly
            shelfBoard.castShadow = true;
            shelfBoard.receiveShadow = true;
            shelfGroup.add(shelfBoard);
        }

        // 3. Procedural Books
        let currentBookX = -internalWidth / 2 + 0.01; // Start at left inside edge
        const shelfSurfaceY = currentShelfY + woodThickness / 2;

        while (currentBookX < internalWidth / 2 - 0.05) {
            // sizes vary
            const bookWidth = 0.02 + seededRandom() * 0.05;
            const bookHeight = (shelfSpacing * 0.6) + seededRandom() * (shelfSpacing * 0.1);
            const bookDepth = 0.15 + seededRandom() * 0.03;

            if (currentBookX + bookWidth > internalWidth / 2) break;

            const bookColor = getRandomColor();
            const bookMat = new THREE.MeshPhongMaterial({ color: bookColor, shininess: 60 });
            const bookGeo = new THREE.BoxGeometry(bookWidth, bookHeight, bookDepth);
            const book = new THREE.Mesh(bookGeo, bookMat);

            // Position: Resting on shelf, pushed towards the back with slight random variation
            const depthVariation = seededRandom() * 0.05;
            book.position.set(
                currentBookX + bookWidth / 2,
                shelfSurfaceY + bookHeight / 2,
                -shelfDepth / 2 + woodThickness + bookDepth / 2 + depthVariation
            );
            
            book.castShadow = true;
            book.receiveShadow = true;
            // Store original Y position and animation data
            book.userData.originalY = book.position.y;
            book.userData.levitateOffset = 0;
            book.userData.oscillationTime = Math.random() * Math.PI * 2; // Start at random phase
            shelfGroup.add(book);

            if (Math.random() > 0.8) {
                state.books.push(book);
            }

            currentBookX += bookWidth + 0.002; // Tiny gap between books
            
            if (seededRandom() > 0.92) {
                currentBookX += bookWidth * 3; // random bigger gaps
            }
        }
    }

    state.scene.add(shelfGroup);
}