// Class: lime.app.Application

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 lime_app_Module() {return require("./../../lime/app/Module");}
function Reflect() {return require("./../../Reflect");}
function lime_ui_Window() {return require("./../../lime/ui/Window");}
function lime_graphics_Renderer() {return require("./../../lime/graphics/Renderer");}
function HxOverrides() {return require("./../../HxOverrides");}
function lime_system_System() {return require("./../../lime/system/System");}
function lime_app__$Event_$Int_$Void() {return require("./../../lime/app/_Event_Int_Void");}
function haxe_ds_IntMap() {return require("./../../haxe/ds/IntMap");}
function lime__$backend_html5_HTML5Application() {return require("./../../lime/_backend/html5/HTML5Application");}

// Constructor

var Application = function() {
	this.onUpdate = new (lime_app__$Event_$Int_$Void().default)();
	(lime_app_Module().default).call(this);
	if(Application.current == null) {
		Application.current = this;
	}
	this.modules = [];
	this.windowByID = new (haxe_ds_IntMap().default)();
	this.backend = new (lime__$backend_html5_HTML5Application().default)(this);
	this.registerModule(this);
}

// Meta

Application.__name__ = ["lime","app","Application"];
Application.__super__ = (lime_app_Module().default);
Application.prototype = $extend((lime_app_Module().default).prototype, {
	addModule: function(module) {
		module.registerModule(this);
		this.modules.push(module);
		if(this.__renderers.length > 0) {
			var _g = 0;
			var _g1 = this.__renderers;
			while(_g < _g1.length) {
				var renderer = _g1[_g];
				++_g;
				module.addRenderer(renderer);
			}
		}
		if(this.__windows.length > 0) {
			var _g2 = 0;
			var _g11 = this.__windows;
			while(_g2 < _g11.length) {
				var $window = _g11[_g2];
				++_g2;
				module.addWindow($window);
			}
		}
		module.setPreloader(this.__preloader);
	},
	addRenderer: function(renderer) {
		(lime_app_Module().default).prototype.addRenderer.call(this,renderer);
		var _g = 0;
		var _g1 = this.modules;
		while(_g < _g1.length) {
			var module = _g1[_g];
			++_g;
			module.addRenderer(renderer);
		}
	},
	create: function(config) {
		this.config = config;
		this.backend.create(config);
		if(config != null) {
			if((Reflect().default).hasField(config,"fps")) {
				this.set_frameRate(config.fps);
			}
			if((Reflect().default).hasField(config,"windows")) {
				var _g = 0;
				var _g1 = config.windows;
				while(_g < _g1.length) {
					var windowConfig = _g1[_g];
					++_g;
					var $window = new (lime_ui_Window().default)(windowConfig);
					this.createWindow($window);
					break;
				}
			}
			if(this.__preloader == null || this.__preloader.complete) {
				this.setPreloader(this.__preloader);
				var _g2 = 0;
				var _g11 = this.modules;
				while(_g2 < _g11.length) {
					var module = _g11[_g2];
					++_g2;
					this.setPreloader(this.__preloader);
				}
			}
		}
	},
	createWindow: function(window) {
		(lime_app_Module().default).prototype.addWindow.call(this,window);
		var _g = 0;
		var _g1 = this.modules;
		while(_g < _g1.length) {
			var module = _g1[_g];
			++_g;
			module.addWindow(window);
		}
		if(window.renderer == null) {
			var renderer = new (lime_graphics_Renderer().default)(window);
			this.addRenderer(renderer);
		}
		window.create(this);
		this.windowByID.set(window.id,window);
		window.onCreate.dispatch();
	},
	exec: function() {
		Application.current = this;
		return this.backend.exec();
	},
	onModuleExit: function(code) {
		this.backend.exit();
	},
	onWindowClose: function(window) {
		this.removeWindow(window);
	},
	removeModule: function(module) {
		if(module != null) {
			module.unregisterModule(this);
			(HxOverrides().default).remove(this.modules,module);
		}
	},
	removeWindow: function(window) {
		if(window != null && this.windowByID.exists(window.id)) {
			(HxOverrides().default).remove(this.__windows,window);
			this.windowByID.remove(window.id);
			window.close();
			if(window.renderer != null) {
				this.removeRenderer(window.renderer);
			}
			if(this.get_window() == window) {
				this.window = null;
			}
			if(this.__windows.length == 0) {
				(lime_system_System().default).exit(0);
			}
		}
	},
	setPreloader: function(preloader) {
		(lime_app_Module().default).prototype.setPreloader.call(this,preloader);
		var _g = 0;
		var _g1 = this.modules;
		while(_g < _g1.length) {
			var module = _g1[_g];
			++_g;
			module.setPreloader(preloader);
		}
	},
	get_frameRate: function() {
		return this.backend.getFrameRate();
	},
	set_frameRate: function(value) {
		return this.backend.setFrameRate(value);
	},
	get_preloader: function() {
		return this.__preloader;
	},
	get_renderer: function() {
		return this.__renderers[0];
	},
	get_renderers: function() {
		return this.__renderers;
	},
	get_window: function() {
		return this.__windows[0];
	},
	get_windows: function() {
		return this.__windows;
	}
});
Application.prototype.__class__ = $hxClasses["lime.app.Application"] = Application;

// Init

{
	var init = (lime__$backend_html5_HTML5Application().default)
	var p = Application.prototype;
	Object.defineProperties(p,{ "frameRate" : { get : p.get_frameRate, set : p.set_frameRate}, "preloader" : { get : p.get_preloader}, "renderer" : { get : p.get_renderer}, "renderers" : { get : p.get_renderers}, "window" : { get : p.get_window}, "windows" : { get : p.get_windows}});
};

// Statics




// Export

exports.default = Application;