import type { ToneMappingEffect as _TonemappingEffect, ToneMappingMode } from "postprocessing";
import type { ToneMapping } from "three";

import { MODULES } from "../../../engine/engine_modules.js";
import { serializable } from "../../../engine/engine_serialization.js";
import { nameToThreeTonemapping } from "../../../engine/engine_tonemapping.js";
import { getParam } from "../../../engine/engine_utils.js";
import { EffectProviderResult, PostProcessingEffect } from "../PostProcessingEffect.js";
import { VolumeParameter } from "../VolumeParameter.js";
import { registerCustomEffectType } from "../VolumeProfile.js";
import { NEToneMappingMode, NEToneMappingModeNames, threeToNeedleToneMapping, threeToneMappingToEffectMode, toThreeToneMapping } from "./Tonemapping.utils.js";

const debug = getParam("debugpost");


/**
 * [ToneMappingEffect](https://engine.needle.tools/docs/api/ToneMappingEffect) adjusts the brightness and contrast of the rendered scene to map high dynamic range (HDR) colors to a displayable range.
 * This effect is essential for achieving realistic lighting and color representation in 3D scenes, as it helps to preserve details in both bright and dark areas.
 * Various tonemapping algorithms can be applied to achieve different visual styles and effects.
 * @summary Tonemapping Post-Processing Effect
 * @category Effects
 * @group Components
 */
export class ToneMappingEffect extends PostProcessingEffect {

    get typeName() {
        return "ToneMapping";
    }

    @serializable(VolumeParameter)
    readonly mode: VolumeParameter = new VolumeParameter(undefined);

    @serializable(VolumeParameter)
    readonly exposure: VolumeParameter = new VolumeParameter(1);

    /** Set the tonemapping mode to e.g. "agx" */
    setMode(mode: NEToneMappingModeNames) {
        const enumValue = NEToneMappingMode[mode as NEToneMappingModeNames];
        if (enumValue === undefined) {
            console.error("[PostProcessing] Invalid ToneMapping mode", mode);
            return this;
        }
        this.mode.value = enumValue;
        return this;
    }

    get isToneMapping() { return true; }

    /** The three.js ToneMapping enum value resolved from the current mode */
    get threeToneMapping(): ToneMapping {
        if (this.mode.isInitialized && this.mode.overrideState)
            return toThreeToneMapping(this.mode.value);
        return this.context.renderer.toneMapping as ToneMapping;
    }

    /** The exposure value to apply */
    get toneMappingExposure(): number {
        if (this.exposure.overrideState && this.exposure.value !== undefined)
            return Math.max(0.0, this.exposure.value);
        return this.context.renderer.toneMappingExposure;
    }

    private _tonemappingEffect: _TonemappingEffect | null = null;
    onCreateEffect(): EffectProviderResult | undefined {

        // ensure the effect tonemapping value is initialized
        if (this.mode.isInitialized == false) {
            const mode = threeToNeedleToneMapping(this.context.renderer.toneMapping);
            if (debug) console.log("[PostProcessing] Initializing ToneMapping mode to renderer.toneMapping", this.context.renderer.toneMapping + " → " + mode);
            this.mode.initialize(mode);
        }

        this._tonemappingEffect?.dispose();

        const threeMode = toThreeToneMapping(this.mode.value);
        const tonemapping = this._tonemappingEffect = new MODULES.POSTPROCESSING.MODULE.ToneMappingEffect({
            mode: threeToneMappingToEffectMode(threeMode),
        });
        this.mode.onValueChanged = (newValue) => {
            if (typeof newValue === "string") {
                newValue = nameToThreeTonemapping(newValue);
                tonemapping.mode = threeToneMappingToEffectMode(newValue);
            }
            else {
                const threeMode = toThreeToneMapping(newValue);
                tonemapping.mode = threeToneMappingToEffectMode(threeMode);
            }
            tonemapping.name = "ToneMapping (" + NEToneMappingMode[newValue] + ")";
            if (debug) console.log("[PostProcessing] ToneMapping mode changed to", NEToneMappingMode[newValue], threeMode, tonemapping.mode);
        };


        if (debug) console.log("[PostProcessing] Use ToneMapping", NEToneMappingMode[this.mode.value], threeMode, tonemapping.mode, "renderer.tonemapping: " + this.context.renderer.toneMapping);

        return tonemapping;
    }

}

registerCustomEffectType("Tonemapping", ToneMappingEffect);
