/**
 * @description  地图icon
 * @author       朱琦
 * @date         2022/3/4
 */

import { defaultIconConfig, defaultIconSize } from '../../config/map-default-config'
import { getThemeIconConfig } from '../../theme'
import { getIconName } from '../../public-function'

class MapIcon {
    constructor(theme) {
        this.load(theme)
    }
    /**
     * 获取该主题的图标配置
     * @param {String} theme 主题类型
     */
    load(theme) {
        this.theme = theme
        this.iconConfig = getThemeIconConfig(theme)
    }
    /**
     * 根据名称获取icon配置
     * @param {String} name icon name
     * @returns iconConfig
     */
    getIconConfig(name) {
        const findIndex = this.iconConfig.findIndex(config => config.name === name)
        const config = findIndex > -1 ? this.iconConfig[findIndex] : defaultIconConfig
        return Object.assign({}, config)
    }
    /**
     * 获取打点 图标配置
     * @param { String | Object} markerConfig icon配置
     * String: 图标名称
     * object:{
     *  icon:图标名称
     *  size:[Array] 图标尺寸
     * }
     * @param {Object} layerConfig marker图层配置
     */
    getMarkerIcon(markerConfig, layerConfig = {}) {
        let data = markerConfig.constructor === String ? { icon: markerConfig } : markerConfig

        const iconConfigure = this.getIconConfigure(data, layerConfig)
        const size = iconConfigure.size

        return {
            size: new AMap.Size(size[0], size[1]),
            image: iconConfigure.url,
            imageOffset: new AMap.Pixel(0, 0),
            imageSize: new AMap.Size(size[0], size[1]),
            pointSize: size
        };
    }
    /**
     * 根据data和图层配置 获取 点位配置
     * @param {Object} data 数据
     * @param {Object} config 图层配置
     * @returns 图标配置项
     */
    getIconConfigure(data, config = {}) {
        const iconName = data.icon || getIconName(data, config.iconFilter)
        const iconConfig = this.getIconConfig(iconName)
        data.icon = iconName
        const size = getIconSize(data, config.iconSize, data.size || iconConfig.size)
        return {
            ...iconConfig,
            size
        }
    }
}

/**
 * 获取默认打点尺寸
 * @param {String} name 图标名
 * @returns [Array] 图标尺寸
 */
const getDefaultIconSize = (name) => {
    let size = [defaultIconSize, defaultIconSize]

    if (!name) return size

    if (name.includes('-car')) {
        // 车辆
        size = [defaultIconSize * 0.8, defaultIconSize]
    } else if (name.includes('-gl-')) {
        // 公路
        size = [defaultIconSize, defaultIconSize * 0.86]
    } else if (name.includes('-gs-')) {
        // 高速
        size = [defaultIconSize, defaultIconSize * 1.18]
    }
    return size
}
/**
 * 获取图标尺寸
 * @param {String | Object} data 数据
 * @param {Array | Function} iconSize 图标尺寸配置
 * @param {Array} defaultSize 图标默认尺寸  该优先级大于getDefaultIconSize
 * @returns 图标尺寸
 */
const getIconSize = (data, iconSize, defaultSize) => {
    
    const iconData = data && data.constructor === Object ? data : { icon: data}
    const iconName = iconData.icon
    let size = defaultSize || getDefaultIconSize(iconName)
    
    if (iconData.size && iconData.size.length === 2) {
        // iconData.size 优先级最高
        size = iconData.size
    } else {
        if (iconSize && iconSize.constructor === Array) {
            size = iconSize
        } else if (iconSize && iconSize.constructor === Function) {
            size = iconSize(iconName)
        }
    }


    return [Number(size[0]), Number(size[1])]
}

/**
 * 创建主题实例
 * @param {String} theme 主题类型
 * @returns 主题实例
 */
const createMapIcon = (theme) => {
    return new MapIcon(theme)
}

export { createMapIcon, getDefaultIconSize, getIconSize }