// 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_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.__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) {
		if(this.__buffer == null || this.__bufferContext != gl) {
			var uvWidth = 1;
			var uvHeight = 1;
			var this1 = new Float32Array(20);
			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[6] = this.get_height();
			this.__bufferData[9] = uvHeight;
			this.__bufferData[10] = this.get_width();
			this.__bufferData[13] = uvWidth;
			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 {
			gl.bindBuffer(gl.ARRAY_BUFFER,this.__buffer);
		}
		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(renderer) {
		(openfl__$internal_renderer_canvas_CanvasVideo().default).render(this,renderer);
		this.__renderEvent(renderer);
	},
	__renderDOM: function(renderer) {
		(openfl__$internal_renderer_dom_DOMVideo().default).render(this,renderer);
		this.__renderEvent(renderer);
	},
	__renderGL: function(renderer) {
		(openfl__$internal_renderer_opengl_GLVideo().default).render(this,renderer);
		this.__renderEvent(renderer);
	},
	__renderGLMask: function(renderer) {
		(openfl__$internal_renderer_opengl_GLVideo().default).renderMask(this,renderer);
	},
	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 = 5

// Export

exports.default = Video;