    import { SpatialReference, Point } from '../../../base/geometry'
    import { Log } from '../../../util'
    import LOD from './LOD'
    import TileInfo from './TileInfo'
    /**
     * @description: 瓦片解析工具
     * @return {*}
     */
    export default class TileInfoUtil {
      /**
       * @description: 解析igs1.0 tileInfo
       * @param {Object} tileInfo
       * @return {TileInfo}
       */
      static parseTileInfo1(tileInfo) {
        if (!tileInfo) {
          Log.error('tileInfo未定义')
        }
        const { tileWidth, tileHeight, lods, originX, originY } = tileInfo
        return new TileInfo({
          origin: new Point({
            coordinates: [originX, originY]
          }),
          lods: lods.map((lod) => {
            return LOD.fromJSON(lod)
          }),
          size: [tileWidth, tileHeight]
        })
        // igs1.0 tileInfo
        // {
        //   name: 'EPSG_4326_WORLD_TILE',
        //   type: 'tile',
        //   xMin: -180,
        //   xMax: 180,
        //   yMin: -90,
        //   yMax: 90,
        //   tileWidth: 256,
        //   tileHeight: 256,
        //   startLevel: 0,
        //   endLevel: 9,
        //   resolutions: [
        //     0.7031249999891483, 0.3515624999945742, 0.1757812499972871,
        //     0.08789062499864354, 0.04394531249932177, 0.021972656249660886,
        //     0.010986328124830443, 0.005493164062415209, 0.0027465820312076047,
        //     0.001373291015603805
        //   ],
        //   lods: [
        //     {
        //       level: 0,
        //       resolution: 0.7031249999891483,
        //       scale: 295829355.45,
        //       dxTileLogic: 179.99999999722198,
        //       dyTileLogic: 179.99999999722198,
        //       tileLogicRect: {
        //         xmin: -180,
        //         ymin: -89.99999999722198,
        //         xmax: 179.99999999444395,
        //         ymax: 90
        //       }
        //     },
        //     {
        //       level: 1,
        //       resolution: 0.3515624999945742,
        //       scale: 147914677.725,
        //       dxTileLogic: 89.99999999861099,
        //       dyTileLogic: 89.99999999861099,
        //       tileLogicRect: {
        //         xmin: -180,
        //         ymin: -89.99999999722198,
        //         xmax: 179.99999999444395,
        //         ymax: 90
        //       }
        //     }
        //   ],
        //   originX: -180,
        //   originY: 90,
        //   originType: 'LeftTop'
        // }
      }

      /**
       * @description: 解析igs2.0 tileInfo
       * @param {Object} tileInfo
       * @return {TileInfo}
       */
      static parseTileInfo2(tileInfo) {
        if (!tileInfo) {
          Log.error('tileInfo未定义')
        }

        const { cols, rows, dpi, format, origin, lods, spatialReference } = tileInfo

        return new TileInfo({
          dpi,
          format,
          origin: new Point({
            coordinates: [origin.x, origin.y],
            spatialReference: new SpatialReference(spatialReference)
          }),
          lods: lods.map((lod) => {
            return LOD.fromJSON(lod)
          }),
          size: [cols, rows],
          spatialReference: new SpatialReference(spatialReference)
        })
        // igs2.0 tileInfo
        //  {
        //   endLevel: 14,
        //   startLevel: 0,
        //   cols: 256,
        //   rows: 256,
        //   compressionQuality: null,
        //   dpi: 0,
        //   format: 'PNG',
        //   origin: {
        //     x: -5123200,
        //     y: 10002100
        //   },
        //   lods: [
        //     {
        //       level: 0,
        //       resolution: 156367.47632692187,
        //       scale: 590995186.1175
        //     },
        //     {
        //       level: 1,
        //       resolution: 78183.73816346093,
        //       scale: 295497593.05875
        //     }
        //   ],
        //   spatialReference: {
        //     wkid: 4547,
        //     name: '高斯大地坐标系_中国2000_38带3_北'
        //   }
        // }
      }

      /**
       * @description: 解析arcgis tileInfo
       * @param {Object} tileInfo
       * @return {TileInfo}
       */
      static parseArcgisTileInfo(tileInfo) {
        if (!tileInfo) {
          Log.error('tileInfo未定义')
        }
        const { cols, rows, dpi, format, origin, lods, spatialReference } = tileInfo
        return new TileInfo({
          dpi,
          format,
          origin: new Point({
            coordinates: [origin.x, origin.y],
            spatialReference: new SpatialReference({
              wkid: spatialReference.latestWkid || spatialReference.wkid
            })
          }),
          lods: lods.map((lod) => {
            return LOD.fromJSON(lod)
          }),
          size: [cols, rows],
          spatialReference: new SpatialReference({
            wkid: spatialReference.latestWkid || spatialReference.wkid
          })
        })
        //  arcgis tileInfo
        // {
        //   rows: 256,
        //   cols: 256,
        //   dpi: 96,
        //   format: 'JPEG',
        //   compressionQuality: 90,
        //   origin: {
        //     x: -20037508.342787,
        //     y: 20037508.342787
        //   },
        //   spatialReference: {
        //     wkid: 102100,
        //     latestWkid: 3857
        //   },
        //   lods: [
        //     {
        //       level: 0,
        //       resolution: 156543.03392800014,
        //       scale: 591657527.591555
        //     },
        //     {
        //       level: 1,
        //       resolution: 78271.51696399994,
        //       scale: 295828763.795777
        //     }
        //   ]
        // }
      }

      /**
       * @description: WMS tileInfo
       * @param {WMSLayer} layer
       * @return {TileInfo}
       */
      static parseWMSTileInfo(layer) {
        if (!layer) {
          Log.error('wmsLayer未定义')
        }
        // 处理wms 图层
        const extent = layer.extent
        const tileSize = Math.max(layer.imageWidth, layer.imageHeight)
        const resolution0 =
          Math.max(extent.ymax - extent.ymin, extent.xmax - extent.xmin) / tileSize
        const resolutions = []
        resolutions.push(resolution0)
        for (let r = 0; r < 20; r++) {
          resolutions.push(resolutions[r] / 2)
        }

        const origin = new Point({
          coordinates: [extent.xmin, extent.ymax],
          spatialReference: layer.spatialReferences[0]
        })

        return new TileInfo({
          origin,
          lods: resolutions.map((resolution, index) => {
            return LOD.fromJSON({
              level: index,
              resolution,
              scale: (resolution * 96) / 0.0254
            })
          }),
          size: [layer.imageWidth, layer.imageHeight],
          spatialReference: layer.spatialReferences[0]
        })
      }

      /**
       * @description: MapImage tileInfo
       * @param {IGSMapImageLayer} layer
       * @return {TileInfo}
       */
      static parseMapImageTileInfo(layer) {
        if (!layer) {
          Log.error('IGSMapImageLayer未定义')
        }
        // 处理wms 图层
        const extent = layer.extent
        const tileSize = Math.max(layer.imageWidth, layer.imageHeight)
        const resolution0 =
          Math.max(extent.ymax - extent.ymin, extent.xmax - extent.xmin) / tileSize
        const resolutions = []
        resolutions.push(resolution0)
        for (let r = 0; r < 20; r++) {
          resolutions.push(resolutions[r] / 2)
        }

        const origin = new Point({
          coordinates: [extent.xmin, extent.ymax],
          spatialReference: layer.spatialReference
        })
        return new TileInfo({
          origin,
          lods: resolutions.map((resolution, index) => {
            return LOD.fromJSON({
              level: index,
              resolution,
              scale: (resolution * 96) / 0.0254
            })
          }),
          size: [layer.imageWidth, layer.imageHeight],
          spatialReference: layer.spatialReference
        })
      }

      /**
       * @description: WMTS tileInfo
       * @param {WMTSLayer} layer
       * @return {TileInfo}
       */
      static parseWMTSTileInfo(layer) {
        if (!layer) {
          Log.error('wmtsLayer未定义')
        }

        // 处理wmts 图层
        const tileMatrixSets = layer.tileMatrixSets
        const tileMatrixSet = tileMatrixSets[0]
        const resolutions = tileMatrixSet.getTileResolution()
        const originArr = tileMatrixSet.tileMatrix[0].topLeftCorner.split(' ')
        const origin = new Point({
          coordinates: [Number(originArr[1]), Number(originArr[0])],
          spatialReference: SpatialReference.fromJSON(
            layer.spatialReference.toJSON()
          )
        })
        return new TileInfo({
          origin,
          lods: tileMatrixSet.tileMatrix.map((tm, index) => {
            return LOD.fromJSON({
              level: index,
              resolution: resolutions[index],
              scale: parseFloat(tm.scaleDenominator)
            })
          }),
          size: [layer.imageWidth, layer.imageHeight],
          spatialReference: SpatialReference.fromJSON(
            layer.spatialReference.toJSON()
          )
        })
      }
    }