// Class: openfl.media.Video

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_display_IShaderDrawable() {return require("./../../openfl/display/IShaderDrawable");}
function openfl_display_DisplayObject() {return require("./../../openfl/display/DisplayObject");}
function openfl_geom_Rectangle() {return require("./../../openfl/geom/Rectangle");}
function lime_utils__$DataPointer_DataPointer_$Impl_$() {return require("./../../lime/utils/_DataPointer/DataPointer_Impl_");}
function openfl_geom_Point() {return require("./../../openfl/geom/Point");}
function openfl__$internal_renderer_canvas_CanvasVideo() {return require("./../../openfl/_internal/renderer/canvas/CanvasVideo");}
function openfl__$internal_renderer_dom_DOMVideo() {return require("./../../openfl/_internal/renderer/dom/DOMVideo");}
function openfl__$internal_renderer_opengl_GLVideo() {return require("./../../openfl/_internal/renderer/opengl/GLVideo");}
function Std() {return require("./../../Std");}

// Constructor

var Video = function(width,height) {
	if(height == null) {
		height = 240;
	}
	if(width == null) {
		width = 320;
	}
	(openfl_display_DisplayObject().default).call(this);
	this.__width = width;
	this.__height = height;
	this.smoothing = false;
	this.deblocking = 0;
}

// Meta

Video.__name__ = ["openfl","media","Video"];
Video.__interfaces__ = [(openfl_display_IShaderDrawable().default)];
Video.__super__ = (openfl_display_DisplayObject().default);
Video.prototype = $extend((openfl_display_DisplayObject().default).prototype, {
	attachNetStream: function(netStream) {
		this.__stream = netStream;
		if(this.__stream != null) {
			this.__stream.__video.play();
		}
	},
	clear: function() {
	},
	__enterFrame: function(deltaTime) {
		if(this.__renderable && this.__stream != null) {
			this.__setRenderDirty();
		}
	},
	__getBounds: function(rect,matrix) {
		var bounds = (openfl_geom_Rectangle().default).__pool.get();
		bounds.setTo(0,0,this.__width,this.__height);
		bounds.__transform(bounds,matrix);
		rect.__expand(bounds.x,bounds.y,bounds.width,bounds.height);
		(openfl_geom_Rectangle().default).__pool.release(bounds);
	},
	__getBuffer: function(gl,alpha,colorTransform) {
		if(this.__buffer == null || this.__bufferContext != gl) {
			var uvWidth = 1;
			var uvHeight = 1;
			var this1 = new Float32Array(104);
			this.__bufferData = this1;
			this.__bufferData[0] = this.get_width();
			this.__bufferData[1] = this.get_height();
			this.__bufferData[3] = uvWidth;
			this.__bufferData[4] = uvHeight;
			this.__bufferData[27] = this.get_height();
			this.__bufferData[30] = uvHeight;
			this.__bufferData[52] = this.get_width();
			this.__bufferData[55] = uvWidth;
			var _g = 0;
			while(_g < 4) {
				var i = _g++;
				this.__bufferData[26 * i + 5] = alpha;
				if(colorTransform != null) {
					this.__bufferData[26 * i + 6] = colorTransform.redMultiplier;
					this.__bufferData[26 * i + 11] = colorTransform.greenMultiplier;
					this.__bufferData[26 * i + 16] = colorTransform.blueMultiplier;
					this.__bufferData[26 * i + 21] = colorTransform.alphaMultiplier;
					this.__bufferData[26 * i + 22] = colorTransform.redOffset / 255;
					this.__bufferData[26 * i + 23] = colorTransform.greenOffset / 255;
					this.__bufferData[26 * i + 24] = colorTransform.blueOffset / 255;
					this.__bufferData[26 * i + 25] = colorTransform.alphaOffset / 255;
				} else {
					this.__bufferData[26 * i + 6] = 1;
					this.__bufferData[26 * i + 11] = 1;
					this.__bufferData[26 * i + 16] = 1;
					this.__bufferData[26 * i + 21] = 1;
				}
			}
			this.__bufferAlpha = alpha;
			this.__bufferColorTransform = colorTransform != null ? colorTransform.__clone() : null;
			this.__bufferContext = gl;
			this.__buffer = gl.createBuffer();
			gl.bindBuffer(gl.ARRAY_BUFFER,this.__buffer);
			gl.bufferData(gl.ARRAY_BUFFER,this.__bufferData.byteLength,(lime_utils__$DataPointer_DataPointer_$Impl_$().default).fromArrayBufferView(this.__bufferData),gl.STATIC_DRAW);
		} else {
			if(this.__bufferAlpha != alpha) {
				var _g1 = 0;
				while(_g1 < 4) {
					var i1 = _g1++;
					this.__bufferData[26 * i1 + 5] = alpha;
				}
			}
			if(this.__bufferColorTransform == null && colorTransform != null || this.__bufferColorTransform != null && !this.__bufferColorTransform.__equals(colorTransform)) {
				if(colorTransform != null) {
					this.__bufferColorTransform = colorTransform.__clone();
					var _g2 = 0;
					while(_g2 < 4) {
						var i2 = _g2++;
						this.__bufferData[26 * i2 + 6] = colorTransform.redMultiplier;
						this.__bufferData[26 * i2 + 11] = colorTransform.greenMultiplier;
						this.__bufferData[26 * i2 + 16] = colorTransform.blueMultiplier;
						this.__bufferData[26 * i2 + 21] = colorTransform.alphaMultiplier;
						this.__bufferData[26 * i2 + 22] = colorTransform.redOffset / 255;
						this.__bufferData[26 * i2 + 23] = colorTransform.greenOffset / 255;
						this.__bufferData[26 * i2 + 24] = colorTransform.blueOffset / 255;
						this.__bufferData[26 * i2 + 25] = colorTransform.alphaOffset / 255;
					}
				} else {
					var _g3 = 0;
					while(_g3 < 4) {
						var i3 = _g3++;
						this.__bufferData[26 * i3 + 6] = 1;
						this.__bufferData[26 * i3 + 11] = 1;
						this.__bufferData[26 * i3 + 16] = 1;
						this.__bufferData[26 * i3 + 21] = 1;
						this.__bufferData[26 * i3 + 22] = 0;
						this.__bufferData[26 * i3 + 23] = 0;
						this.__bufferData[26 * i3 + 24] = 0;
						this.__bufferData[26 * i3 + 25] = 0;
					}
				}
			}
			gl.bindBuffer(gl.ARRAY_BUFFER,this.__buffer);
			gl.bufferData(gl.ARRAY_BUFFER,this.__bufferData.byteLength,(lime_utils__$DataPointer_DataPointer_$Impl_$().default).fromArrayBufferView(this.__bufferData),gl.STATIC_DRAW);
		}
		return this.__buffer;
	},
	__getTexture: function(gl) {
		if(this.__stream == null) {
			return null;
		}
		if(this.__texture == null) {
			this.__texture = gl.createTexture();
			gl.bindTexture(gl.TEXTURE_2D,this.__texture);
			gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);
			gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);
			gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);
			gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);
			this.__textureTime = -1;
		}
		if(this.__stream.__video.currentTime != this.__textureTime) {
			var internalFormat = gl.RGBA;
			var format = gl.RGBA;
			gl.bindTexture(gl.TEXTURE_2D,this.__texture);
			gl.texImage2DWEBGL(gl.TEXTURE_2D,0,internalFormat,format,gl.UNSIGNED_BYTE,this.__stream.__video);
			this.__textureTime = this.__stream.__video.currentTime;
		}
		return this.__texture;
	},
	__hitTest: function(x,y,shapeFlag,stack,interactiveOnly,hitObject) {
		if(!hitObject.get_visible() || this.__isMask) {
			return false;
		}
		if(this.get_mask() != null && !this.get_mask().__hitTestMask(x,y)) {
			return false;
		}
		this.__getRenderTransform();
		var px = this.__renderTransform.__transformInverseX(x,y);
		var py = this.__renderTransform.__transformInverseY(x,y);
		if(px > 0 && py > 0 && px <= this.__width && py <= this.__height) {
			if(stack != null && !interactiveOnly) {
				stack.push(hitObject);
			}
			return true;
		}
		return false;
	},
	__hitTestMask: function(x,y) {
		var point = (openfl_geom_Point().default).__pool.get();
		point.setTo(x,y);
		this.__globalToLocal(point,point);
		var hit = point.x > 0 && point.y > 0 && point.x <= this.__width && point.y <= this.__height;
		(openfl_geom_Point().default).__pool.release(point);
		return hit;
	},
	__renderCanvas: function(renderSession) {
		(openfl__$internal_renderer_canvas_CanvasVideo().default).render(this,renderSession);
	},
	__renderDOM: function(renderSession) {
		(openfl__$internal_renderer_dom_DOMVideo().default).render(this,renderSession);
	},
	__renderGL: function(renderSession) {
		(openfl__$internal_renderer_opengl_GLVideo().default).render(this,renderSession);
	},
	__renderGLMask: function(renderSession) {
		(openfl__$internal_renderer_opengl_GLVideo().default).renderMask(this,renderSession);
	},
	get_height: function() {
		return this.__height * this.get_scaleY();
	},
	set_height: function(value) {
		if(this.get_scaleY() != 1 || value != this.__height) {
			this.__setTransformDirty();
			this.__dirty = true;
		}
		this.set_scaleY(1);
		return this.__height = value;
	},
	get_videoHeight: function() {
		if(this.__stream != null) {
			return (Std().default)["int"](this.__stream.__video.videoHeight);
		}
		return 0;
	},
	get_videoWidth: function() {
		if(this.__stream != null) {
			return (Std().default)["int"](this.__stream.__video.videoWidth);
		}
		return 0;
	},
	get_width: function() {
		return this.__width * this.__scaleX;
	},
	set_width: function(value) {
		if(this.__scaleX != 1 || this.__width != value) {
			this.__setTransformDirty();
			this.__dirty = true;
		}
		this.set_scaleX(1);
		return this.__width = value;
	}
});
Video.prototype.__class__ = $hxClasses["openfl.media.Video"] = Video;

// Init

Object.defineProperties(Video.prototype,{ "videoHeight" : { get : function () { return this.get_videoHeight (); }}, "videoWidth" : { get : function () { return this.get_videoWidth (); }}});

// Statics


Video.__bufferStride = 26

// Export

exports.default = Video;