class BasicMapComponent {
    render(container, component, width, height, configs) {
        this.container = container;
        this.component = component;

        this.map = new BMapGL.Map(this.container);
        this.map.addControl(new BMapGL.ZoomControl());

        this.firstTime = true;
        this.centerPoint = "108.991,34.297";
        this.zoomLevel = 10;

        for (const config of configs) {
            this.updateConfig(config);
        }
    }

    resize(width, height) {
    }

    updateConfig(config) {
        switch (config.k) {
            case "centerCoor":
                this.centerPoint = config.v;
                this.centerMap();
                break;
            case "zoomLevel":
                this.zoomLevel = parseInt(config.v);
                this.zoomMap();
                break;
            case "factors":
                this.factors = config.v.split(",");
                this.createPoints();
                break;
        }
    }

    centerMap() {
        const coor = this.centerPoint.split(",");
        if (this.firstTime) {
            this.map.centerAndZoom(new BMapGL.Point(Number(coor[0]), Number(coor[1])), this.zoomLevel);
            this.firstTime = false;
        }
        else {
            this.map.setCenter(data.points[0]);
        }
    }

    zoomMap() {
        this.map.setZoom(this.zoomLevel);
    }

    createPoints() {
        if (this.factors.length % 2 === 0) {
            this.points = [];
            this.markers = {};
            for (let i = this.factors.length / 2; i > 0; i--) {
                const p = new BMapGL.Point(-1, -1);
                this.points.push(p);
            }
        }
    }

    updateTableData(key, value, devices) {
        if (!this.points || !this.markers) {
            return;
        }

        const index = this.factors.indexOf(key);
        if (index !== -1) {
            const i1 = Math.floor(index / 2);
            const i2 = index % 2;

            let num = -1;
            if (value && !value.startsWith("@")) {
                try {
                    const n = parseFloat(value);
                    if (!isNaN(n)) {
                        num = n;
                    }
                }
                catch { }
            }

            if (i2 === 0) {
                this.points[i1].lng = num;
            }
            else {
                this.points[i1].lat = num;
            }

            const oldMarker = this.markers[i1];
            if (oldMarker) {
                map.removeOverlay(oldMarker);
            }

            this.markers[i1] = null;
            if (this.points[i1].lng !== -1 && this.points[i1].lat !== -1) {
                const marker = new BMapGL.Marker(this.points[i1]);
                this.map.addOverlay(marker);
                this.markers[i1] = marker;
            }
        }
    }
}