/** @import { Vector3 } from 'three' */

// Global ATON and THREE

import AppState from "../state.js";

/**
 * @module Environment
 */

/**
 * @param {Vector3} vector - An object with x,y,z coordinates
 */
export function changeLightDirection(vector) {
    ATON.setMainLightDirection(vector);
}

/**
 * 
 * @param {Boolean} isEnabled 
 */
export function toggleAmbientOcclusion(isEnabled) {
    ATON.FX.togglePass(ATON.FX.PASS_AO, isEnabled);
    console.log('Ambient occlusion', isEnabled ? 'ON' : 'OFF');
}
/**
 * Slider to change light direction, based on ATON.UI
 * @param {String} direction - The axis direction, one of 'x','y','z'
 * @param {String} label - The slider label
 * @param {[number, number]} range - The slider's range
 * @param {Number} step - The slider's step
 */
export function createLightSlider(direction, label, range, step) {
    const currentVal = AppState.lightDirection[direction];

    const lightSlider = ATON.UI.createSlider({
        range,
        label,
        value: Number.parseFloat(currentVal).toPrecision(2),
        oninput: val => {
            const lightDir = AppState.lightDirection;

            // Keep existing direction values for the other axes
            lightDir[direction] = Number.parseFloat(val);
            changeLightDirection(lightDir);
            AppState.lightDirection = lightDir;
        },
    });

    lightSlider.classList.add('ms-1');
    const input = lightSlider.querySelector('input');
    if (input) input.step = step;

    return lightSlider;
}
/**
 * Slider to change the env exposure level, based on ATON.UI
 * @param {String} label - The slider label
 * @param {[number, number]} range - The slider's range
 * @param {Number} step - The slider's step
 */
export function createExposureSlider(label, range, step = 0.05) {
    const currentVal = String(AppState.exposure);

    const exposureSlider = ATON.UI.createSlider({
        range,
        label,
        value: Number.parseFloat(currentVal).toPrecision(1),
        oninput: val => {
            ATON.setExposure(val);
            AppState.exposure = val;

            console.debug('Current exposure:', ATON.getExposure());
        },
    });

    exposureSlider.classList.add('ms-1');
    let input = exposureSlider.querySelector('input');
    if (input) input.step = step;

    return exposureSlider;
}