import {
  GeometryType,
  PictureMarkerSymbol,
  Log
} from '@mapgis/webclient-common'
import { BaseGraphic } from './index'

const { Cartesian2, Math, Color } = Cesium

/**
 * PictureMarkerGraphic
 * @private
 * @param option - {Object} 构造参数
 * @param {Feature} [option.feature = null] 构造Cesium几何的要素对象
 * */
class PictureMarkerGraphic extends BaseGraphic {
  constructor(options) {
    super(options)
  }

  /**
   * 重载基类的checkSymbol方法
   * @public
   * @return {Boolean} 是否为支持的符号
   * */
  checkSymbol() {
    if (!(this._feature.symbol instanceof PictureMarkerSymbol)) {
      Log.info('要素的符号类型不是PictureMarkerSymbol！')
      return false
    }
    return true
  }

  /**
   * 重载基类的_createPrimitive方法
   * @private
   * */
  _createPrimitive() {
    // 获取要素几何
    const _geometry = this._feature.geometry
    switch (_geometry.type) {
      case GeometryType.point:
        const _billboard = this._billboardsCollection.add(
          this._createBillboardOptions(this._getPositions()[0])
        )
        _billboard.id = this._feature.id
        this._billboards.push(_billboard)
        break
      case GeometryType.multiPoint:
        const _positions = this._getPositions()
        for (let i = 0; i < _positions.length; i++) {
          const _billboard = this._billboardsCollection.add(
            this._createBillboardOptions(_positions[i])
          )
          _billboard.id = this._feature.id
          this._billboards.push(_billboard)
        }
        break
      default:
        break
    }
  }

  /**
   * 通过点坐标构造一个billboard的初始化参数
   * @private
   * @param {Cartesian3} position 三维几何点坐标
   * @return {Object} billboard构造参数
   * */
  _createBillboardOptions(position) {
    // 获取要素symbol
    const _symbol = this._feature.symbol
    return {
      // 几何点坐标
      position,
      // billboard的图片url
      image: _symbol.url,
      // 设置图层透明度
      color: new Color(1, 1, 1, this._layerOpacity),
      // 图片宽度
      width: _symbol.width,
      // 图片高度
      height: _symbol.height,
      // 图片旋转角度
      rotation: Math.toRadians(_symbol.angle),
      // 图片原点偏移
      pixelOffset: new Cartesian2(_symbol.xoffset, _symbol.yoffset * -1)
    }
  }
}

export default PictureMarkerGraphic
