import { SymbolType, defined } from '@mapgis/webclient-common'
import CesiumPluginUtil from './CesiumPluginUtil'

// 转换symbol样式
function convertSimpleMarkerSymbolToFeatureStyle(symbol, style) {
  const symbolLayers = style.symbolLayers
  const material = symbolLayers.material
  const outline = symbolLayers.outline
  /**
   * convert
    const simpleMarkerSymbol = new SimpleMarkerSymbol({
      color: new Color(252, 100, 22, 1),
      angle: 90,
      xoffset: -1,
      yoffset: 5,
      size: 13,
      outline: new SimpleLineSymbol({
        color: new Color(255, 0, 0, 1),
        width: 2,
      }),
    });
    to
    * {
        type: 'polygon-3d',
        symbolLayers: {
            type: 'fill',
            material: { color: new Cesium.Color(0.0, 0.0, 1.0, 1.0) },
            outline: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), width: 1.0 },
            size: 20.0
        }
    }
   *
   */
  symbolLayers.type = 'fill'
  material['color'] = CesiumPluginUtil.convertColorToRgb(symbol.color)
  if (defined(symbol.outline)) {
    outline['color'] = CesiumPluginUtil.convertColorToRgb(symbol.outline.color)
    if (defined(symbol.outline.width)) {
      outline['width'] = symbol.outline.width
    }
  }
  if (defined(symbol.size)) {
    symbolLayers['size'] = parseFloat(symbol.size)
  }

  // 无法设置xoffset、yoffset
  // 无法支持angle
  // 无法支持path
  // 无法支持style
}

// 转换symbol样式
function convertPictureMarkerSymbolToFeatureStyle(symbol, style) {
  const symbolLayers = style.symbolLayers
  const material = symbolLayers.material
  const outline = symbolLayers.outline
  /**
   * convert
   * const pictureMarkerSymbol = new PictureMarkerSymbol({
          color: new Color(252, 100, 22, 1),
          angle: 90,
          xoffset: -1,
          yoffset: 5,
          height: 20,
          width: 30,
          url: "xxx",
          outline: new SimpleLineSymbol({
            color: new Color(255, 0, 0, 1),
            width: 2,
          }),
        });
   * to
   * {
      type: 'point-3d',
      symbolLayers: {
          type: 'icon',
          material: {color: new Cesium.Color(1.0, 1.0, 1.0, 1.0)},
          resource: {herf: "http://192.168.82.89:8895/img/Restaurant.svg", scale: 1.5, distanceDisplayCondition: new Cesium.DistanceDisplayCondition(10, 100000), disableDepthTestDistance: 10000},
          size: 20.0
      }
    }
   */
  // 无法设置xoffset、yoffset
  material['color'] = CesiumPluginUtil.convertColorToRgb(symbol.color)

  if (defined(symbol.height) && defined(symbol.width)) {
    // 暂时处理，需要修改cesium内核代码
    const maxSize = Math.max(
      parseFloat(symbol.height),
      parseFloat(symbol.width)
    )
    symbolLayers['size'] = maxSize
  }

  if (!symbolLayers.resource) {
    symbolLayers.resource = {}
  }
  const resource = symbolLayers.resource
  if (defined(symbol.url)) {
    resource.herf = symbol.url
  }

  if (defined(symbol.outline)) {
    outline['color'] = CesiumPluginUtil.convertColorToRgb(symbol.outline.color)
    if (defined(symbol.outline.width)) {
      outline['width'] = symbol.outline.width
    }
  }

  // 无法支持angle
  // 无法支持path
  // 无法支持style
}

function convertSimpleLineSymbolToFeatureStyle(symbol, style) {
  /**
   *     const simpleLineSymbol = new SimpleLineSymbol({
          color: new Color(255, 0, 0, 1),
          width: 2,
          cap: "round",
          join: "round",
          miterLimit: 2,
          style: "solid",
        });
   *
   */
  const symbolLayers = style.symbolLayers
  const material = symbolLayers.material
  if (defined(symbol)) {
    material['color'] = CesiumPluginUtil.convertColorToRgb(symbol.color)

    if (defined(symbol.width)) {
      symbolLayers['size'] = symbol.width
    }
  }
  // 不支持线首尾角
  // 不支持线接角
  // 不支持线style
  // 不支持斜接比例
}

function convertSimpleFillSymboltoFeatureStyle(symbol, style) {
  /**
        const simpleFillSymbol = new SimpleFillSymbol({
          color: new Color(255, 0, 0, 1),
          style: "solid",
          outline: new SimpleLineSymbol({
            color: new Color(255, 0, 0, 1),
            width: 2,
          }),
        });
   *
   */
  const symbolLayers = style.symbolLayers
  const material = symbolLayers.material
  const outline = symbolLayers.outline
  material['color'] = CesiumPluginUtil.convertColorToRgb(symbol.color)
  if (defined(symbol.outline)) {
    outline['color'] = CesiumPluginUtil.convertColorToRgb(symbol.outline.color)
    if (defined(symbol.outline.width)) {
      outline['width'] = symbol.outline.width
    }
  }
  // 无法设置style
}

class ConvertSymbolUtil {
  /**
   * @description 转换符号为要素样式
   * @public
   * @param {Symbol} symbol
   * @param {Symbol} def 默认服务
   * @return {Object} 样式结构，参考默认样式
   */
  static convertSymboltoFeatureStyle(symbol, def) {
    let style = {
      type: 'polygon-3d',
      symbolLayers: {
        type: 'fill',
        material: { color: new Cesium.Color(0.0, 0.0, 1.0, 1.0) },
        outline: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), width: 1.0 },
        size: 20.0
      }
    }

    if (def) {
      style = JSON.parse(JSON.stringify(def))
    }

    if (!symbol) return style
    switch (symbol.type) {
      case SymbolType.marker: {
        convertSimpleMarkerSymbolToFeatureStyle(symbol, style)
        break
      }
      case SymbolType.pictureMarker: {
        convertPictureMarkerSymbolToFeatureStyle(symbol, style)
        break
      }
      case SymbolType.line: {
        convertSimpleLineSymbolToFeatureStyle(symbol, style)
        break
      }
      case SymbolType.fill: {
        convertSimpleFillSymboltoFeatureStyle(symbol, style)
        break
      }
      default: {
        break
      }
    }
    return style
  }
}
export default ConvertSymbolUtil
