{% extends "./base_ms.html" %}
{% block plugins %}
<script type="text/javascript" src="{{ STATIC_URL }}geonode/js/ms2/utils/ms2_base_plugins.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}geonode/js/ms2/utils/ms2_map_viewer_plugins.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}geonode/js/ms2/utils/ms2_composer_plugins.js"></script>
{% endblock %}
{% block style %}
<style>
	body {
        padding-top: 0px !important;
	}
	#ms-container {
		position: absolute;
		top: 72px;
		bottom: 0px;
        width: 100%;
        z-index: 1029;
        background-color: white;
    }
</style>
{% endblock %}
{% block app_config %}
{% autoescape off %}
<script type="text/javascript">
    const geoserver = "{{ OGC_SERVER.default.PUBLIC_LOCATION|default:"" }}";
    const ms2_config = {{ ms2_config|default:"false" }};
    document.addEventListener('DOMContentLoaded', function () {
        const userDetails = {
            "User": user,
            "access_token": accessToken
        }
        initMapstore2Api('edit', function(MapStore2) {
            // you have to add the configuration for stylerplugin
            MS2_PLUGINS = window.squashMS2PlugCfg(MS2_BASE_PLUGINS, MS2_MAP_PLUGINS, MS2_EDIT_PLUGINS);
            const stylerCfg = {
                "name": "StyleEditor",
                    "cfg": {
                            "styleService": {
                            "baseUrl": "{{ SITEURL }}gs/",
                            "formats": ['css','sld'],
                            "availableUrls": [geoserver, "{{ SITEURL }}gs/"]
                        },
                        "editingAllowedRoles": null
                    }
                }
            MS2_PLUGINS.desktop.push(stylerCfg);
            MS2_PLUGINS["mobile"] = MS2_PLUGINS.desktop;
            MapStore2.create('ms-container', {
                config: ms2_config,
                mapId: ms2_config.map && ms2_config.map.info && ms2_config.map.info.id,
                initialState: {
                    defaultState: {
                        map: {center: {x: 13, y: 45, crs: "EPSG:4326"}},
                        maptype: {mapType: maptype},
                        mode: "embedded",
                        maps : {
                            enabled: false,
                            showMapDetails: true,
                            errors: [],
                            searchText: "",
                            results: ""},
                        mousePosition: {enabled: false},
                        controls: {
                            toolbar: {
                                active: null,
                                expanded: false
                            }
                        },
                        mapInfo: {enabled: true, infoFormat: "text/html" },
                        security: {
                                token: "{{ ACCESS_TOKEN }}"
                            }
                    }
                },
                proxy: "{{ SITEURL }}proxy/?url=",
                printingEnabled: true,
                localConfig: {
                    geonode_url: "{{ SITEURL }}",
                    genode_rest_api: "{{ SITEURL }}mapstore/rest/",
                    loadAfterTheme: true,
                    printUrl: "{{ SITEURL }}gs/pdf/info.json",
                    translations: '',
                    useAuthenticationRules: true,
                    authenticationRules: [{
                        "urlPattern": ".*geostore.*",
                        "method": "bearer"
                    }, {
                        "urlPattern": ".*geoserver.*",
                        "authkeyParamName": "access_token",
                        "method": "authkey"
                    }]
                },
                plugins: MS2_PLUGINS
            });
            if (user && user.id) {
                MapStore2.triggerAction({type: "LOGIN_SUCCESS", userDetails});
            }
            if (ms2_config) {
                MapStore2.triggerAction({
                    type: "MAP_CONFIG_LOADED",
                    config: ms2_config,
                    legacy: !!ms2_config.map && ms2_config.map.info && ms2_config.map.info.id,
                    mapId: ms2_config.map && ms2_config.map.info && ms2_config.map.info.id
                });
            }
        });
    });
</script>
{% endautoescape %}
{% endblock %}
{% block map_content %}
<div id="ms-container" class="ms2">
        <div class="_ms2_init_spinner _ms2_init_center">
            <div></div>
        </div>
        <div class="_ms2_init_text _ms2_init_center">Loading MapStore</div>
</div>
{% endblock %}
