/*
 * Copyright 2013, kaj dijkstra , 
 * Author, Kaj Dijkstra.
 * All rights reserved.
 *
 */
 var cubeSamplerID = 0;
import {math} from './math.js';

/**
 * light
**/
class samplerCube{

	constructor() {

		this.textures = [];
		this.cubeTexture = gl.createTexture();
		this.id = ++kepler.samplerId;//+engine.samplerCubeID
		
		this.FLIP_Y = true;
		
		this.MIN_FILTER = gl.LINEAR;
		this.MAG_FILTER = gl.LINEAR;
		
		this.WRAP_S = gl.REPEAT;
		this.WRAP_T = gl.REPEAT;
		
		this.datatype = gl.RGB;
		this.format = gl.RGB;
		this.internalFormat = gl.RGB;
		
		this.target = gl.TEXTURE_CUBE_MAP;
		
		
		this.type = gl.UNSIGNED_BYTE;

		this.alpha = 1.0;
		
		this.binded = false;
		this.anisotropic = false;
	}

	setViewport( viewport ){
		
		this.viewport = viewport;
		this.gl = viewport.gl;
	}

	setTarget(target) {
		
		 var faces = [	gl.TEXTURE_CUBE_MAP_POSITIVE_X,
						gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
						gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
						gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
						gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
						gl.TEXTURE_CUBE_MAP_NEGATIVE_Z ];
						
		for(var c =0; c<faces.length; c++) {
			
			var texture = new texture();
			
			texture.face = faces[c];
			
			this.addTexture( texture );
			
		}
	}

	getTexture( face ) {

		if( face ) {
			
			for(var c = 0; c < this.textures.length; c++) {
				
				if( face == this.textures[c].face ) {
					
					return this.textures[c];
					
				}
				
			}
			
		} else {
			
			return this.textures[0];
			
		}
		
	}

	getTextures( ) {
		return this.textures;
	}

	addTexture(texture, face) {

		
		this.textures.push({texture: texture, face : face});
	}

	/**
	 * bind sampler to shader
	 * @param {(shader)} shader
	**/
	bind( shader ) {
		//this.cubeTexture
		
		var glTexture = this.cubeTexture;
		
		this.gl.enable ( this.gl.BLEND ) ;

		
		this.gl.bindTexture(this.gl.TEXTURE_CUBE_MAP,  glTexture );
		this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, this.FLIP_Y);

		if(!this.binded) 
			this.id = shader.samplerId++;
		
		//this.gl.activeTexture(this.gl.TEXTURE_2D + this.id);
			
			
		for(var c = 0; c<this.textures.length; c++) {
			

			var texture = this.textures[c].texture;

			
			if (texture.dataType  == "framebuffer" ) {
			
				//texture.glTexture =  texture.data;
				var type = texture.type;
				
			} else {
				
				var type = texture.dataType;
			}	
				
				var data = texture.data;
				var face = this.textures[c].face;
				
				//this.gl.activeTexture(this.gl.TEXTURE_2D + this.id + c);
				
				this.type = type;
				
				console.log("binding", texture, data );
				
				var mips = [];
				var width = texture.width;
				var height = texture.height;
					
					//serialize texture data type
					switch( type ) {
						case "float":
							this.gl.texImage2D(face, 0, this.format, width, height, 0, this.internalFormat, this.gl.FLOAT, data);
						break;
						case "int":
							this.gl.texImage2D(face, 0, this.format, width, height, 0, this.internalFormat, this.gl.UNSIGNED_BYTE, data);
						break;
						case "depth":
							this.gl.texImage2D(face, 0, this.gl.DEPTH_COMPONENT, width, height, 0, this.gl.DEPTH_COMPONENT, this.gl.UNSIGNED_SHORT, null);
						break;
						case "image":
							this.gl.texImage2D(face, 0, this.format, this.internalFormat, this.gl.UNSIGNED_BYTE, data);
						break;
						case "canvas":
							this.gl.texImage2D(face, 0, this.format, width, height, 0, this.internalFormat, this.UNSIGNED_BYTE, data);
						break;
						case "COMPRESSED_RGBA":

							//var textureCompression = kepler.extensions.textureCompression;
							var mipmaps = texture.mipmaps;
							
							var width = mipmaps[0].width;
							var height = mipmaps[0].height;
							
							for(var i = 0; i < mipmaps.length; i++) {
								var mipmap = mipmaps[i];
								
								this.gl.compressedTexImage2D(texture.face, i, mipmap.internalFormat, mipmap.width, mipmap.height, 0, mipmap.byteArray);
							}
						
						break;
					}
					
					if (math.isPowerOfTwo(width) && math.isPowerOfTwo(height) ) {
					
					//	this.gl.texParameteri(this.target, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
					//	this.gl.texParameteri(this.target, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR_MIPMAP_LINEAR);// mipmaps > 1 ? this.gl.LINEAR_MIPMAP_LINEAR : this.gl.LINEAR 
					//	this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_S, this.WRAP_S);
					//	this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_T, this.WRAP_T);
					this.gl.texParameteri(this.gl.TEXTURE_CUBE_MAP, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
					this.gl.texParameteri(this.gl.TEXTURE_CUBE_MAP, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);
					this.gl.texParameteri(this.gl.TEXTURE_CUBE_MAP, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);
					this.gl.texParameteri(this.gl.TEXTURE_CUBE_MAP, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);
					
					
						//this.gl.generateMipmap(this.gl.TEXTURE_2D);
						
					} else {

						this.gl.texParameteri(this.target, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);
						this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);
						this.gl.texParameteri(this.target, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);
						
					}
					
					
				
			}

			

			
			this.gl.bindTexture(this.target, null);
			
			this.binded = true;
			
		
	}
}

export {samplerCube as default};
