// Class: openfl.display.Stage3D

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 $bind = require("./../../bind_stub").default;
var $extend = require("./../../extend_stub").default;
function openfl_events_EventDispatcher() {return require("./../../openfl/events/EventDispatcher");}
function haxe_Timer() {return require("./../../haxe/Timer");}
function lime_graphics_RendererType() {return require("./../../lime/graphics/RendererType");}
function openfl_display3D_Context3D() {return require("./../../openfl/display3D/Context3D");}
function js_Browser() {return require("./../../js/Browser");}
function Reflect() {return require("./../../Reflect");}
function js_html__$CanvasElement_CanvasUtil() {return require("./../../js/html/_CanvasElement/CanvasUtil");}
function lime__$backend_html5_HTML5GLRenderContext() {return require("./../../lime/_backend/html5/HTML5GLRenderContext");}
function lime_graphics_opengl_GL() {return require("./../../lime/graphics/opengl/GL");}
function openfl_events_ErrorEvent() {return require("./../../openfl/events/ErrorEvent");}
function openfl_events_Event() {return require("./../../openfl/events/Event");}
function openfl__$internal_stage3D_opengl_GLStage3D() {return require("./../../openfl/_internal/stage3D/opengl/GLStage3D");}

// Constructor

var Stage3D = function() {
	(openfl_events_EventDispatcher().default).call(this);
	this.__x = 0;
	this.__y = 0;
	this.visible = true;
}

// Meta

Stage3D.__name__ = ["openfl","display","Stage3D"];
Stage3D.__super__ = (openfl_events_EventDispatcher().default);
Stage3D.prototype = $extend((openfl_events_EventDispatcher().default).prototype, {
	requestContext3D: function(context3DRenderMode,profile) {
		if(profile == null) {
			profile = "baseline";
		}
		if(context3DRenderMode == null) {
			context3DRenderMode = "auto";
		}
		this.__contextRequested = true;
		if(this.context3D != null) {
			(haxe_Timer().default).delay($bind(this,this.__dispatchCreate),1);
		}
	},
	requestContext3DMatchingProfiles: function(profiles) {
		this.requestContext3D();
	},
	__createContext: function(stage,renderer) {
		this.__stage = stage;
		if(renderer.__type == (lime_graphics_RendererType().default).OPENGL) {
			this.context3D = new (openfl_display3D_Context3D().default)(this,renderer);
			this.__dispatchCreate();
		} else if(renderer.__type == (lime_graphics_RendererType().default).DOM) {
			this.__canvas = (js_Browser().default).get_document().createElement("canvas");
			this.__canvas.width = stage.stageWidth;
			this.__canvas.height = stage.stageHeight;
			var $window = stage.window;
			var transparentBackground = (Reflect().default).hasField($window.config,"background") && $window.config.background == null;
			var colorDepth = (Reflect().default).hasField($window.config,"colorDepth") ? $window.config.colorDepth : 16;
			var options = { alpha : transparentBackground || colorDepth > 16, antialias : (Reflect().default).hasField($window.config,"antialiasing") && $window.config.antialiasing > 0, depth : (Reflect().default).hasField($window.config,"depthBuffer") ? $window.config.depthBuffer : true, premultipliedAlpha : true, stencil : (Reflect().default).hasField($window.config,"stencilBuffer") && $window.config.stencilBuffer, preserveDrawingBuffer : false};
			this.__webgl = (js_html__$CanvasElement_CanvasUtil().default).getContextWebGL(this.__canvas,options);
			if(this.__webgl != null) {
				this.__renderContext = new (lime__$backend_html5_HTML5GLRenderContext().default)(this.__webgl);
				(lime_graphics_opengl_GL().default).context = this.__renderContext;
				this.context3D = new (openfl_display3D_Context3D().default)(this,renderer);
				var renderer1 = renderer;
				renderer1.element.appendChild(this.__canvas);
				this.__style = this.__canvas.style;
				this.__style.setProperty("position","absolute",null);
				this.__style.setProperty("top","0",null);
				this.__style.setProperty("left","0",null);
				this.__style.setProperty(renderer1.__transformOriginProperty,"0 0 0",null);
				this.__style.setProperty("z-index","-1",null);
				this.__dispatchCreate();
			} else {
				this.__dispatchError();
			}
		}
	},
	__dispatchError: function() {
		this.__contextRequested = false;
		this.dispatchEvent(new (openfl_events_ErrorEvent().default)("error",false,false,"Context3D not available"));
	},
	__dispatchCreate: function() {
		if(this.__contextRequested) {
			this.__contextRequested = false;
			this.dispatchEvent(new (openfl_events_Event().default)("context3DCreate"));
		}
	},
	__renderCairo: function(stage,renderer) {
		if(!this.visible) {
			return;
		}
		if(this.__contextRequested) {
			this.__dispatchError();
			this.__contextRequested = false;
		}
	},
	__renderCanvas: function(stage,renderer) {
		if(!this.visible) {
			return;
		}
		if(this.__contextRequested) {
			this.__dispatchError();
			this.__contextRequested = false;
		}
	},
	__renderDOM: function(stage,renderer) {
		if(!this.visible) {
			return;
		}
		if(this.__contextRequested && this.context3D == null) {
			this.__createContext(stage,renderer);
		}
		if(this.context3D != null) {
			(lime_graphics_opengl_GL().default).context = this.__renderContext;
			this.__resetContext3DStates();
		}
	},
	__renderGL: function(stage,renderer) {
		if(!this.visible) {
			return;
		}
		if(this.__contextRequested && this.context3D == null) {
			this.__createContext(stage,renderer);
		}
		if(this.context3D != null) {
			this.__resetContext3DStates();
			(openfl__$internal_stage3D_opengl_GLStage3D().default).render(this,renderer);
		}
	},
	__resize: function(width,height) {
		if(this.__canvas != null) {
			this.__canvas.width = width;
			this.__canvas.height = height;
		}
	},
	__resetContext3DStates: function() {
		this.context3D.__updateBlendFactors();
		this.context3D.__updateBackbufferViewport();
	},
	get_x: function() {
		return this.__x;
	},
	set_x: function(value) {
		if(this.__x == value) {
			return value;
		}
		this.__x = value;
		if(this.context3D != null) {
			this.context3D.__updateBackbufferViewport();
		}
		return value;
	},
	get_y: function() {
		return this.__y;
	},
	set_y: function(value) {
		if(this.__y == value) {
			return value;
		}
		this.__y = value;
		if(this.context3D != null) {
			this.context3D.__updateBackbufferViewport();
		}
		return value;
	}
});
Stage3D.prototype.__class__ = $hxClasses["openfl.display.Stage3D"] = Stage3D;

// Init

Object.defineProperties(Stage3D.prototype,{ "x" : { get : function () { return this.get_x (); }, set : function (v) { return this.set_x (v); }}, "y" : { get : function () { return this.get_y (); }, set : function (v) { return this.set_y (v); }}});

// Statics




// Export

exports.default = Stage3D;