// Class: lime._backend.html5.HTML5Application

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 js_Browser() {return require("./../../../js/Browser");}
function Std() {return require("./../../../Std");}
function lime_ui_Joystick() {return require("./../../../lime/ui/Joystick");}
function lime__$backend_html5_GameDeviceData() {return require("./../../../lime/_backend/html5/GameDeviceData");}
function lime_ui_Gamepad() {return require("./../../../lime/ui/Gamepad");}
function haxe_ds_IntMap() {return require("./../../../haxe/ds/IntMap");}
function lime_media_AudioManager() {return require("./../../../lime/media/AudioManager");}
function lime_system_Sensor() {return require("./../../../lime/system/Sensor");}
function lime_system_SensorType() {return require("./../../../lime/system/SensorType");}

// Constructor

var HTML5Application = function(parent) {
	this.gameDeviceCache = new (haxe_ds_IntMap().default)();
	this.parent = parent;
	this.currentUpdate = 0;
	this.lastUpdate = 0;
	this.nextUpdate = 0;
	this.framePeriod = -1;
	(lime_media_AudioManager().default).init();
	this.accelerometer = (lime_system_Sensor().default).registerSensor((lime_system_SensorType().default).ACCELEROMETER,0);
}

// Meta

HTML5Application.__name__ = ["lime","_backend","html5","HTML5Application"];
HTML5Application.prototype = {
	convertKeyCode: function(keyCode) {
		if(keyCode >= 65 && keyCode <= 90) {
			return keyCode + 32;
		}
		switch(keyCode) {
		case 16:
			return 1073742049;
		case 17:
			return 1073742048;
		case 18:
			return 1073742050;
		case 20:
			return 1073741881;
		case 33:
			return 1073741899;
		case 34:
			return 1073741902;
		case 35:
			return 1073741901;
		case 36:
			return 1073741898;
		case 37:
			return 1073741904;
		case 38:
			return 1073741906;
		case 39:
			return 1073741903;
		case 40:
			return 1073741905;
		case 45:
			return 1073741897;
		case 46:
			return 127;
		case 96:
			return 1073741922;
		case 97:
			return 1073741913;
		case 98:
			return 1073741914;
		case 99:
			return 1073741915;
		case 100:
			return 1073741916;
		case 101:
			return 1073741917;
		case 102:
			return 1073741918;
		case 103:
			return 1073741919;
		case 104:
			return 1073741920;
		case 105:
			return 1073741921;
		case 106:
			return 1073741909;
		case 107:
			return 1073741911;
		case 109:
			return 1073741910;
		case 110:
			return 1073741923;
		case 111:
			return 1073741908;
		case 112:
			return 1073741882;
		case 113:
			return 1073741883;
		case 114:
			return 1073741884;
		case 115:
			return 1073741885;
		case 116:
			return 1073741886;
		case 117:
			return 1073741887;
		case 118:
			return 1073741888;
		case 119:
			return 1073741889;
		case 120:
			return 1073741890;
		case 121:
			return 1073741891;
		case 122:
			return 1073741892;
		case 123:
			return 1073741893;
		case 124:
			return 1073741928;
		case 125:
			return 1073741929;
		case 126:
			return 1073741930;
		case 144:
			return 1073741907;
		case 186:
			return 59;
		case 187:
			return 61;
		case 188:
			return 44;
		case 189:
			return 45;
		case 190:
			return 46;
		case 191:
			return 47;
		case 192:
			return 96;
		case 219:
			return 91;
		case 220:
			return 92;
		case 221:
			return 93;
		case 222:
			return 39;
		}
		return keyCode;
	},
	create: function(config) {
	},
	exec: function() {
		(js_Browser().default).get_window().addEventListener("keydown",$bind(this,this.handleKeyEvent),false);
		(js_Browser().default).get_window().addEventListener("keyup",$bind(this,this.handleKeyEvent),false);
		(js_Browser().default).get_window().addEventListener("focus",$bind(this,this.handleWindowEvent),false);
		(js_Browser().default).get_window().addEventListener("blur",$bind(this,this.handleWindowEvent),false);
		(js_Browser().default).get_window().addEventListener("resize",$bind(this,this.handleWindowEvent),false);
		(js_Browser().default).get_window().addEventListener("beforeunload",$bind(this,this.handleWindowEvent),false);
		(js_Browser().default).get_window().addEventListener("devicemotion",$bind(this,this.handleSensorEvent),false);
		
				if (!CanvasRenderingContext2D.prototype.isPointInStroke) {
					CanvasRenderingContext2D.prototype.isPointInStroke = function (path, x, y) {
						return false;
					};
				}
				if (!CanvasRenderingContext2D.prototype.isPointInPath) {
					CanvasRenderingContext2D.prototype.isPointInPath = function (path, x, y) {
						return false;
					};
				}
				
				if ('performance' in window == false) {
					window.performance = {};
				}
				
				if ('now' in window.performance == false) {
					var offset = Date.now();
					if (performance.timing && performance.timing.navigationStart) {
						offset = performance.timing.navigationStart
					}
					window.performance.now = function now() {
						return Date.now() - offset;
					}
				}
				
				var lastTime = 0;
				var vendors = ['ms', 'moz', 'webkit', 'o'];
				for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
					window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
					window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
				}
				
				if (!window.requestAnimationFrame)
					window.requestAnimationFrame = function(callback, element) {
						var currTime = new Date().getTime();
						var timeToCall = Math.max(0, 16 - (currTime - lastTime));
						var id = window.setTimeout(function() { callback(currTime + timeToCall); }, 
						  timeToCall);
						lastTime = currTime + timeToCall;
						return id;
					};
				
				if (!window.cancelAnimationFrame)
					window.cancelAnimationFrame = function(id) {
						clearTimeout(id);
					};
				
				window.requestAnimFrame = window.requestAnimationFrame;
			;
		this.lastUpdate = new Date().getTime();
		this.handleApplicationEvent();
		return 0;
	},
	exit: function() {
	},
	getFrameRate: function() {
		if(this.framePeriod < 0) {
			return 60;
		} else if(this.framePeriod == 1000) {
			return 0;
		} else {
			return 1000 / this.framePeriod;
		}
	},
	handleApplicationEvent: function(__) {
		if(this.parent.get_window() != null) {
			this.parent.get_window().backend.updateSize();
		}
		this.updateGameDevices();
		this.currentUpdate = new Date().getTime();
		if(this.currentUpdate >= this.nextUpdate) {
			this.deltaTime = this.currentUpdate - this.lastUpdate;
			this.parent.onUpdate.dispatch((Std().default)["int"](this.deltaTime));
			if(this.parent.get_renderer() != null && this.parent.get_renderer().context != null) {
				this.parent.get_renderer().render();
				this.parent.get_renderer().onRender.dispatch();
				if(!this.parent.get_renderer().onRender.canceled) {
					this.parent.get_renderer().flip();
				}
			}
			if(this.framePeriod < 0) {
				this.nextUpdate = this.currentUpdate;
				this.nextUpdate = this.currentUpdate;
			} else {
				this.nextUpdate = this.currentUpdate + this.framePeriod;
			}
			this.lastUpdate = this.currentUpdate;
		}
		(js_Browser().default).get_window().requestAnimationFrame($bind(this,this.handleApplicationEvent));
	},
	handleKeyEvent: function(event) {
		if(this.parent.get_window() != null) {
			var keyCode = this.convertKeyCode(event.keyCode != null ? event.keyCode : event.which);
			var modifier = (event.shiftKey ? 3 : 0) | (event.ctrlKey ? 192 : 0) | (event.altKey ? 768 : 0) | (event.metaKey ? 3072 : 0);
			if(event.type == "keydown") {
				this.parent.get_window().onKeyDown.dispatch(keyCode,modifier);
				if(this.parent.get_window().onKeyDown.canceled) {
					event.preventDefault();
				}
			} else {
				this.parent.get_window().onKeyUp.dispatch(keyCode,modifier);
				if(this.parent.get_window().onKeyUp.canceled) {
					event.preventDefault();
				}
			}
		}
	},
	handleSensorEvent: function(event) {
		this.accelerometer.onUpdate.dispatch(event.accelerationIncludingGravity.x,event.accelerationIncludingGravity.y,event.accelerationIncludingGravity.z);
	},
	handleWindowEvent: function(event) {
		if(this.parent.get_window() != null) {
			var _g = event.type;
			switch(_g) {
			case "beforeunload":
				if(!event.defaultPrevented) {
					this.parent.get_window().onClose.dispatch();
					if(this.parent.get_window() != null && this.parent.get_window().onClose.canceled) {
						event.preventDefault();
					}
				}
				break;
			case "blur":
				this.parent.get_window().onFocusOut.dispatch();
				this.parent.get_window().onDeactivate.dispatch();
				break;
			case "focus":
				this.parent.get_window().onFocusIn.dispatch();
				this.parent.get_window().onActivate.dispatch();
				break;
			case "resize":
				this.parent.get_window().backend.handleResizeEvent(event);
				break;
			}
		}
	},
	setFrameRate: function(value) {
		if(value >= 60) {
			this.framePeriod = -1;
		} else if(value > 0) {
			this.framePeriod = 1000 / value;
		} else {
			this.framePeriod = 1000;
		}
		return value;
	},
	updateGameDevices: function() {
		var devices = (lime_ui_Joystick().default).__getDeviceData();
		if(devices == null) {
			return;
		}
		var id;
		var gamepad;
		var joystick;
		var data;
		var cache;
		var _g1 = 0;
		var _g = devices.length;
		while(_g1 < _g) {
			var i = _g1++;
			id = i;
			data = devices[id];
			if(data == null) {
				continue;
			}
			if(!this.gameDeviceCache.exists(id)) {
				cache = new (lime__$backend_html5_GameDeviceData().default)();
				cache.id = id;
				cache.connected = data.connected;
				var _g3 = 0;
				var _g2 = data.buttons.length;
				while(_g3 < _g2) {
					var i1 = _g3++;
					cache.buttons.push(data.buttons[i1].value);
				}
				var _g31 = 0;
				var _g21 = data.axes.length;
				while(_g31 < _g21) {
					var i2 = _g31++;
					cache.axes.push(data.axes[i2]);
				}
				if(data.mapping == "standard") {
					cache.isGamepad = true;
				}
				this.gameDeviceCache.set(id,cache);
				if(data.connected) {
					(lime_ui_Joystick().default).__connect(id);
					if(cache.isGamepad) {
						(lime_ui_Gamepad().default).__connect(id);
					}
				}
			}
			cache = this.gameDeviceCache.get(id);
			joystick = (lime_ui_Joystick().default).devices.get(id);
			gamepad = (lime_ui_Gamepad().default).devices.get(id);
			if(data.connected) {
				var button;
				var value;
				var _g32 = 0;
				var _g22 = data.buttons.length;
				while(_g32 < _g22) {
					var i3 = _g32++;
					value = data.buttons[i3].value;
					if(value != cache.buttons[i3]) {
						if(i3 == 6) {
							joystick.onAxisMove.dispatch(data.axes.length,value);
							if(gamepad != null) {
								gamepad.onAxisMove.dispatch(4,value);
							}
						} else if(i3 == 7) {
							joystick.onAxisMove.dispatch(data.axes.length + 1,value);
							if(gamepad != null) {
								gamepad.onAxisMove.dispatch(5,value);
							}
						} else {
							if(value > 0) {
								joystick.onButtonDown.dispatch(i3);
							} else {
								joystick.onButtonUp.dispatch(i3);
							}
							if(gamepad != null) {
								switch(i3) {
								case 0:
									button = 0;
									break;
								case 1:
									button = 1;
									break;
								case 2:
									button = 2;
									break;
								case 3:
									button = 3;
									break;
								case 4:
									button = 9;
									break;
								case 5:
									button = 10;
									break;
								case 8:
									button = 4;
									break;
								case 9:
									button = 6;
									break;
								case 10:
									button = 7;
									break;
								case 11:
									button = 8;
									break;
								case 12:
									button = 11;
									break;
								case 13:
									button = 12;
									break;
								case 14:
									button = 13;
									break;
								case 15:
									button = 14;
									break;
								case 16:
									button = 5;
									break;
								default:
									continue;
								}
								if(value > 0) {
									gamepad.onButtonDown.dispatch(button);
								} else {
									gamepad.onButtonUp.dispatch(button);
								}
							}
						}
						cache.buttons[i3] = value;
					}
				}
				var _g33 = 0;
				var _g23 = data.axes.length;
				while(_g33 < _g23) {
					var i4 = _g33++;
					if(data.axes[i4] != cache.axes[i4]) {
						joystick.onAxisMove.dispatch(i4,data.axes[i4]);
						if(gamepad != null) {
							gamepad.onAxisMove.dispatch(i4,data.axes[i4]);
						}
						cache.axes[i4] = data.axes[i4];
					}
				}
			} else if(cache.connected) {
				cache.connected = false;
				(lime_ui_Joystick().default).__disconnect(id);
				(lime_ui_Gamepad().default).__disconnect(id);
			}
		}
	}
};
HTML5Application.prototype.__class__ = HTML5Application.prototype.constructor = $hxClasses["lime._backend.html5.HTML5Application"] = HTML5Application;

// Init



// Statics




// Export

exports.default = HTML5Application;