import * as THREE from 'three';
import { PostEffect as PostEffectParameter } from '../assets/marmosets/MviewFiles'
import { Pass } from 'three/examples/jsm/postprocessing/Pass';


export default class TestPass implements Pass  {

    enabled: boolean;
    needsSwap: boolean;
    clear: boolean;
    renderToScreen: boolean;

    fsQuad: Pass.FullScreenQuad
    material: THREE.ShaderMaterial;

    viewSize: THREE.Vector2;


    bloomTexture = new THREE.WebGLRenderTarget(256, 256)
    bloomFsQuad: Pass.FullScreenQuad;

    constructor() {

        this.enabled = true;
        this.needsSwap = false;
        this.clear = true;
        this.renderToScreen = true;

    
        this.viewSize = new THREE.Vector2();


        this.material = this.generateMaterial();
        this.fsQuad = new Pass.FullScreenQuad( this.material );


        
        this.bloomFsQuad = new Pass.FullScreenQuad( new THREE.ShaderMaterial({

            vertexShader: `
            
                varying vec2 vUv;

                void main() {

                    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

                }

            `,

            fragmentShader: `
            
                uniform sampler2D tInput;
                
                varying vec2 vUv;

                void main() {

                    //gl_FragColor = texture2D( tInput, vUv );
                    gl_FragColor = vec4(1.0,0.0,0.0,1.0);
                }


            `

        }))

    }


    build( parameters: PostEffectParameter ) {

      
    }

    

    private generateMaterial() {

        let material = new THREE.ShaderMaterial();
        material.defines = { }
        material.uniforms = {

            uTime: { value: 0  },
            tInput: { value: null },
            tBloom: {value: null }

        }
        material.vertexShader = `

            varying vec2 vUv;

            void main() {

                vUv = uv;
                gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

            }

        `

        material.fragmentShader = `
        
            precision mediump float;

            uniform float uTime;
            uniform sampler2D tInput;
            uniform sampler2D tBloom;
            
            varying vec2 vUv;
        
            void main() {

                vec4 color = texture2D(tInput, vUv);
                vec4 bloomColor = texture2D(tBloom, vUv);

                gl_FragColor = color * bloomColor;

            }

        `
        material.needsUpdate = true

        return material;

    }


    setSize(width: number, height: number): void {
        //throw new Error("Method not implemented.");

        //this.computerParameters( parameterTest, this.viewSize.x, this.viewSize.y );

    }

    
    render(renderer: THREE.WebGLRenderer, writeBuffer: THREE.WebGLRenderTarget, readBuffer: THREE.WebGLRenderTarget, deltaTime: number, maskActive: boolean): void {
        //throw new Error("Method not implemented.");

        renderer.getSize(this.viewSize)        
       
        writeBuffer.setSize(this.viewSize.x, this.viewSize.y);
        readBuffer.setSize(this.viewSize.x, this.viewSize.y);


    
        renderer.setRenderTarget(this.bloomTexture);
        this.bloomFsQuad.render(renderer)


        
        //Others
        this.material.uniforms['uTime'].value += deltaTime
        this.material.uniforms['tInput'].value = readBuffer.texture;
        this.material.uniforms['tBloom'].value = this.bloomTexture.texture;
        

        if ( this.renderToScreen ) {

			renderer.setRenderTarget( null );
			this.fsQuad.render( renderer );

		} else {

			renderer.setRenderTarget( writeBuffer );

			if ( this.clear ) renderer.clear();

			this.fsQuad.render( renderer );

        }
        

    }

    
}
