// Class: openfl.display3D.textures.CubeTexture

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../../hxClasses_stub").default;
var $import = require("./../../../import_stub").default;
var $extend = require("./../../../extend_stub").default;
function openfl_display3D_textures_TextureBase() {return require("./../../../openfl/display3D/textures/TextureBase");}
function haxe_Timer() {return require("./../../../haxe/Timer");}
function openfl_events_Event() {return require("./../../../openfl/events/Event");}
function lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$() {return require("./../../../lime/graphics/_WebGLRenderContext/WebGLRenderContext_Impl_");}
function openfl_utils__$ByteArray_ByteArray_$Impl_$() {return require("./../../../openfl/utils/_ByteArray/ByteArray_Impl_");}
function haxe_Log() {return require("./../../../haxe/Log");}
function openfl_display3D_Context3D() {return require("./../../../openfl/display3D/Context3D");}
function js__$Boot_HaxeError() {return require("./../../../js/_Boot/HaxeError");}
function openfl_errors_IllegalOperationError() {return require("./../../../openfl/errors/IllegalOperationError");}
function openfl__$internal_formats_atf_ATFReader() {return require("./../../../openfl/_internal/formats/atf/ATFReader");}
function Std() {return require("./../../../Std");}

// Constructor

var CubeTexture = function(context,size,format,optimizeForRenderToTexture,streamingLevels) {
	(openfl_display3D_textures_TextureBase().default).call(this,context);
	this.__size = size;
	this.__width = this.__height = this.__size;
	this.__optimizeForRenderToTexture = optimizeForRenderToTexture;
	this.__streamingLevels = streamingLevels;
	this.__textureTarget = this.__context.gl.TEXTURE_CUBE_MAP;
	this.__uploadedSides = 0;
}

// Meta

CubeTexture.__name__ = ["openfl","display3D","textures","CubeTexture"];
CubeTexture.__super__ = (openfl_display3D_textures_TextureBase().default);
CubeTexture.prototype = $extend((openfl_display3D_textures_TextureBase().default).prototype, {
	uploadCompressedTextureFromByteArray: function(data,byteArrayOffset,async) {
		if(async == null) {
			async = false;
		}
		var _gthis = this;
		if(!async) {
			this.__uploadCompressedTextureFromByteArray(data,byteArrayOffset);
		} else {
			(haxe_Timer().default).delay(function() {
				_gthis.__uploadCompressedTextureFromByteArray(data,byteArrayOffset);
				_gthis.dispatchEvent(new (openfl_events_Event().default)("textureReady"));
			},1);
		}
	},
	uploadFromBitmapData: function(source,side,miplevel,generateMipmap) {
		if(generateMipmap == null) {
			generateMipmap = false;
		}
		if(miplevel == null) {
			miplevel = 0;
		}
		if(source == null) {
			return;
		}
		var size = this.__size >> miplevel;
		if(size == 0) {
			return;
		}
		var image = this.__getImage(source);
		if(image == null) {
			return;
		}
		if(miplevel == 0 && image.buffer != null && image.buffer.data == null && image.buffer.get_src() != null) {
			var gl = this.__context.gl;
			var size1 = this.__size >> miplevel;
			if(size1 == 0) {
				return;
			}
			var target = this.__sideToTarget(side);
			this.__context.__bindGLTextureCubeMap(this.__textureID);
			(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,target,miplevel,this.__internalFormat,this.__format,gl.UNSIGNED_BYTE,image.buffer.get_src());
			this.__context.__bindGLTextureCubeMap(null);
			this.__uploadedSides |= 1 << side;
			return;
		}
		this.uploadFromTypedArray(image.get_data(),side,miplevel);
	},
	uploadFromByteArray: function(data,byteArrayOffset,side,miplevel) {
		if(miplevel == null) {
			miplevel = 0;
		}
		if(byteArrayOffset == 0) {
			this.uploadFromTypedArray(data.b,side,miplevel);
			return;
		}
		var buffer = (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).toArrayBuffer(data);
		var this1;
		if(buffer != null) {
			this1 = new Uint8Array(buffer,byteArrayOffset);
		} else {
			this1 = null;
		}
		this.uploadFromTypedArray(this1,side,miplevel);
	},
	uploadFromTypedArray: function(data,side,miplevel) {
		if(miplevel == null) {
			miplevel = 0;
		}
		if(data == null) {
			return;
		}
		var gl = this.__context.gl;
		var size = this.__size >> miplevel;
		if(size == 0) {
			return;
		}
		var target = this.__sideToTarget(side);
		this.__context.__bindGLTextureCubeMap(this.__textureID);
		(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,target,miplevel,this.__internalFormat,size,size,0,this.__format,gl.UNSIGNED_BYTE,data);
		this.__context.__bindGLTextureCubeMap(null);
		this.__uploadedSides |= 1 << side;
	},
	__getGLFramebuffer: function(enableDepthAndStencil,antiAlias,surfaceSelector) {
		var gl = this.__context.gl;
		if(this.__glFramebuffer == null) {
			this.__glFramebuffer = gl.createFramebuffer();
			this.__framebufferSurface = -1;
		}
		if(this.__framebufferSurface != surfaceSelector) {
			this.__framebufferSurface = surfaceSelector;
			this.__context.__bindGLFramebuffer(this.__glFramebuffer);
			gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_CUBE_MAP_POSITIVE_X + surfaceSelector,this.__textureID,0);
			if(this.__context.__enableErrorChecking) {
				var code = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
				if(code != gl.FRAMEBUFFER_COMPLETE) {
					(haxe_Log().default).trace("Error: Context3D.setRenderToTexture status:" + code + " width:" + this.__width + " height:" + this.__height,{ fileName : "CubeTexture.hx", lineNumber : 165, className : "openfl.display3D.textures.CubeTexture", methodName : "__getGLFramebuffer"});
				}
			}
		}
		return (openfl_display3D_textures_TextureBase().default).prototype.__getGLFramebuffer.call(this,enableDepthAndStencil,antiAlias,surfaceSelector);
	},
	__setSamplerState: function(state) {
		if((openfl_display3D_textures_TextureBase().default).prototype.__setSamplerState.call(this,state)) {
			var gl = this.__context.gl;
			if(state.mipfilter != "mipnone" && !this.__samplerState.mipmapGenerated) {
				gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
				this.__samplerState.mipmapGenerated = true;
			}
			if((openfl_display3D_Context3D().default).GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT != 0) {
				var aniso;
				var _g = state.filter;
				switch(_g) {
				case "anisotropic16x":
					aniso = 16;
					break;
				case "anisotropic2x":
					aniso = 2;
					break;
				case "anisotropic4x":
					aniso = 4;
					break;
				case "anisotropic8x":
					aniso = 8;
					break;
				default:
					aniso = 1;
				}
				if(aniso > (openfl_display3D_Context3D().default).GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT) {
					aniso = (openfl_display3D_Context3D().default).GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT;
				}
				gl.texParameterf(gl.TEXTURE_CUBE_MAP,(openfl_display3D_Context3D().default).GL_TEXTURE_MAX_ANISOTROPY_EXT,aniso);
			}
			return true;
		}
		return false;
	},
	__sideToTarget: function(side) {
		var gl = this.__context.gl;
		switch(side) {
		case 0:
			return gl.TEXTURE_CUBE_MAP_POSITIVE_X;
		case 1:
			return gl.TEXTURE_CUBE_MAP_NEGATIVE_X;
		case 2:
			return gl.TEXTURE_CUBE_MAP_POSITIVE_Y;
		case 3:
			return gl.TEXTURE_CUBE_MAP_NEGATIVE_Y;
		case 4:
			return gl.TEXTURE_CUBE_MAP_POSITIVE_Z;
		case 5:
			return gl.TEXTURE_CUBE_MAP_NEGATIVE_Z;
		default:
			throw new (js__$Boot_HaxeError().default)(new (openfl_errors_IllegalOperationError().default)());
		}
	},
	__uploadCompressedTextureFromByteArray: function(data,byteArrayOffset) {
		var _gthis = this;
		var reader = new (openfl__$internal_formats_atf_ATFReader().default)(data,byteArrayOffset);
		var alpha = reader.readHeader(this.__size,this.__size,true);
		var gl = this.__context.gl;
		this.__context.__bindGLTextureCubeMap(this.__textureID);
		var hasTexture = false;
		reader.readTextures(function(side,level,gpuFormat,width,height,blockLength,bytes) {
			var format = alpha ? (openfl_display3D_textures_TextureBase().default).__compressedFormatsAlpha.get(gpuFormat) : (openfl_display3D_textures_TextureBase().default).__compressedFormats.get(gpuFormat);
			if(format == 0) {
				return;
			}
			hasTexture = true;
			var target = _gthis.__sideToTarget(side);
			_gthis.__format = format;
			_gthis.__internalFormat = format;
			if(alpha && gpuFormat == 2) {
				var size = (Std().default)["int"](blockLength / 2);
				var _gthis1 = _gthis.__internalFormat;
				var buffer = bytes.b.buffer;
				var this1;
				if(buffer != null) {
					if(size == null) {
						this1 = new Uint8Array(buffer,0);
					} else {
						this1 = new Uint8Array(buffer,0,size);
					}
				} else {
					this1 = null;
				}
				(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).compressedTexImage2D(gl,target,level,_gthis1,width,height,0,this1);
				var alphaTexture = new CubeTexture(_gthis.__context,_gthis.__size,"compressed",_gthis.__optimizeForRenderToTexture,_gthis.__streamingLevels);
				alphaTexture.__format = format;
				alphaTexture.__internalFormat = format;
				_gthis.__context.__bindGLTextureCubeMap(alphaTexture.__textureID);
				var alphaTexture1 = alphaTexture.__internalFormat;
				var buffer1 = bytes.b.buffer;
				var this2;
				if(buffer1 != null) {
					if(size == null) {
						this2 = new Uint8Array(buffer1,size);
					} else {
						this2 = new Uint8Array(buffer1,size,size);
					}
				} else {
					this2 = null;
				}
				(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).compressedTexImage2D(gl,target,level,alphaTexture1,width,height,0,this2);
				_gthis.__alphaTexture = alphaTexture;
			} else {
				var _gthis2 = _gthis.__internalFormat;
				var buffer2 = bytes.b.buffer;
				var this3;
				if(buffer2 != null) {
					if(blockLength == null) {
						this3 = new Uint8Array(buffer2,0);
					} else {
						this3 = new Uint8Array(buffer2,0,blockLength);
					}
				} else {
					this3 = null;
				}
				(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).compressedTexImage2D(gl,target,level,_gthis2,width,height,0,this3);
			}
		});
		if(!hasTexture) {
			var _g = 0;
			while(_g < 6) {
				var side1 = _g++;
				var elements = this.__size * this.__size * 4;
				var this4;
				if(elements != null) {
					this4 = new Uint8Array(elements);
				} else {
					this4 = null;
				}
				var data1 = this4;
				var tmp = this.__sideToTarget(side1);
				(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,tmp,0,this.__internalFormat,this.__size,this.__size,0,this.__format,gl.UNSIGNED_BYTE,data1);
			}
		}
		this.__context.__bindGLTextureCubeMap(null);
	}
});
CubeTexture.prototype.__class__ = CubeTexture.prototype.constructor = $hxClasses["openfl.display3D.textures.CubeTexture"] = CubeTexture;

// Init



// Statics




// Export

exports.default = CubeTexture;