// Class: lime._backend.html5.HTML5Renderer

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;
function Type() {return require("./../../../Type");}
function lime_graphics_RenderContext() {return require("./../../../lime/graphics/RenderContext");}
function lime_graphics_RendererType() {return require("./../../../lime/graphics/RendererType");}
function Reflect() {return require("./../../../Reflect");}
function lime_graphics_opengl_GL() {return require("./../../../lime/graphics/opengl/GL");}
function lime__$backend_html5_HTML5GLRenderContext() {return require("./../../../lime/_backend/html5/HTML5GLRenderContext");}
function lime_math_Rectangle() {return require("./../../../lime/math/Rectangle");}
function js_Browser() {return require("./../../../js/Browser");}
function Std() {return require("./../../../Std");}
function lime_graphics_Image() {return require("./../../../lime/graphics/Image");}

// Constructor

var HTML5Renderer = function(parent) {
	this.parent = parent;
}

// Meta

HTML5Renderer.__name__ = ["lime","_backend","html5","HTML5Renderer"];
HTML5Renderer.prototype = {
	create: function() {
		this.createContext();
		var _g = this.parent.context;
		if((Type().default).enumIndex(_g) == 0) {
			this.parent.window.backend.canvas.addEventListener("webglcontextlost",$bind(this,this.handleEvent),false);
			this.parent.window.backend.canvas.addEventListener("webglcontextrestored",$bind(this,this.handleEvent),false);
		}
	},
	createContext: function() {
		if(this.parent.window.backend.div != null) {
			this.parent.context = (lime_graphics_RenderContext().default).DOM(this.parent.window.backend.div);
			this.parent.type = (lime_graphics_RendererType().default).DOM;
		} else if(this.parent.window.backend.canvas != null) {
			var webgl = null;
			var renderType = this.parent.window.backend.renderType;
			var forceCanvas = renderType == "canvas";
			var forceWebGL = renderType == "opengl" || renderType == "webgl" || renderType == "webgl1" || renderType == "webgl2";
			var allowWebGL2 = renderType != "webgl1";
			if(forceWebGL || !forceCanvas && (!(Reflect().default).hasField(this.parent.window.config,"hardware") || this.parent.window.config.hardware)) {
				var transparentBackground = (Reflect().default).hasField(this.parent.window.config,"background") && this.parent.window.config.background == null;
				var colorDepth = (Reflect().default).hasField(this.parent.window.config,"colorDepth") ? this.parent.window.config.colorDepth : 16;
				var options = { alpha : transparentBackground || colorDepth > 16, antialias : (Reflect().default).hasField(this.parent.window.config,"antialiasing") && this.parent.window.config.antialiasing > 0, depth : (Reflect().default).hasField(this.parent.window.config,"depthBuffer") ? this.parent.window.config.depthBuffer : true, premultipliedAlpha : true, stencil : (Reflect().default).hasField(this.parent.window.config,"stencilBuffer") && this.parent.window.config.stencilBuffer, preserveDrawingBuffer : false};
				var glContextType = ["webgl","experimental-webgl"];
				if(allowWebGL2) {
					glContextType.unshift("webgl2");
				}
				var _g = 0;
				while(_g < glContextType.length) {
					var name = glContextType[_g];
					++_g;
					webgl = this.parent.window.backend.canvas.getContext(name,options);
					if(webgl != null) {
						break;
					}
				}
			}
			if(webgl == null) {
				this.parent.context = (lime_graphics_RenderContext().default).CANVAS(this.parent.window.backend.canvas.getContext("2d"));
				this.parent.type = (lime_graphics_RendererType().default).CANVAS;
			} else {
				(lime_graphics_opengl_GL().default).context = new (lime__$backend_html5_HTML5GLRenderContext().default)(webgl);
				this.parent.context = (lime_graphics_RenderContext().default).OPENGL((lime_graphics_opengl_GL().default).context);
				this.parent.type = (lime_graphics_RendererType().default).OPENGL;
			}
		}
	},
	flip: function() {
	},
	handleEvent: function(event) {
		var _g = event.type;
		switch(_g) {
		case "webglcontextlost":
			event.preventDefault();
			if((lime_graphics_opengl_GL().default).context != null) {
				(lime_graphics_opengl_GL().default).context.__contextLost = true;
			}
			this.parent.context = null;
			this.parent.onContextLost.dispatch();
			break;
		case "webglcontextrestored":
			this.createContext();
			this.parent.onContextRestored.dispatch(this.parent.context);
			break;
		default:
		}
	},
	readPixels: function(rect) {
		if(this.parent.window.backend.canvas != null) {
			if(rect == null) {
				rect = new (lime_math_Rectangle().default)(0,0,this.parent.window.backend.canvas.width,this.parent.window.backend.canvas.height);
			} else {
				rect.__contract(0,0,this.parent.window.backend.canvas.width,this.parent.window.backend.canvas.height);
			}
			if(rect.width > 0 && rect.height > 0) {
				var canvas = (js_Browser().default).get_document().createElement("canvas");
				canvas.width = (Std().default)["int"](rect.width);
				canvas.height = (Std().default)["int"](rect.height);
				var context = canvas.getContext("2d");
				context.drawImage(this.parent.window.backend.canvas,-rect.x,-rect.y);
				return (lime_graphics_Image().default).fromCanvas(canvas);
			}
		}
		return null;
	},
	render: function() {
	}
};
HTML5Renderer.prototype.__class__ = HTML5Renderer.prototype.constructor = $hxClasses["lime._backend.html5.HTML5Renderer"] = HTML5Renderer;

// Init



// Statics




// Export

exports.default = HTML5Renderer;