/**
 * @description  2.0 map sdk
 * @author       朱琦
 * @date         2022/1/7 17:24
 */

import MapSdk from "./";
import mapPublic from './map-public'

class MapSdkT extends MapSdk {

    constructor(map, mapId, mapConfig, pointClick) {
        super(map, mapId, mapConfig, pointClick);
    }

    /**
     * 创建点聚合
     */
    createMvMapMarkerCluster(data, config = {}, callback) {
        return new Promise((resolve) => {
            let points = [];
            if (data) {
                data.map(item => {
                    points.push({ lnglat: item.path, ...item });
                })
            }
            let cluster = new AMap.MarkerClusterer(this.map, points, {
                renderMarker: (context) => {
                    if (!context.data.length) return
                    const marker = context.marker
                    marker.setExtData(context.data[0])
                    // 设置marker 选中 点击
                    this.createMvMapMarker(marker, config, callback)
                },
                renderClusterMarker: (context) => mapPublic.renderClusterMarker(context, config),
                ...config
            })
            cluster.on('click', e => {
                if (e.clusterData && e.clusterData.length > 1) {
                    callback(e, e.clusterData, { ...config, CLICK_TYPE: 'Cluster' })
                }
            })
            resolve(cluster)
        })
    }

    /**
     * 添加地图控件
     * @param controls( ['scale','toolBar','controlBar','overView'] )-Array
     *          -- scale        比例尺
     *          -- toolBar      工具条
     *          -- controlBar   工具条方向盘
     *          -- overView     显示鹰眼
     */
    addMvMapControl(controls) {
        if (controls.indexOf('scale') != -1) {
            this.map.addControl(new AMap.Scale())
        }
        if (controls.indexOf('toolBar') != -1) {
            this.map.addControl(new AMap.ToolBar({
                position: { top: '10px', right: '10px' }
            }))
        }
        if (controls.indexOf('controlBar') != -1) {
            this.map.addControl(new AMap.ControlBar())
        }
        if (controls.indexOf('overView') != -1) {
            this.map.addControl(new AMap.HawkEye())
        }
    }

    /**
     * 创建热力图
     *  - data Array
     *      path : [lng,lat]
     *  - config Object
     *      radius  热力图中单个点的半径
     *      opacity 热力图透明度区间
     *      zooms 支持的缩放级别范围
     *      gradient    热力图的渐变区间
     *      max 最大权重值
     */
    createMvMapHeatMap(data, config = {}) {
        let self = this;
        let heatMap = new AMap.HeatMap(self.map, {
            radius: config.radius || 25,
            opacity: config.opacity || [0, 1],
            zooms: config.zooms || [3, 20],
            gradient: config.gradient || {
                0.5: 'blue',
                0.65: 'rgb(117,211,248)',
                0.7: 'rgb(0, 255, 0)',
                0.9: '#ffea00',
                1.0: 'red'
            }
        });
        heatMap.setDataSet({
            data: data,
            max: config.max || 100
        });
        return heatMap;
    }
}

export default MapSdkT;