const sceneCtx = {
    scene: null,
    camera: null,
    renderer: null,
    ambient: null,
    directional: null,
    canvas: null
};

function _ambienteColor(ambiente) {
    if (ambiente === 'dia') return PALETA_PSX.cieloDia;
    if (ambiente === 'tarde') return PALETA_PSX.cieloTarde;
    return PALETA_PSX.cieloNoche;
}

function initScene(canvas) {
    sceneCtx.canvas = canvas;

    const scene = new THREE.Scene();
    scene.background = new THREE.Color(PALETA_PSX.cieloDia);
    scene.fog = new THREE.Fog(PALETA_PSX.cieloDia, FOG_NEAR, FOG_FAR);

    const camera = new THREE.PerspectiveCamera(
        CAMERA_FOV,
        window.innerWidth / window.innerHeight,
        CAMERA_NEAR,
        CAMERA_FAR
    );
    camera.position.set(0, CAMERA_OFFSET_Y, CAMERA_OFFSET_Z);
    camera.lookAt(0, 1.2, -CAMERA_LOOK_AHEAD);

    const renderer = new THREE.WebGLRenderer({
        canvas: canvas,
        antialias: false,
        powerPreference: 'low-power'
    });
    renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
    renderer.setSize(window.innerWidth, window.innerHeight, false);
    if (renderer.outputColorSpace !== undefined) {
        renderer.outputColorSpace = THREE.SRGBColorSpace;
    }

    const ambient = new THREE.AmbientLight(0xffffff, 0.85);
    const directional = new THREE.DirectionalLight(0xffffff, 0.6);
    directional.position.set(-3, 8, 4);
    scene.add(ambient);
    scene.add(directional);

    sceneCtx.scene = scene;
    sceneCtx.camera = camera;
    sceneCtx.renderer = renderer;
    sceneCtx.ambient = ambient;
    sceneCtx.directional = directional;

    window.addEventListener('resize', onResize);
    onResize();
}

function aplicarAmbiente(ambiente) {
    if (!sceneCtx.scene) return;
    const color = new THREE.Color(_ambienteColor(ambiente));
    sceneCtx.scene.background = color;
    sceneCtx.scene.fog = new THREE.Fog(color, FOG_NEAR, FOG_FAR);

    if (ambiente === 'noche') {
        sceneCtx.ambient.intensity = 0.4;
        sceneCtx.directional.intensity = 0.25;
        sceneCtx.directional.color = new THREE.Color(0xffd6a0);
    } else if (ambiente === 'tarde') {
        sceneCtx.ambient.intensity = 0.7;
        sceneCtx.directional.intensity = 0.55;
        sceneCtx.directional.color = new THREE.Color(0xffd6a0);
    } else {
        sceneCtx.ambient.intensity = 0.85;
        sceneCtx.directional.intensity = 0.6;
        sceneCtx.directional.color = new THREE.Color(0xffffff);
    }
}

function onResize() {
    if (!sceneCtx.renderer) return;
    const w = window.innerWidth;
    const h = window.innerHeight;
    sceneCtx.camera.aspect = w / h;
    sceneCtx.camera.updateProjectionMatrix();
    sceneCtx.renderer.setSize(w, h, false);
}

function renderScene() {
    if (!sceneCtx.renderer) return;
    sceneCtx.renderer.render(sceneCtx.scene, sceneCtx.camera);
}

function detectarWebGL() {
    try {
        const canvas = document.createElement('canvas');
        return !!(canvas.getContext('webgl') || canvas.getContext('experimental-webgl'));
    } catch (e) {
        return false;
    }
}
