import { coordinateConfig } from '../utils/Params'

/**
 * @class MapCoordTrans
 * @classdesc 坐标转换
 */
export class MapCoordTrans {
  /**
   * 百度转高德
   * @param {number[][]} ptlist 坐标数组，[[x1,y1],[x2,y2]...]
   * @returns 转换后坐标数组
   */
  baiduToGaode(ptlist) {
    let gList = []
    for (let i = 0, len = ptlist.length; i < len; i++) {
      let blon = ptlist[i][0]
      let blat = ptlist[i][1]
      blon = +blon
      blat = +blat
      let x = blon - 0.0065
      let y = blat - 0.006
      let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * coordinateConfig.xPI)
      let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * coordinateConfig.xPI)
      let glng = z * Math.cos(theta)
      let glat = z * Math.sin(theta)
      gList.push([glng, glat])
    }
    return gList
  }
  /**
   * 百度转WGS84
   * @param {number[][]} ptlist 坐标数组，[[x1,y1],[x2,y2]...]
   * @returns 转换后坐标数组
   */
  baiduToWGS84(ptlist) {
    let gdList = this.baiduToGaode(ptlist)
    return this.gaodeToWGS84(gdList)
  }
  /**
   * 高德转WGS84
   * @param {number[][]} ptlist 坐标数组，[[x1,y1],[x2,y2]...]
   * @returns 转换后坐标数组
   */
  gaodeToWGS84(ptlist) {
    let gList = []
    for (let i = 0, len = ptlist.length; i < len; i++) {
      let glon = ptlist[i][0]
      let glat = ptlist[i][1]

      glat = +glat
      glon = +glon
      if (this.outofChina(glon, glat)) {
        gList.push([glon, glat])
      } else {
        let dlat = this.transformLat(glon - 105.0, glat - 35.0)
        let dlng = this.transformlng(glon - 105.0, glat - 35.0)
        let radlat = (glat / 180.0) * coordinateConfig.PI
        let magic = Math.sin(radlat)
        magic = 1 - coordinateConfig.ee * magic * magic
        let sqrtmagic = Math.sqrt(magic)
        dlat =
          (dlat * 180.0) /
          (((coordinateConfig.a * (1 - coordinateConfig.ee)) / (magic * sqrtmagic)) * coordinateConfig.PI)
        dlng = (dlng * 180.0) / ((coordinateConfig.a / sqrtmagic) * Math.cos(radlat) * coordinateConfig.PI)
        let mglat = glat + dlat
        let mglng = glon + dlng
        gList.push([glon * 2 - mglng, glat * 2 - mglat])
      }
    }
    return gList
  }
  /**
   * 高德转百度
   * @param {number[][]} ptlist 坐标数组，[[x1,y1],[x2,y2]...]
   * @returns 转换后坐标数组
   */
  gaodeToBaidu(ptlist) {
    let gList = []
    for (let i = 0, len = ptlist.length; i < len; i++) {
      let glon = ptlist[i][0]
      let glat = ptlist[i][1]

      glat = +glat
      glon = +glon
      let z = Math.sqrt(glon * glon + glat * glat) + 0.00002 * Math.sin(glat * coordinateConfig.xPI)
      let theta = Math.atan2(glat, glon) + 0.000003 * Math.cos(glon * coordinateConfig.xPI)
      let blng = z * Math.cos(theta) + 0.0065
      let blat = z * Math.sin(theta) + 0.006
      gList.push([blng, blat])
    }
    return gList
  }
  /**
   * WGS84转高德
   * @param {number[][]} ptlist 坐标数组，[[x1,y1],[x2,y2]...]
   * @returns 转换后坐标数组
   */
  wgs84ToGaode(ptlist) {
    let gList = []
    for (let i = 0, len = ptlist.length; i < len; i++) {
      let wgslon = ptlist[i][0]
      let wgslat = ptlist[i][1]

      wgslat = +wgslat
      wgslon = +wgslon
      if (this.outofChina(wgslon, wgslat)) {
        gList.push([wgslon, wgslon])
      } else {
        let dlat = this.transformLat(wgslon - 105.0, wgslat - 35.0)
        let dlng = this.transformlng(wgslon - 105.0, wgslat - 35.0)
        let radlat = (wgslat / 180.0) * coordinateConfig.PI
        let magic = Math.sin(radlat)
        magic = 1 - coordinateConfig.ee * magic * magic
        let sqrtmagic = Math.sqrt(magic)
        dlat =
          (dlat * 180.0) /
          (((coordinateConfig.a * (1 - coordinateConfig.ee)) / (magic * sqrtmagic)) * coordinateConfig.PI)
        dlng = (dlng * 180.0) / ((coordinateConfig.a / sqrtmagic) * Math.cos(radlat) * coordinateConfig.PI)
        let mglat = wgslat + dlat
        let mglng = wgslon + dlng
        gList.push([mglng, mglat])
      }
    }
    return gList
  }
  /**
   * WGS84转百度
   * @param {array} ptlist 坐标数组，[[x1,y1],[x2,y2]...]
   * @returns 转换后坐标数组
   */
  wgs84ToBaidu(ptlist) {
    let gList = this.wgs84ToGaode(ptlist)
    return this.gaodeToBaidu(gList)
  }
  /**
   * WGS84转WEB墨卡托
   * @param {array} pt 坐标，[x1,y1]
   * @returns 转换后坐标
   */
  lonLatToMercator(pt) {
    let x = (pt[0] * 20037508.34) / 180
    let y = Math.log(Math.tan(((90 + pt[1]) * Math.PI) / 360)) / (Math.PI / 180)
    y = (y * 20037508.34) / 180

    return [x, y]
  }
  /**
   * WEB墨卡托转WGS84
   * @param {array} pt 坐标，[x1,y1]
   * @returns 转换后坐标
   */
  mercatorToLonlat(pt) {
    var lon = (pt[0] / 20037508.34) * 180
    var lat = (pt[1] / 20037508.34) * 180
    lat = (180 / Math.PI) * (2 * Math.atan(Math.exp((lat * Math.PI) / 180)) - Math.PI / 2)
    return [lon, lat]
  }
  outofChina(lng, lat) {
    lat = +lat
    lng = +lng
    // 纬度[3.86,53.55],经度[73.66,135.05]
    return !(lng > 73.66 && lng < 135.05 && lat > 3.86 && lat < 53.55)
  }
  transformLat(lng, lat) {
    lat = +lat
    lng = +lng
    let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng))
    ret +=
      ((20.0 * Math.sin(6.0 * lng * coordinateConfig.PI) + 20.0 * Math.sin(2.0 * lng * coordinateConfig.PI)) * 2.0) /
      3.0
    ret +=
      ((20.0 * Math.sin(lat * coordinateConfig.PI) + 40.0 * Math.sin((lat / 3.0) * coordinateConfig.PI)) * 2.0) / 3.0
    ret +=
      ((160.0 * Math.sin((lat / 12.0) * coordinateConfig.PI) + 320 * Math.sin((lat * coordinateConfig.PI) / 30.0)) *
        2.0) /
      3.0
    return ret
  }

  transformlng(lng, lat) {
    lat = +lat
    lng = +lng
    let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng))
    ret +=
      ((20.0 * Math.sin(6.0 * lng * coordinateConfig.PI) + 20.0 * Math.sin(2.0 * lng * coordinateConfig.PI)) * 2.0) /
      3.0
    ret +=
      ((20.0 * Math.sin(lng * coordinateConfig.PI) + 40.0 * Math.sin((lng / 3.0) * coordinateConfig.PI)) * 2.0) / 3.0
    ret +=
      ((150.0 * Math.sin((lng / 12.0) * coordinateConfig.PI) + 300.0 * Math.sin((lng / 30.0) * coordinateConfig.PI)) *
        2.0) /
      3.0
    return ret
  }
  wgs84ToshCus(lon, lat) {
    return [
      -11583561.4655538 + lon * 95356.0541247944 + lat * 29.8337748880993,
      -3471402.36568149 + lon * 74.8848737309186 + lat * 110845.418549433
    ]
  }
}
