import * as THREE from 'three';
import { state } from '../state.js';
import { createRoomWalls } from './room-walls.js';
import { createBookshelf } from './bookshelf.js';
import { createDoor } from './door.js';
import { createTvSet } from './tv-set.js';
import { PictureFrame } from './PictureFrame.js';
import painting1 from '/textures/painting1.jpg';
import painting2 from '/textures/painting2.jpg';
import floorTextureUrl from '/textures/floor.jpg';

// --- Scene Modeling Function ---
export function createSceneObjects() {
    // --- Materials (MeshPhongMaterial) ---
    const darkMetal = new THREE.MeshPhongMaterial({ 
        color: 0x6b6b6b, 
        shininess: 80, 
        specular: 0x888888 
    });
    
    // --- 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: 0x555555, 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(0x111111, 1); 
    state.scene.add(ambientLight);

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


    createTvSet(-state.roomSize/2 + 1.2, -state.roomSize/2 + 0.8, Math.PI * 0.1);

    // --- 5. Standing Lamp ---
    const lampBase = new THREE.CylinderGeometry(0.05, 0.2, 0.1, 12);
    const lampPole = new THREE.CylinderGeometry(0.02, 0.02, 1.5, 8);
    const lampShade = new THREE.ConeGeometry(0.2, 0.4, 16);

    const baseMesh = new THREE.Mesh(lampBase, darkMetal);
    const poleMesh = new THREE.Mesh(lampPole, darkMetal);
    const shadeMesh = new THREE.Mesh(lampShade, darkMetal);

    // Ensure lamp parts cast shadows
    baseMesh.castShadow = true; baseMesh.receiveShadow = true;
    poleMesh.castShadow = true; poleMesh.receiveShadow = true;
    //shadeMesh.castShadow = true; shadeMesh.receiveShadow = true;

    baseMesh.position.y = -0.6;
    poleMesh.position.y = 0.0;
    shadeMesh.position.y = 0.8 + 0.1;
    shadeMesh.rotation.x = Math.PI;

    // Lamp Light (Warm Glow) - Configured to cast shadows
    state.lampLightPoint = new THREE.PointLight(0xffaa00, state.originalLampIntensity, 4); 
    state.lampLightPoint.position.set(-0.01, state.roomHeight-0.9, 0.01); 
    state.lampLightPoint.castShadow = true;
    // Optimization: Reduced map size and far plane to ease resource burden
    state.lampLightPoint.shadow.mapSize.width = 512; 
    state.lampLightPoint.shadow.mapSize.height = 512;
    state.lampLightPoint.shadow.camera.near = 0.1;
    state.lampLightPoint.shadow.camera.far = 4; // Matches the light's attenuation distance (4)
    state.lampLightPoint.penumbra = 0.5;

    state.lampLightSpot = new THREE.SpotLight(0xffaa00, state.originalLampIntensity, 4); 
    state.lampLightSpot.position.set(-0.01, 1.0, 0.01); 
    state.lampLightSpot.target.position.set(0, 5, 0);
    state.lampLightSpot.castShadow = true;
    // Optimization: Reduced map size and far plane to ease resource burden
    state.lampLightSpot.shadow.mapSize.width = 512; 
    state.lampLightSpot.shadow.mapSize.height = 512;
    state.lampLightSpot.shadow.camera.near = 0.1;
    state.lampLightSpot.shadow.camera.far = 4; // Matches the light's attenuation distance (4)
    state.lampLightSpot.penumbra = 1;
    
    const lampGroup = new THREE.Group();
    lampGroup.add(baseMesh, poleMesh, shadeMesh, state.lampLightSpot, state.lampLightSpot.target, state.lampLightPoint);
    lampGroup.position.set(0.8, 0.7, -state.roomSize/2+0.5); 

    state.scene.add(lampGroup);

    state.landingSurfaces.push(shadeMesh);

    // --- 7. Old Camera (On the table) ---
    const cameraBody = new THREE.BoxGeometry(0.4, 0.3, 0.15);
    const cameraLens = new THREE.CylinderGeometry(0.08, 0.08, 0.05, 12);
    const cameraMaterial = new THREE.MeshPhongMaterial({ 
        color: 0x333333, 
        shininess: 50, 
        specular: 0x444444 
    });

    const cameraMesh = new THREE.Mesh(cameraBody, cameraMaterial);
    const lensMesh = new THREE.Mesh(cameraLens, cameraMaterial);
    lensMesh.position.z = 0.15;
    lensMesh.rotation.x = Math.PI/2;

    cameraMesh.add(lensMesh);
    cameraMesh.position.set(-1.7, 0.15, 0.4);
    cameraMesh.rotation.y = -Math.PI / 10;
    cameraMesh.castShadow = true; cameraMesh.receiveShadow = true;
    state.scene.add(cameraMesh);

    // --- 8. Pizza Box ---
    const boxGeometry = new THREE.BoxGeometry(0.5, 0.05, 0.5);
    const boxMaterial = new THREE.MeshPhongMaterial({ color: 0xe0c896, shininess: 5 });
    const pizzaBox = new THREE.Mesh(boxGeometry, boxMaterial);
    pizzaBox.position.set(-1.8, 0.025, -0.8);
    pizzaBox.rotation.y = Math.PI / 5;
    pizzaBox.castShadow = true; pizzaBox.receiveShadow = true;
    state.scene.add(pizzaBox);

    // --- 8. Cassette ---
    const cassetteGeometry = new THREE.BoxGeometry(0.2, 0.05, 0.45);
    const cassetteMaterial = new THREE.MeshPhongMaterial({ color: 0xe0c896, shininess: 5 });
    const cassette = new THREE.Mesh(cassetteGeometry, cassetteMaterial);
    cassette.position.set(-0.5, 0.025, -1.4);
    cassette.rotation.y = Math.PI / 3;
    cassette.castShadow = true; cassette.receiveShadow = true;
    state.scene.add(cassette);

    createDoor(state.roomSize/2, -state.roomSize/2 * 0.5, -Math.PI/2);
    createBookshelf(-state.roomSize/2 + 0.2, state.roomSize/2*0.2, 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);
    
    const pictureFrame = new PictureFrame(state.scene, {
        position: new THREE.Vector3(-state.roomSize/2, 2.0, -state.roomSize/2 + 1.5),
        width: 1.5,
        height: 1,
        imageUrls: [painting1, painting2],
        rotationY: Math.PI / 2
    });
    state.pictureFrames.push(pictureFrame);

    const pictureFrame2 = new PictureFrame(state.scene, {
        position: new THREE.Vector3(state.roomSize/2, 2.0, 0.3),
        width: 1.5,
        height: 1,
        imageUrls: [painting2, painting1],
        rotationY: -Math.PI / 2
    });
    state.pictureFrames.push(pictureFrame2);
}
