let descricaoFCV = ko.observable('');

function ViewModel(usarGoogleGeoCode, chaveMapa, DadosRelatorioFCV) {
    const mSelf = this;
    descricaoFCV(DadosRelatorioFCV.Descricao);
    mSelf.currentMap = new InlogMaps.Map;

    let params = {};
    let mapType = "";

    if (usarGoogleGeoCode) {
        params = {
            libraries: ['drawing', 'places'],
            apiKey: chaveMapa
        };

        mapType = InlogMaps.MapType.Google;

    } else {
        params = {
        };

        mapType = InlogMaps.MapType.Leaflet

    }

    mSelf.currentMap.initialize(mapType, params, "imprimir-relatorio").then(() => {
        DadosRelatorioFCV.Enlaces.forEach(enlace => drawPontos(enlace.Coordenadas, "#5570cc", "Planejamento-" + enlace.CodigoLinha, 1, 7, 1.0))
        DadosRelatorioFCV.Terminais.forEach(terminal => drawTerminais(terminal));
        drawPontos(DadosRelatorioFCV.Rastreamentos, "#ff0000", 'Rastreamento',3, 2, 0.3)
    });
    LoadingOff();
}

function drawPontos(values, color, nameOfPolyline, index, weight, opacity) {
    if (values.length > 0) {
        let options = new InlogMaps.PolylineOptions();
        options.path = [];


        options.path = values.map(ponto => [formatLatLong(ponto.Latitude), formatLatLong(ponto.Longitude)])

        options.addToMap = true;
        options.fitBounds = true;
        options.color = color;
        options.opacity = opacity;
        options.weight = weight;
        options.draggable = false;
        options.editable = false;
        options.zIndex = index;

        vm.currentMap.drawPolyline(nameOfPolyline, options);
    }
}

function drawTerminais(forma) {
    switch (forma.TipoForma) {
        case 1:
            let options = new InlogMaps.PolygonOptions();
            options.path = [];

            for (var j = 0; j < forma.FormasCoordernadas.length; j++) {
                options.path.push([formatLatLong(forma.FormasCoordernadas[j].Latitude), formatLatLong(forma.FormasCoordernadas[j].Longitude)]);
            }

            options.addToMap = true;
            options.fitBounds = false;
            options.fillColor = "#FF0000";
            options.fillOpacity = 0.4;
            options.color = forma.Cor;
            options.draggable = false;
            options.editable = false;
            options.zIndex = 2;

            vm.currentMap.drawPolygon('Terminal', options);
            break
        case 3: // circulo
            var centro = [formatLatLong(forma.LatitudeCentro), formatLatLong(forma.LongitudeCentro)];
            let options2 = new InlogMaps.CircleOptions();
            options2.center = centro;
            options2.radius = forma.Raio;
            options2.addToMap = true;
            options2.weight = 1;
            options2.fillOpacity = 0.4;
            options2.color = '#000000';
            options2.fitBounds = false;
            options2.draggable = false;
            options2.editable = false;
            options2.weight = 2.0;
            options2.opacity = 1.0;
            vm.currentMap.drawCircle('PontosControle', options2);
            break;

    }
}

function formatLatLong(latLong) {
    latLong = latLong.toString();
    if (latLong.length > 9) {
        latLong = latLong.replace(/(\d{3})/, "$1.");
    } else {
        latLong = latLong.replace(/(\d{2})/, "$1.");
    }

    return parseFloat(latLong);
}

function Imprimir() {
    $(".btn").css('display', 'none');
    window.print();
    $(".btn").css('display', 'block');
}