import { Context, getParam } from "@needle-tools/engine";
import { Mesh } from "three";

export const debug = getParam("debugmaterialx");





/**
 * =====================================
 * Unused
 */

// Patch WebGL2 methods for debugging purposes

const patchWebGL2 = () => {
    const getUniformLocation = WebGL2RenderingContext.prototype.getUniformLocation;
    const programAndNameToUniformLocation = new WeakMap<WebGLUniformLocation, { program: WebGLProgram, name: string }>();
    WebGL2RenderingContext.prototype.getUniformLocation = function (program: WebGLProgram, name: string) {
        const location = getUniformLocation.call(this, program, name);
        if (location) {
            programAndNameToUniformLocation.set(location, { program, name });
        }
        return location;
    };

    const uniform4fv = WebGL2RenderingContext.prototype.uniform4fv;
    WebGL2RenderingContext.prototype.uniform4fv = function (location: WebGLUniformLocation | null, v: Float32Array | number[]) {
        if (location) {
            const uniformName = programAndNameToUniformLocation.get(location);
            if (true) console.log("Calling uniform4fv", { location, v, name: uniformName?.name });
        }
        return uniform4fv.call(this, location, v);
    };
};
// patchWebGL2();


// TODO doesn't actually reset yet...
function resetShaders(ctx: Context) {
    const scene = ctx.scene;
    const gl = ctx.renderer;
    console.log(gl.properties, gl.info)
    scene.traverse(object => {
        if ((object as Mesh).isMesh) {
            const mesh = object as Mesh;
            if (Array.isArray(mesh.material)) {
                mesh.material.forEach(mat => gl.properties.remove(mat));
            }
            else {
                gl.properties.remove(mesh.material);
            }
        }
    })
    if (gl.info?.programs)
        gl.info.programs.length = 0;
}