import { Color, Fog as Fog3 } from "three";

import { serializable } from "../engine/engine_serialization.js";
import { Behaviour } from "./Component.js";


/**
 * Fog rendering mode
 */
enum FogMode {
    /** Linear fog increases uniformly with distance */
    Linear = 1,
    /** Exponential fog increases exponentially with distance */
    Exponential = 2,
    /** Exponential squared fog for denser falloff */
    ExponentialSquared = 3,
}

/**
 * Adds distance-based fog effect to the scene.  
 * When enabled, objects will fade into the fog color based on their distance from the camera.    
 * 
 * This component is automatically added to the scene when fog is enabled in the editor.   
 * For setting fog from code you can simply use `scene.fog = new Fog3(color, near, far)` without adding this component.
 *
 * @summary Adds fog effect to the scene
 * @category Rendering
 * @group Components
 * @link https://threejs.org/docs/#Fog
 */
export class Fog extends Behaviour {

    /**
     * The underlying Three.js Fog object. You can modify its properties directly for more advanced control.
     * @remarks The Fog component provides convenient access to common fog properties like `near`, `far`, and `color`. Modifying those will update the underlying `fog` object accordingly. However, you can also access and modify the `fog` object directly for more advanced use cases, such as changing the fog mode or using a custom shader.  
     * @link https://threejs.org/docs/#Fog for available properties and methods on the Fog object.
     */
    get fog() {
        if (!this._fog) this._fog = new Fog3(0x000000, 0, 50);
        return this._fog;
    }

    get mode() {
        return FogMode.Linear;
    }

    @serializable()
    set near(value: number) {
        this.fog.near = value;
    }
    get near() {
        return this.fog.near;
    }

    @serializable()
    set far(value: number) {
        this.fog.far = value;
    }
    get far() {
        return this.fog.far;
    }

    @serializable(Color)
    set color(value: Color) {
        this.fog.color.copy(value);
    }
    get color() {
        return this.fog.color;
    }

    private _fog?: Fog3;

    onEnable() {
        this.scene.fog = this.fog;
    }

    onDisable() {
        if (this.scene.fog === this._fog)
            this.scene.fog = null;
    }


}