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) {
            // --- Procedural Flasks ---
            const flaskRadius = 0.03 + seededRandom() * 0.04;
            const flaskHeight = (shelfSpacing * 0.5) + seededRandom() * (shelfSpacing * 0.3);
            const neckHeight = flaskHeight * (0.4 + seededRandom() * 0.2);
            const neckRadius = flaskRadius * (0.4 + seededRandom() * 0.2);

            if (currentBookX + flaskRadius * 2 > internalWidth / 2) break;

            const flaskGroup = new THREE.Group();

            // 1. Glass Material
            const glassMaterial = new THREE.MeshPhongMaterial({
                color: 0xdddddd,
                transparent: true,
                opacity: 0.3,
                shininess: 100,
                specular: 0xeeeeff
            });

            // 2. Flask Geometry (Sphere base + Cylinder neck)
            const sphereGeo = new THREE.SphereGeometry(flaskRadius, 16, 12);
            const sphereMesh = new THREE.Mesh(sphereGeo, glassMaterial);
            sphereMesh.position.y = flaskRadius;

            const neckGeo = new THREE.CylinderGeometry(neckRadius, neckRadius, neckHeight, 12);
            const neckMesh = new THREE.Mesh(neckGeo, glassMaterial);
            neckMesh.position.y = flaskRadius * 2 + neckHeight / 2;

            flaskGroup.add(sphereMesh, neckMesh);

            // 3. Liquid inside
            const liquidFill = 0.5 + seededRandom() * 0.4; // 50% to 90% full
            const liquidColor = getRandomColor();
            const liquidMaterial = new THREE.MeshPhongMaterial({ color: liquidColor, emissive: liquidColor, emissiveIntensity: 0.3, shininess: 80 });
            const liquidGeo = new THREE.SphereGeometry(flaskRadius * 0.95, 16, 12, 0, Math.PI * 2, 0, Math.PI * liquidFill);
            const liquidMesh = new THREE.Mesh(liquidGeo, liquidMaterial);
            liquidMesh.position.y = flaskRadius;
            liquidMesh.rotation.z = Math.PI;
            flaskGroup.add(liquidMesh);

            // Position flask on shelf
            flaskGroup.position.set(
                currentBookX + flaskRadius,
                shelfSurfaceY,
                -shelfDepth / 2 + woodThickness + flaskRadius + (seededRandom() * 0.05)
            );

            flaskGroup.userData.originalY = flaskGroup.position.y;
            flaskGroup.userData.levitateOffset = 0;
            flaskGroup.userData.oscillationTime = Math.random() * Math.PI * 2;
            shelfGroup.add(flaskGroup);
            state.books.push(flaskGroup); // Add all flasks to levitation candidates

            currentBookX += flaskRadius * 2 + 0.02; // Gap between flasks
        }
    }

    state.scene.add(shelfGroup);
}