import * as THREE from 'three';
import { state } from '../state.js';
import { onResizePostprocessing } from './postprocessing.js';
import sceneFeatureManager from '../scene/SceneFeatureManager.js';

function updateShaderTime() {
    if (state.tvScreen && state.tvScreen.material.uniforms && state.tvScreen.material.uniforms.u_time) {
        state.tvScreen.material.uniforms.u_time.value = state.clock.getElapsedTime();
    }
}

// --- Animation Loop ---
export function animate() {
    requestAnimationFrame(animate);

    const deltaTime = state.clock.getDelta();

    if (deltaTime > 0) {
        sceneFeatureManager.update(deltaTime);
        state.effectsManager.update(deltaTime);
        updateShaderTime();
    }

    // RENDER!
    if (state.composer) {
        state.composer.render();
    } else {
        state.renderer.render(state.scene, state.camera);
    }
}

// --- Window Resize Handler ---
export function onWindowResize() {
    state.camera.aspect = window.innerWidth / window.innerHeight;
    state.camera.updateProjectionMatrix();
    state.renderer.setSize(window.innerWidth, window.innerHeight);
    onResizePostprocessing();
}