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 uber {
	
	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.setViewport( this.viewport );
		this.framebuffer.addSampler( this.targetSampler );
		this.framebuffer.create();
		
		
		
		var shadowNoiseTexure = kepler.resources.getTexture("rotrandom.png");
		var shadowNoiseSampler = new sampler2D();
		
		shadowNoiseSampler.addTexture(shadowNoiseTexure);
		
		var sphericalHarmonics = [];
		
		for(var c = 0; c<8; c++) {
			
			sphericalHarmonics.push([Math.random,  Math.random,  Math.random]);

		}
		
		
		
		//this.reflectionSampler = new samplerCube();
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("positive_x"), this.gl.TEXTURE_CUBE_MAP_POSITIVE_X);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("negative_x"), this.gl.TEXTURE_CUBE_MAP_NEGATIVE_X);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("negative_y"), this.gl.TEXTURE_CUBE_MAP_POSITIVE_Y);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("positive_y"), this.gl.TEXTURE_CUBE_MAP_NEGATIVE_Y);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("positive_z"), this.gl.TEXTURE_CUBE_MAP_POSITIVE_Z);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("negative_z"), this.gl.TEXTURE_CUBE_MAP_NEGATIVE_Z);

		//this.reflectionSampler.addTexture(kepler.resources.getTexture("cubemaps-1024/positive_x.png"), gl.TEXTURE_CUBE_MAP_POSITIVE_X);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("cubemaps-1024/negative_x.png"), gl.TEXTURE_CUBE_MAP_NEGATIVE_X);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("cubemaps-1024/negative_y.png"), gl.TEXTURE_CUBE_MAP_POSITIVE_Y);
		///this.reflectionSampler.addTexture(kepler.resources.getTexture("cubemaps-1024/positive_y.png"), gl.TEXTURE_CUBE_MAP_NEGATIVE_Y);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("cubemaps-1024/positive_z.png"), gl.TEXTURE_CUBE_MAP_POSITIVE_Z);
		//this.reflectionSampler.addTexture(kepler.resources.getTexture("cubemaps-1024/negative_z.png"), gl.TEXTURE_CUBE_MAP_NEGATIVE_Z);
		this.reflectionSampler = this.viewport.system.reflectionSampler;
		

		//deferredSampler.internalFormat = this.gl.RGBA8;
		//deferredSampler.filter = this.gl.UNSIGNED_BYTE
		

		this.shader = new shader();
		this.shader.createFromFile( "shaders/deferred.shader");
		this.shader.setUniform("viewProjection", this.viewport.quad.viewProjection );
		this.shader.setUniform("reflectionSampler", this.reflectionSampler );

		//this.shader.setUniform("materialSampler", materialSampler );
		//this.shader.setUniform("shadowDepthSampler", this.viewport.shadowCameras[0].sampler );
		this.shader.setUniform("shadowNoiseSampler", shadowNoiseSampler );
		this.shader.setUniform("shadowBias", .6  );//0.03
		
		var depthPasses = this.viewport.system.depthPasses;
		var depthpass = depthPasses[0];
		
		this.shader.setUniform("lightPosition", depthpass.lightPosition  );
		this.shader.setUniform("lightType", depthpass.lightType == "pointLight" ? 0 : 1  );
		
		this.shader.setUniform("SpotAngles", [30, 0.1]);
		this.shader.setUniform("LightColor", [1, 1, 1] );
		this.shader.setUniform("far", this.viewport.mainCamera.far );
		this.shader.setUniform("shadowFar", this.viewport.mainCamera.far );
		this.shader.setUniform("anisotropy", 0.1 );

		this.shader.setUniform("lightGeometry", [1.0 / 145.0, 4.0, 0.1] );
		this.shader.setUniform("clearCoatColor", [1.0, 1.0, 1.0] );
		
		this.shader.setUniform("lightColor", [1,1,1] ); //colorTemperatureToSRGB(6000.0) );
		this.shader.setUniform("lightDirection", [0.0, -0.7, 0.5]  );
		
		this.shader.setUniform("luma_z", 1  );
		//this.shader.setUniform("lightPosition", [0, 12, 12] );

		
		this.shader.setUniform("lightIntensity", .81);
		this.shader.setUniform("environmentLuminance", 0.6);
		//this.shader.setUniform("lightIntensity", 1.3);
		//this.shader.setUniform("environmentLuminance", 0.35);
		this.shader.setUniform("roughness", 1. );
		
		this.shader.setUniform("clearCoatRoughness", .2 );
		this.shader.setUniform("clearCoat", 0.3 );
		this.shader.setUniform("clearCoatThickness", 0.2 );
		this.shader.setUniform("metallic", .0 );
		this.shader.setUniform("reflectance", 0.1);
		
		
		this.shader.setUniform("reflectance", 0.0);
		
		this.shader.setUniform("sphericalHarmonics", sphericalHarmonics );
		this.shader.setUniform("attenuation", 3 );
		
	}
	
	toViewport( yeas ) {
		
		this.renderToViewport = yeas;
		
		this.shader.setUniform("luma_z", this.renderToViewport ? 0 : 1  );
		
	}
	colorTemperatureToSRGB(T) {
		// Compute CCT in CIE 1960 space
		var u = (0.860117757 + 1.54118254e-4 * T + 1.28641212e-7 * T * T) /
				(1 + 8.42420235e-4 * T + 7.08145163e-7 * T * T);
		var v = (0.317398726 + 4.22806245e-5 * T + 4.20481691e-8 * T * T) /
				(1 - 2.89741816e-5 * T + 1.61456053e-7 * T * T);
		// Convert to xyY
		var x = (3.0 * u) / (2.0 * u - 8.0 * v + 4.0);
		var y = (2.0 * v) / (2.0 * u - 8.0 * v + 4.0);
		// Convert to XYZ
		var XYZ = [x / y, 1.0, (1.0 - x - y) / y];
		// Convert to linear sRGB
		var rgb = [
			XYZ[0] *  3.2406 + XYZ[1] * -1.5372 + XYZ[2] * -0.4986,
			XYZ[0] * -0.9689 + XYZ[1] *  1.8758 + XYZ[2] *  0.0415,
			XYZ[0] *  0.0557 + XYZ[1] * -0.2040 + XYZ[2] *  1.0570
		];
		var max = Math.max(rgb[0], Math.max(rgb[1], rgb[2]));
		rgb[0] /= max;
		rgb[1] /= max;
		rgb[2] /= max;
		return rgb;
	}


	
	render() {
		//console.log("render uber");
		var camera = this.viewport.mainCamera;
		var scene = this.viewport.scene;
		var entitys = scene.getEntitys();
		var deferredShader = this.shader;
		
		if(this.renderToViewport)
			this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null );
		else
			this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.framebuffer.glFramebuffer );//msaaFramebuffer

		
		deferredShader.setUniform("cameraPosition", camera.eye );
		
		
		deferredShader.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.DEPTH_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT );//this.gl.clear( this.gl.DEPTH_BUFFER_BIT );
		
		this.gl.enable( this.gl.DEPTH_TEST );
		
		this.gl.enable( this.gl.CULL_FACE );
		
	
		//this.gl.blendFunc(this.gl.SRC_ALPHA, this.gl.ONE);
		//this.gl.blendFunc(this.gl.ONE, this.gl.ONE_MINUS_SRC_ALPHA);
		//nnee
		//this.gl.blendFunc(this.gl.ONE, this.gl.ONE_MINUS_SRC_ALPHA);//nnee
		//this.gl.blendFunc(this.gl.SRC_ALPHA, this.gl.ONE_MINUS_SRC_ALPHA);
		//var lights = 0;
		
		//this.gl.disable(this.gl.DEPTH_TEST);
		//this.gl.enable(this.gl.BLEND);
		//this.gl.enable(this.gl.DEPTH_TEST);
		//this.gl.enable(this.gl.BLEND);
		//this.gl.blendEquation(this.gl.FUNC_ADD);
		//this.gl.blendFunc(this.gl.SRC_ALPHA, this.gl.ONE_MINUS_SRC_ALPHA);
		//deferredShader.setUniform("lightViewProjection", this.viewport.shadowCameras[0].viewProjection );
		
		
		//this.shader.setUniform("lightPosition", [1,1999, 1000], true  );
		
		var depthPasses = this.viewport.system.depthPasses;
		this.viewport.quad.draw( deferredShader, null );
		
		//for(var e = 0;e<depthPasses.length;e++) {
			
			//this.gl.clear( this.gl.DEPTH_BUFFER_BIT);
			//var depthPass = depthPasses[e];
			
			//this.shader.setUniform("lightPosition", depthPass.lightPosition, true  );
			
			

			
		//}
		
	}
}

export {uber as default};