import * as THREE from 'three';
import { state } from '../state.js';
import wallTextureUrl from '/textures/wall.jpg';

export function createRoomWalls() {
    const wallTexture = state.loader.load(wallTextureUrl);
    wallTexture.wrapS = THREE.RepeatWrapping;
    wallTexture.wrapT = THREE.RepeatWrapping;
    
    // USING MeshPhongMaterial for specular highlights on walls
    const wallMaterial = new THREE.MeshPhongMaterial({ 
        map: wallTexture, 
        side: THREE.FrontSide, 
        shininess: 5, 
        specular: 0x111111 // Subtle reflection
    });
    
    // 1. Back Wall (behind the TV)
    const backWall = new THREE.Mesh(new THREE.PlaneGeometry(state.roomSize, state.roomHeight), wallMaterial);
    backWall.position.set(0, state.roomHeight / 2, -state.roomSize / 2);
    backWall.receiveShadow = true; 
    backWall.name = 'backWall';
    state.scene.add(backWall);

    // 2. Front Wall (behind the camera)
    const frontWall = new THREE.Mesh(new THREE.PlaneGeometry(state.roomSize, state.roomHeight), wallMaterial);
    frontWall.position.set(0, state.roomHeight / 2, state.roomSize / 2);
    frontWall.rotation.y = Math.PI; 
    frontWall.name = 'frontWall';
    frontWall.receiveShadow = true;
    state.scene.add(frontWall);
    
    // 3. Left Wall
    const leftWall = new THREE.Mesh(new THREE.PlaneGeometry(state.roomSize, state.roomHeight), wallMaterial);
    leftWall.rotation.y = Math.PI / 2; 
    leftWall.name = 'leftWall';
    leftWall.position.set(-state.roomSize / 2, state.roomHeight / 2, 0);
    leftWall.receiveShadow = true;
    state.scene.add(leftWall);

    // 4. Right Wall
    const rightWall = new THREE.Mesh(new THREE.PlaneGeometry(state.roomSize, state.roomHeight), wallMaterial);
    rightWall.name = 'rightWall';
    rightWall.rotation.y = -Math.PI / 2; 
    rightWall.position.set(state.roomSize / 2, state.roomHeight / 2, 0);
    rightWall.receiveShadow = true;
    state.scene.add(rightWall);

    // 5. Ceiling 
    const ceilingGeometry = new THREE.PlaneGeometry(state.roomSize, state.roomSize);
    const ceilingTexture = wallTexture; 
    ceilingTexture.repeat.set(4, 4); 
    // USING MeshPhongMaterial
    const ceilingMaterial = new THREE.MeshPhongMaterial({ 
        map: ceilingTexture, 
        side: THREE.FrontSide,
        shininess: 5, 
        specular: 0x111111 
    });
    
    const ceiling = new THREE.Mesh(ceilingGeometry, ceilingMaterial);
    ceiling.rotation.x = Math.PI / 2; 
    ceiling.position.set(0, state.roomHeight, 0);
    ceiling.receiveShadow = true; 
    state.scene.add(ceiling);

    state.crawlSurfaces.push(backWall, leftWall, rightWall);
    
    // --- 6. Add a Window to the Back Wall ---
    const windowWidth = 1.5;
    const windowHeight = 1.2;
    const windowGeometry = new THREE.PlaneGeometry(windowWidth, windowHeight);

    const nightSkyMaterial = new THREE.MeshPhongMaterial({ 
        color: 0x0a1a3a, 
        emissive: 0x0a1a3a, 
        emissiveIntensity: 0.5,
        side: THREE.FrontSide
    });
    const windowPane = new THREE.Mesh(windowGeometry, nightSkyMaterial);

    const windowZ = -state.roomSize / 2 + 0.001; 
    windowPane.position.set(-3.5, state.roomHeight * 0.5 + 1.5, windowZ);
    state.scene.add(windowPane);
}