export function getMarkers(map,data,line){
    if(!data.tmpMarker){
        data.tmpMarker = new AMap.Marker({
            anchor: 'bottom-center',
            offset: [0, -15],
        });
    }
    return line.filter((_, index) => index % 1 === 0).map((item) => {
        let text = {
            fontSize: 10,
            content: `时间:${item.locationTime}<br>速度: ${((item.speed || 0) * 3.6).toFixed(2)}km/h 方向角: ${(item.bearing || 0).toFixed(2)}°`
        }

        let marker = new AMap.LabelMarker({
            name: item.id,
            position: [item.longitude, item.latitude],
            zIndex: 10,
            icon: {
                // 图标类型，现阶段只支持 image 类型
                type: 'image',
                // 图片 url
                image: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',
                // 图片尺寸
                size: [6, 9],
                // 图片相对 position 的锚点，默认为 bottom-center
                anchor: 'bottom-center'
            },
            // text,
        })

        function show(e) {
            let position = e.data.data && e.data.data.position;

            if (position) {
                data.tmpMarker.setContent(
                    `<div class="amap-info-window"
                                    style="padding: .75rem 1.25rem;font-size:12px;
                                    margin-bottom: 1rem;
                                    border-radius: .25rem;
                                    position: fixed;
                                    top: 1rem;
                                    background-color: white;
                                    width: auto;
                                    min-width: 22rem;
                                    border-width: 0;
                                    right: 1rem;
                                    box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);">`
                    + text.content +
                    '<div class="amap-info-sharp"></div>' +
                    '</div>');
                data.tmpMarker.setPosition(position);
                map.add(data.tmpMarker);
            }
        }

        function hide() {
            map.remove(data.tmpMarker);
        }

        marker.on('mouseover', (e) => show(e));
        marker.on('touchstart', (e) => show(e));
        marker.on('mouseout', () => hide());
        marker.on('touchend', () => hide());

        return marker;
    });
}

export function drawMarkers(map,data,options,lines){
    if(!data.labelsLayer){
        data.labelsLayer = new AMap.LabelsLayer({
            zooms: [3, 20],
            zIndex: 1000,
            // 该层内标注是否避让
            collision: true,
            // 设置 allowCollision：true，可以让标注避让用户的标注
            allowCollision: false,
        });
        map.add(data.labelsLayer)
    }
    data.labelsLayer.remove(data.markers)
    data.markers = []

    if(!options.showMarker){
        return
    }
    data.markers = lines.map(line=>{
        return getMarkers(map,data,line)
    }).flat()
    data.labelsLayer.add(data.markers)
}
