import framebuffer from '../framebuffer.js';
import sampler2D from '../sampler2D.js';
import {math, vector3, matrix4} from '../math.js';
import samplerCube from '../samplerCube.js';
import shader from '../shader.js';


class ssao {
	
	constructor( ) {
		this.realtime = true;
	}
	
	setViewport( viewport ){
		
		this.viewport = viewport;
		this.gl = viewport.gl;
	}
	
	prepare() {
		
		this.framebuffer = new framebuffer();
		this.framebuffer.width = this.viewport.width;
		this.framebuffer.height = this.viewport.height;
		
		var ambientOcclusionSampler = new sampler2D();
		ambientOcclusionSampler.type = this.gl.FLOAT;
		
		this.framebuffer.setViewport( this.viewport );
		this.framebuffer.addSampler( ambientOcclusionSampler );
		
		this.framebuffer.create();
		
		var randomImage2 = new Uint8Array ( 	[149,123,253,255, 126,3,96,255,    164,246,98,255, 154,177,13,255,
											52,83,220,255,   1,142,142,255,   32,57,79,255,   48,159,32,255,
											56,232,114,255,  177,216,203,255, 69,196,217,255, 240,165,81,255,
											224,56,85,255,   232,89,189,255,  143,25,202,255, 117,73,12,255] );
		
		
		var diffuseTexture = kepler.textureFromTypedArray( randomImage2, 4, 4 );
		var randomSampler = new sampler2D();
		randomSampler.texture = diffuseTexture;
		
		this.shader = new shader();
		this.shader.createFromFile("shaders/ambientOcclusion.shader");
		this.shader.setUniform("viewProjection", this.viewport.quad.viewProjection );

		this.shader.setUniform("screenWidth", this.viewport.width );
		this.shader.setUniform("screenHeight", this.viewport.height );
		this.shader.setUniform("far", this.viewport.mainCamera.far );
		
		
		this.shader.setUniform("randomSampler",  randomSampler);

		var radius = 0.1;
		
		var scale = [ vector3.scale( new vector3(-0.556641,-0.037109,-0.654297), radius ), 
					vector3.scale( new vector3(0.173828,0.111328,0.064453), radius ), 
					vector3.scale( new vector3(0.001953,0.082031,-0.060547), radius ), 
					vector3.scale( new vector3(0.220703,-0.359375,-0.062500), radius ), 
					vector3.scale( new vector3(0.242188,0.126953,-0.250000), radius ), 
					vector3.scale( new vector3(0.070313,-0.025391,0.148438), radius ), 
					vector3.scale( new vector3(-0.078125,0.013672,-0.314453), radius ), 
					vector3.scale( new vector3(0.117188,-0.140625,-0.199219), radius ), 
					vector3.scale( new vector3(-0.251953,-0.558594,0.082031), radius ), 
					vector3.scale( new vector3(0.308594,0.193359,0.324219), radius ), 
					vector3.scale( new vector3(0.173828,-0.140625,0.031250), radius ), 
					vector3.scale( new vector3(0.179688,-0.044922,0.046875), radius ), 
					vector3.scale( new vector3(-0.146484,-0.201172,-0.029297), radius ), 
					vector3.scale( new vector3(-0.300781,0.234375,0.539063), radius ), 
					vector3.scale( new vector3(0.228516,0.154297,-0.119141), radius ), 
					vector3.scale( new vector3(-0.119141,-0.003906,-0.066406), radius ), 
					vector3.scale( new vector3(-0.218750,0.214844,-0.250000), radius ), 
					vector3.scale( new vector3(0.113281,-0.091797,0.212891), radius ), 
					vector3.scale( new vector3(0.105469,-0.039063,-0.019531), radius ), 
					vector3.scale( new vector3(-0.705078,-0.060547,0.023438), radius ), 
					vector3.scale( new vector3(0.021484,0.326172,0.115234), radius ), 
					vector3.scale( new vector3(0.353516,0.208984,-0.294922), radius ), 
					vector3.scale( new vector3(-0.029297,-0.259766,0.089844), radius ), 
					vector3.scale( new vector3(-0.240234,0.146484,-0.068359), radius ), 
					vector3.scale( new vector3(-0.296875,0.410156,-0.291016), radius ), 
					vector3.scale( new vector3(0.078125,0.113281,-0.126953), radius ), 
					vector3.scale( new vector3(-0.152344,-0.019531,0.142578), radius ), 
					vector3.scale( new vector3(-0.214844,-0.175781,0.191406), radius ), 
					vector3.scale( new vector3(0.134766,0.414063,-0.707031), radius ), 
					vector3.scale( new vector3(0.291016,-0.833984,-0.183594), radius ), 
					vector3.scale( new vector3(-0.058594,-0.111328,0.457031), radius ), 
					vector3.scale( new vector3(-0.115234,-0.287109,-0.259766), radius ) ];
					
		var kernelRad = [[1.163003/radius,4.624262/radius,9.806342/radius,2.345541/radius], 
						[2.699039/radius,6.016871/radius,3.083554/radius,3.696197/radius], 
						[1.617461/radius,2.050939/radius,4.429457/radius,5.234036/radius], 
						[3.990876/radius,1.514475/radius,3.329241/radius,7.328508/radius], 
						[2.527725/radius,3.875453/radius,8.760140/radius,1.412308/radius], 
						[2.885205/radius,1.977866/radius,3.617674/radius,3.453552/radius], 
						[1.712336/radius,5.341163/radius,4.771728/radius,2.965737/radius], 
						[1.204293/radius,1.108428/radius,2.109570/radius,2.475453/radius] ]; 
		

		//this.shader.setUniform("scale", scale );
		//this.shader.setUniform("kernelRad", kernelRad );
		
		//this.shader.setUniform("kernel", scale );
		//this.shader.setUniform("kernelRad", kernelRad );
		
		this.shader.setUniform("far", this.viewport.mainCamera.far );
		console.log("this.viewport.mainCamera.far", this.viewport.mainCamera.far);
		//this.shader.setUniform("positionSampler", this.infoSampler );
		//this.shader.setUniform("positionSampler", this.infoSampler );
		
		
		
		// ssaoConvolutionFinalSampler );
	
		var  ssaoKernel = [];
		for (var i = 0; i < 64; ++i)
		{
		   var sample = [
				Math.random() * 2.0 - 1.0, 
				Math.random() * 2.0 - 1.0, 
				Math.random()
			];
			
			sample = vector3.normalize(sample);
			sample *= Math.random();
			
			scale = i / 64.0; 
			
			ssaoKernel.push(sample);  
		}
		
		this.shader.setUniform("samples", ssaoKernel );
		
		
		this.shader.setUniform("parameters", [0.6, 0.075, 1.0, 1.0] );
	}
	
	
	render() {
		
		if(this.renderToViewport)
			this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null );
		else
			this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.framebuffer.glFramebuffer );

		this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
		this.gl.clearColor( 0, 0, 0, 1 );
		this.gl.viewport(0, 0, this.viewport.width, this.viewport.height);
		
		this.viewport.quad.draw( this.shader, null );
		
	}
}

export {ssao as default};