import * as THREE from 'three';
import { state } from '../state.js';
import { createRoomWalls } from './room-walls.js';
import { createBookshelf } from './bookshelf.js';
import { createMagicMirror } from './magic-mirror.js';
import { createFireplace } from './fireplace.js';
import { createTable } from './table.js';
import { createCauldron } from './cauldron.js';
import { createRats } from './rat.js';
import { createLectern } from './lectern.js';
import { PictureFrame } from './PictureFrame.js';
import painting1 from '/textures/painting1.jpg';
import painting2 from '/textures/painting2.jpg';
import painting3 from '/textures/painting3.jpg';
import floorTextureUrl from '/textures/stone_floor.png';
import tableTextureUrl from '/textures/wood.png';

// --- Scene Modeling Function ---
export function createSceneObjects() {
    // --- Materials (MeshPhongMaterial) ---
    const darkMetal = new THREE.MeshPhongMaterial({ 
        color: 0x6b6b6b, 
        shininess: 80, 
        specular: 0x888888 
    });

    const woodMaterial = new THREE.MeshPhongMaterial({
        map: state.loader.load(tableTextureUrl),
        shininess: 10,
        specular: 0x222222
    });
    
    // --- 1. Floor ---
    const floorGeometry = new THREE.PlaneGeometry(20, 20);
    const floorTexture = state.loader.load(floorTextureUrl);
    floorTexture.wrapS = THREE.RepeatWrapping;
    floorTexture.wrapT = THREE.RepeatWrapping;
    floorTexture.repeat.set(state.roomSize, state.roomSize);
    const floorMaterial = new THREE.MeshPhongMaterial({ map: floorTexture, color: 0xaaaaaa, shininess: 5 }); 
    const floor = new THREE.Mesh(floorGeometry, floorMaterial);
    floor.rotation.x = -Math.PI / 2;
    floor.position.y = 0;
    floor.receiveShadow = true; 
    state.scene.add(floor);

    state.landingSurfaces.push(floor);

    createRoomWalls();  
    
    // 3. Lighting (Minimal and focused)
    const ambientLight = new THREE.AmbientLight(0x404040, 1); 
    state.scene.add(ambientLight);

    const roomLight = new THREE.PointLight(0xffdcb4, 0.8, state.roomSize); 
    roomLight.position.set(0, 1.8, 0); 
    state.scene.add(roomLight);


    createMagicMirror(-0.1, -state.roomSize/2 + 0.3, 0.2);

    // --- 5. Candle ---
    const candleStickGeo = new THREE.CylinderGeometry(0.05, 0.06, 0.3, 12);
    const candleStick = new THREE.Mesh(candleStickGeo, darkMetal);
    candleStick.castShadow = true;
    candleStick.receiveShadow = true;

    // Candle Light (Warm Glow)
    state.candleLight = new THREE.PointLight(0xffaa00, state.originalLampIntensity, 4); 
    state.candleLight.position.set(0, 0.2, 0); 
    state.candleLight.castShadow = true;
    state.candleLight.shadow.mapSize.width = 512; 
    state.candleLight.shadow.mapSize.height = 512;
    state.candleLight.shadow.camera.near = 0.1;
    state.candleLight.shadow.camera.far = 4;
    state.candleLight.penumbra = 0.5;
    
    const candleGroup = new THREE.Group();
    candleGroup.add(candleStick, state.candleLight);
    candleGroup.position.set(0.8, 0.15, -state.roomSize/2+0.6); 

    state.scene.add(candleGroup);

    createTable(-1.8, 0, -1.2, Math.PI / 2.3);

    // Add cauldron on top of the table (Y = table height + cauldron radius)
    createCauldron(-1.8, 0.5 + 0.2, -0.8);

    // --- 8. Timber Frames ---
    const beamThickness = 0.15;
    const beamDepth = 0.2;

    // Ceiling Beams
    const ceilingBeamGeoX = new THREE.BoxGeometry(state.roomSize, beamDepth * 1.3, beamThickness);
    const ceilingBeamGeoZ = new THREE.BoxGeometry(beamThickness, beamDepth * 0.8, state.roomSize);

    const createBeam = (geo, pos, rotY = 0) => {
        const beam = new THREE.Mesh(geo, woodMaterial);
        beam.position.copy(pos);
        beam.rotation.y = rotY;
        beam.castShadow = true;
        beam.receiveShadow = true;
        state.scene.add(beam);
        return beam;
    };

    // Create a grid of ceiling beams
    // Beams along X-axis
    createBeam(ceilingBeamGeoX, new THREE.Vector3(0, state.roomHeight - beamDepth / 2, 0));
    createBeam(ceilingBeamGeoX, new THREE.Vector3(0, state.roomHeight - beamDepth / 2, -state.roomSize / 2 + 1.5));
    createBeam(ceilingBeamGeoX, new THREE.Vector3(0, state.roomHeight - beamDepth / 2, state.roomSize / 2 - 1.5));
    // Beams along Z-axis
    createBeam(ceilingBeamGeoZ, new THREE.Vector3(-state.roomSize / 2 + 1.5, state.roomHeight - beamDepth / 2, 0));
    createBeam(ceilingBeamGeoZ, new THREE.Vector3(state.roomSize / 2 - 1.5, state.roomHeight - beamDepth / 2, 0));

    // Wall Beams (Vertical)
    const wallBeamGeo = new THREE.BoxGeometry(beamThickness, state.roomHeight, beamDepth);

    // Back Wall
    createBeam(wallBeamGeo, new THREE.Vector3(-1, state.roomHeight / 2, -state.roomSize / 2 + beamDepth / 2));
    createBeam(wallBeamGeo, new THREE.Vector3(1, state.roomHeight / 2, -state.roomSize / 2 + beamDepth / 2));

    // Left Wall
    createBeam(wallBeamGeo, new THREE.Vector3(-state.roomSize / 2 + beamDepth / 2, state.roomHeight / 2, -1), Math.PI / 2);
    createBeam(wallBeamGeo, new THREE.Vector3(-state.roomSize / 2 + beamDepth / 2, state.roomHeight / 2, 1), Math.PI / 2);

    // Right Wall
    createBeam(wallBeamGeo, new THREE.Vector3(state.roomSize / 2 - beamDepth / 2, state.roomHeight / 2, -1.5), -Math.PI / 2);
    createBeam(wallBeamGeo, new THREE.Vector3(state.roomSize / 2 - beamDepth / 2, state.roomHeight / 2, 1.5), -Math.PI / 2);

    // Wall Beams (Horizontal)
    const wallBeamGeoX = new THREE.BoxGeometry(state.roomSize, beamThickness, beamDepth * 1.3);
    const wallBeamGeoZ = new THREE.BoxGeometry(beamDepth * 1.3, beamThickness, state.roomSize);

    // Back Wall
    createBeam(wallBeamGeoX, new THREE.Vector3(0, state.roomHeight - 0.5, -state.roomSize / 2 + beamDepth / 2));

    // Left Wall
    createBeam(wallBeamGeoZ, new THREE.Vector3(-state.roomSize / 2 + beamDepth / 2, state.roomHeight - 0.5, 0));

    // Right Wall
    createBeam(wallBeamGeoZ, new THREE.Vector3(state.roomSize / 2 - beamDepth / 2, state.roomHeight - 0.5, 0));

    // --- 9. Fireplace ---
    createFireplace(state.roomSize / 2 - 0.5, -1, -Math.PI / 2);

    // --- Lectern ---
    createLectern(-state.roomSize/2 + 0.4, 0, 0.1, Math.PI / 2.3);

    createRats(state.roomSize/2 - 0.01, 0, 0.37, -Math.PI / 2);

    //createBookshelf(-state.roomSize/2 + 0.2, state.roomSize/2*0.1, Math.PI/2, 0);
    createBookshelf(-state.roomSize/2 + 0.2, state.roomSize/2*0.7, Math.PI/2, 0);
    createBookshelf(-state.roomSize/2 * 0.7, -state.roomSize/2+0.3, 0, 1);
    
    // next to bookshelf
    const pictureFrame = new PictureFrame(state.scene, {
        position: new THREE.Vector3(-state.roomSize/2, 1.7, -state.roomSize/2 + 0.8),
        width: 0.75,
        height: 1,
        imageUrls: [painting1, painting2],
        rotationY: Math.PI / 2
    });

    state.pictureFrames.push(pictureFrame);

    // on fireplace
    const pictureFrame2 = new PictureFrame(state.scene, {
        position: new THREE.Vector3(state.roomSize/2 - 0.9, 1.8, -1.1),
        width: 0.5,
        height: 0.7,
        imageUrls: [painting2, painting1, painting3],
        rotationY: -Math.PI / 2
    });
    state.pictureFrames.push(pictureFrame2);

    // next to fireplace
    const pictureFrame3 = new PictureFrame(state.scene, {
        position: new THREE.Vector3(state.roomSize/2, 1.7, 0.75),
        width: 0.7,
        height: 1.2,
        imageUrls: [painting3, painting2, painting1, painting3],
        rotationY: -Math.PI / 2
    });
    state.pictureFrames.push(pictureFrame3);
}
