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';
import texture from '../texture.js';


class fxaa {
	
	constructor(  ) {
	this.realtime = true;
	}
	
	setViewport( viewport ){
		
		this.viewport = viewport;
		this.gl = viewport.gl;
	}
	
	prepare() {
		
		console.log("prepare uber");
		

		this.width = this.viewport.width;
		this.height = this.viewport.height;
		
		this.targetSampler = new sampler2D();
		this.targetSampler.type = this.gl.FLOAT;
		this.targetSampler.filter = this.gl.LINEAR;
		
		
		this.framebuffer = new framebuffer();
		
		
		this.framebuffer.width = this.width;
		this.framebuffer.height = this.height;
		
		this.framebuffer.multiSample = true;
		this.framebuffer.setViewport( this.viewport );
		this.framebuffer.addSampler( this.targetSampler );
		this.framebuffer.create();
		
		
		this.shader = new shader();
		this.shader.createFromFile("shaders/fxaa4.shader");
		
		this.shader.setUniform("viewProjection", this.viewport.quad.viewProjection );
		
		
		this.shader.setUniform("res", [this.width, this.height] );
		this.shader.setUniform("screenSize", [this.width, this.height] );
		this.shader.setUniform("inverseScreenSize", [1 /this.width, 1 /this.height] );
		
		
		this.shader.setUniform("u_lumaThreshold", 0.5 );
		this.shader.setUniform("u_mulReduce", 8 );
		this.shader.setUniform("u_minReduce", 128 );
		this.shader.setUniform("u_maxSpan", 8 );
		
		
		
		this.shader.setUniform("u_showEdges", 0.0 );
		this.shader.setUniform("u_fxaaOn", 1.0 );
		
  this.msFB = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, this.msFB );
  this.msRB = gl.createRenderbuffer();
  gl.bindRenderbuffer(gl.RENDERBUFFER, this.msRB);
  const samples = 4;
  const internalFormat = gl.RGBA32F;
  const width = this.viewport.width;
  const height = this.viewport.height;
  gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, internalFormat, width, height);
  gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, this.msRB);
	 
	 
  //checkFramebuffer(gl);
  
  //gl.clearColor(1,0,0,1);
  //gl.clear(gl.COLOR_BUFFER_BIT);
  /*
 this.texFB = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, this.texFB);
  const tex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, tex);
  const levels = 1;
  gl.texStorage2D(gl.TEXTURE_2D, levels, internalFormat, width, height);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
		*/
		
	
		

		
	}
	
	
	render() {
		

		//this.gl.bindFramebuffer(this.gl.READ_FRAMEBUFFER, this.fxaaFramebuffer.msaaFramebuffer );
		//this.gl.bindFramebuffer(this.gl.DRAW_FRAMEBUFFER, this.fxaaFramebuffer.glFramebuffer  );
		//this.gl.blitFramebuffer(0, 0, this.width, this.height,
		//				   0, 0, this.width, this.height,
		//				   this.gl.COLOR_BUFFER_BIT, this.gl.LINEAR);
		
		
		/*
		var texture = gl.createTexture();
		gl.bindTexture(gl.TEXTURE_2D, texture);
		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
		gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
		gl.bindTexture(gl.TEXTURE_2D, null);

		gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer.glFramebuffer);
		gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
		gl.bindFramebuffer(gl.FRAMEBUFFER, null);
		*/
		//this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null);
		
		//	this.msaaFramebuffer		//Framebuffer
		//	this.msaaColorRenderBuffer  //Renderbuffer
		//	this.msaaDepthRenderBuffer  //Renderbuffer
		
		//console.log(this.framebuffer.glFramebuffer);

		//this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.framebuffer.glFramebuffer);
		//this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, this.framebuffer.msaaColorRenderBuffer);
		//this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.framebuffer.msaaFramebuffer);
		//if(this.renderToViewport)
		//	this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null );
		//else
		//this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.msFB );//msaaFramebuffer
		this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null );
		//this.gl.bindRenderbuffer(this.gl.FRAMEBUFFER, this.msRB );
		
		//this.shader.setUniform("cameraPosition", camera.eye );
		
		
		this.shader.update(this.viewport);
		
		this.gl.viewport(0, 0, this.viewport.width, this.viewport.height);
		this.gl.clearColor( 0, 0, 0, 1 );
		this.gl.clear( this.gl.COLOR_BUFFER_BIT );//this.gl.clear( this.gl.DEPTH_BUFFER_BIT );
		
		this.gl.disable(this.gl.DEPTH_TEST);

		this.viewport.quad.draw( this.shader, null );
		/*
		gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this.msFB);
		gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.framebuffer.glFramebuffer);
		gl.clearBufferfv(gl.COLOR, 0, [0.0, 0.0, 0.0, 1.0]);
		gl.blitFramebuffer(
		0, 0,  this.width,  this.height,
		0, 0,  this.width,  this.height,
		gl.COLOR_BUFFER_BIT, gl.LINEAR);
		
		*/
		/*
		this.gl.bindFramebuffer(this.gl.READ_FRAMEBUFFER, this.framebuffer.msaaFramebuffer);
		//this.gl.bindFramebuffer(this.gl.DRAW_FRAMEBUFFER, this.framebuffer.glFramebuffer);
		this.gl.clearBufferfv(this.gl.COLOR, 0, [0.0, 0.0, 0.0, 1.0]);
		this.gl.blitFramebuffer(
			0, 0, this.width, this.height,
			0, 0, this.width, this.height,
			this.gl.COLOR_BUFFER_BIT, this.gl.NEAREST
		);
		

		
	
		this.gl.bindFramebuffer(this.gl.READ_FRAMEBUFFER, this.msaaFramebuffer );
		this.gl.bindFramebuffer(this.gl.DRAW_FRAMEBUFFER, this.glFramebuffer  );
		this.gl.blitFramebuffer(0, 0, this.width, this.height,
								0, 0, this.width, this.height,
									this.gl.COLOR_BUFFER_BIT, this.gl.LINEAR);//this.gl.LINEAR
		*/
	}
}

export {fxaa as default};