var HTMLMapContainer = require("./html_map_container");
var ScreenPointMappingModule = require("./screen_point_mapping_module");
var ThemesModule = require("./themes_module");
var IndoorsModule = require("./indoors_module");
var PrecacheModule = require("./precache_module");
var CameraModule = require("./camera_module");
var PolygonModule = require("./polygon_module");

var EegeoLeafletMap = require("./eegeo_leaflet_map");

var EegeoMapController = function(mapId, emscriptenApi, domElement, apiKey, options) {

    var _mapId = mapId;
    var _emscriptenApi = emscriptenApi;

    var _screenPointMappingModule = new ScreenPointMappingModule(emscriptenApi);
    var _themesModule = new ThemesModule(emscriptenApi);
    var _indoorsModule = new IndoorsModule(emscriptenApi);
    var _precacheModule = new PrecacheModule(emscriptenApi);
    var _cameraModule = new CameraModule(emscriptenApi);
    var _polygonModule = new PolygonModule(emscriptenApi);

    var _modules = [
        _screenPointMappingModule,
        _themesModule,
        _indoorsModule,
        _precacheModule,
        _cameraModule,
        _polygonModule
    ];

    var _canvasId = options["canvasId"] || "canvas";
    var _canvasWidth = options["width"] || domElement.clientWidth;
    var _canvasHeight = options["height"] || domElement.clientHeight;

    var _mapContainer = new HTMLMapContainer(domElement, _canvasId, _canvasWidth, _canvasHeight);

    var _canvas = _mapContainer.canvas;

    var _Module = window.Module;
    _Module["canvas"] = _canvas;

    var defaultCenter = [37.7858, -122.401];
    var defaultZoom = 12;

    options.center = options.center || defaultCenter;
    options.zoom = options.zoom || defaultZoom;

    var centerLatLng = L.latLng(options.center);
    var distance = _cameraModule.zoomLevelToDistance(options.zoom);

    _Module["arguments"] = [
        _canvasId,
        _mapId.toString(),
        _canvasWidth.toString(),
        _canvasHeight.toString(),
        apiKey,
        centerLatLng.lat.toString(),
        centerLatLng.lng.toString(),
        distance.toString()
        ];

    options.zoomControl = false;

    this.leafletMap = new EegeoLeafletMap(_mapContainer.overlay, options, _cameraModule, _screenPointMappingModule, _precacheModule, _themesModule, _indoorsModule, _polygonModule);

    var _resizeCanvas = null;
    
    var _updateCanvasSize = function() {
        var newWidth = _canvas.clientWidth;
        var newHeight = _canvas.clientHeight;
        if (newWidth !== _canvasWidth || newHeight !== _canvasHeight) {
            _resizeCanvas(newWidth, newHeight);
            _canvasWidth = newWidth;
            _canvasHeight = newHeight;
        }
    };

    this.onInitialised = function(apiPointer) {
        _mapContainer.onInitialised();
        _resizeCanvas = _Module.cwrap("resizeCanvas", null, ["number", "number"]);
        _emscriptenApi.onInitialised(apiPointer, _onUpdate, _onDraw, _onInitialStreamingCompleted);
        _modules.forEach(function(module) {
            module.onInitialised();
        });
        this.leafletMap.onInitialised(_emscriptenApi);
    };

    var _this = this;

    var _onUpdate = function(deltaSeconds) {
        _updateCanvasSize();

        _modules.forEach(function(module) {
            module.onUpdate(deltaSeconds);
        });
    };

    var _onDraw = function(deltaSeconds) {
        _modules.forEach(function(module) {
            module.onDraw(deltaSeconds);
        });

        _this.leafletMap.update();
    };

    var _onInitialStreamingCompleted = function() {
        _modules.forEach(function(module) {
            module.onInitialStreamingCompleted();
        });
    };
};

module.exports = EegeoMapController;
@@ -1818,24 +1818,191 @@
 ons.zoom);%0A%0A
+    var indoorsEnabled = (typeof options%5B%22indoorsEnabled%22%5D === %22undefined%22 %7C%7C !!options%5B%22indoorsEnabled%22%5D);%0A    var indoorsEnabledArg = (indoorsEnabled) ? %221%22 : %220%22;%0A%0A
     _Module%5B
@@ -2248,16 +2248,43 @@
 String()
+,%0A        indoorsEnabledArg
 %0A       

Add option to disable indoor maps