/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


function loadServiceAreaMap() {
    if (MapControl.supported()) {
        if (serviceAreaMap == null) {
            app.preloader.show();
            var mapboxel = document.getElementById("mapbox-servicearea");
            serviceAreaMap = new MapControl(mapboxel, true);
            serviceAreaMap.reloadMap();
            var serviceAreaMapLoaded = false;

            serviceAreaMap.mapObj.on('load', function () {
                serviceAreaMapLoaded = true;
            });

            apirequest(SETTINGS.apis.servicearea, {}, function (resp) {
                var loadWhenMapReady = function () {
                    if (serviceAreaMapLoaded) {
                        setupServiceAreaMap(resp);
                    } else {
                        setTimeout(function () {
                            loadWhenMapReady();
                        }, 100);
                    }
                }
                loadWhenMapReady();
            }, function (xhr) {
                app.preloader.hide();
                try {
                    var error = $.parseJSON(xhr.responseText);
                    if (error && typeof error.msg != 'undefined') {
                        app.dialog.alert(error.msg, "Error");
                        sendErrorReport("Service Area", "Couldn't get service area GeoJSON", error.msg);
                    } else {
                        app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
                        sendErrorReport("Service Area", "Couldn't get service area GeoJSON", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
                    }
                } catch (ex) {
                    app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
                    sendErrorReport("Service Area", "Couldn't get service area GeoJSON", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
                }
            });
        }
    } else {
        showWebGLErrorMessage();
        app.preloader.hide();
    }
}

function setupServiceAreaMap(geojson) {
    app.preloader.hide();
    serviceAreaMap.mapObj.jumpTo({center: [-112.005, 46.589], zoom: 9});
    serviceAreaMap.mapObj.addSource('servicearea', {
        'type': 'geojson',
        'data': geojson
    });
    serviceAreaMap.mapObj.addLayer({
        'id': 'courierservicearea',
        'type': 'fill',
        'source': 'servicearea',
        'paint': {
            'fill-color': '#005387',
            'fill-opacity': 0.4
        },
        'filter': ['==', 'label', 'Courier']
    });
    serviceAreaMap.mapObj.addLayer({
        'id': 'notaryservicearea',
        'type': 'fill',
        'source': 'servicearea',
        'paint': {
            'fill-color': '#008733',
            'fill-opacity': 0.4
        },
        'filter': ['==', 'label', 'Notary']
    });
}

function openServiceAreaExtraChargeInfo() {
    app.dialog.alert("If you need a notary outside the green zone, the travel fee will be calculated \n\
using the current IRS mileage rate. If you need courier service outside the blue zone, we can accomodate \n\
but our delivery window guarantees will not apply.", "Info");
}