export const CesiumToSuperMap = {
  convertPoint: function (Cesium, SuperMap, point) {
    if (!Cesium || !SuperMap || !point) {
      return undefined;
    }
    let lonlatPoint = Cesium.Cartographic.fromCartesian(point);
    let x = Cesium.Math.toDegrees(lonlatPoint.longitude);
    let y = Cesium.Math.toDegrees(lonlatPoint.latitude);
    if (x && y) {
      return new SuperMap.Geometry.Point(x, y);
    }
    return undefined;
  },
  convertPolyline: function (Cesium, SuperMap, polyline) {
    if (!Cesium || !SuperMap || !polyline) {
      throw undefined;
    }
    let points = polyline.positions;
    if (points && points instanceof Array && points.length >= 2) {
      let arr = [];
      for (let i = 0, j = points.length; i < j; i++) {
        let point = this.convertPoint(Cesium, SuperMap, points[i]);
        if (point) {
          arr.push(point);
        }
      }
      return new SuperMap.Geometry.LineString(arr);
    }
    return undefined;
  },
  convertPolygon: function (Cesium, SuperMap, polygon) {
    if (!Cesium || !SuperMap || !polygon) {
      throw undefined;
    }
    let points = polygon.positions;
    if (points && points instanceof Array && points.length >= 3) {
      let arr = [];
      for (let i = 0, j = points.length; i < j; i++) {
        let point = this.convertPoint(Cesium, SuperMap, points[i]);
        if (point) {
          arr.push(point);
        }
      }
      let linearRing = new SuperMap.Geometry.LinearRing(arr);
      return new SuperMap.Geometry.Polygon(linearRing);
    }
    return undefined;
  },
  convertDirPolygon: function (Cesium, SuperMap, polygon) {
    if (!Cesium || !SuperMap || !polygon) {
      throw undefined;
    }
    let points = polygon.positions;
    if (points && points instanceof Array && points.length >= 3) {
      let arr = [];
      for (let i = 0, j = points.length; i < j; i++) {
        let point = new SuperMap.Geometry.Point(points[i][0], points[i][1]);
        if (point) {
          arr.push(point);
        }
      }
      let linearRing = new SuperMap.Geometry.LinearRing(arr);
      return new SuperMap.Geometry.Polygon(linearRing);
    }
    return undefined;
  }
};

export const SuperMapToCesium = {
  geometryToEntity: function (Cesium, SuperMap, geometry) {
    let className = geometry.CLASS_NAME;
    if (className == 'SuperMap.Geometry.MultiPolygon') {
      return this.multipolygonToEntities(Cesium, SuperMap, geometry);
    } else if (className == 'SuperMap.Geometry.Polygon') {
      return [this.polygonToEntity(Cesium, SuperMap, geometry)];
    }
    return undefined;
  },
  polygonToEntity: function (Cesium, SuperMap, geometry) {
    let hierarchy = {};
    for (let i = 0, j = geometry.components.length; i < j; i++) {
      if (i == 0) {
        hierarchy.positions = this.GeometrytoDegreesArray(Cesium, SuperMap, geometry.components[
          i]);
      } else {
        if (!hierarchy.holes) {
          hierarchy.holes = [];
        }
        hierarchy.holes.push({
          positions: this.GeometrytoDegreesArray(Cesium, SuperMap, geometry.components[i])
        });
      }
    }
    return new Cesium.Entity({
      polygon: {
        hierarchy: hierarchy,
        material: Cesium.Color.BLUE.withAlpha(0.5)
      },
      depthTestEnabled: false
    });

  },
  multipolygonToEntities: function (Cesium, SuperMap, geometry) {
    let components = geometry.components;
    let resultEntities = [];
    let entity;
    for (let item in components) {
      entity = this.polygonToEntity(Cesium, SuperMap, components[item]);
      resultEntities.push(entity);
    }
    return resultEntities;
  },
  GeometrytoDegreesArray: function (Cesium, SuperMap, geometry) {
    let vertices = geometry.getVertices();
    let degreesArr = [];
    for (let o in vertices) {
      degreesArr.push(vertices[o].x, vertices[o].y);
    }
    return Cesium.Cartesian3.fromDegreesArray(degreesArr);
  }
};

//转换投影
export const PrjCesiumToSuperMap = {
  convertPoint: function (Cesium, SuperMap, point) {
    if (!Cesium || !SuperMap || !point) {
      return undefined;
    }
    let lonlatPoint = Cesium.Cartographic.fromCartesian(point);
    let pt = new SuperMap.LonLat(Cesium.Math.toDegrees(lonlatPoint.longitude), Cesium.Math
      .toDegrees(lonlatPoint.latitude));
    const CRS_4544 = new L.Proj.CRS('EPSG:4544', {
      def: '+proj=tmerc +lat_0=0 +lon_0=105 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'
    });
    let center_latLng = L.latLng(pt.lat, pt.lon);
    let GcsObj = CRS_4544.project(center_latLng);
    let x = GcsObj.x;
    let y = GcsObj.y;

    if (x && y) {
      return new SuperMap.Geometry.Point(x, y);
    }
    return undefined;
  },
  convertPolyline: function (Cesium, SuperMap, polyline) {
    if (!Cesium || !SuperMap || !polyline) {
      throw undefined;
    }
    let points = polyline.positions;
    if (points && points instanceof Array && points.length >= 2) {
      let arr = [];
      for (let i = 0, j = points.length; i < j; i++) {
        let point = this.convertPoint(Cesium, SuperMap, points[i]);
        if (point) {
          arr.push(point);
        }
      }
      return new SuperMap.Geometry.LineString(arr);
    }
    return undefined;
  },
  convertPolygon: function (Cesium, SuperMap, polygon) {
    if (!Cesium || !SuperMap || !polygon) {
      throw undefined;
    }
    let points = polygon.positions;
    if (points && points instanceof Array && points.length >= 3) {
      let arr = [];
      for (let i = 0, j = points.length; i < j; i++) {
        let point = this.convertPoint(Cesium, SuperMap, points[i]);
        if (point) {
          arr.push(point);
        }
      }
      let linearRing = new SuperMap.Geometry.LinearRing(arr);
      return new SuperMap.Geometry.Polygon(linearRing);
    }
    return undefined;
  },
  wgsToGcs: function (Cesium, SuperMap, LonLat) {
    if (!Cesium || !SuperMap || LonLat.length != 2) {
      return undefined;
    }
    const CRS_4544 = new L.Proj.CRS('EPSG:4544', {
      def: '+proj=tmerc +lat_0=0 +lon_0=105 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'
    });
    let center_latLng = L.latLng(LonLat[1], LonLat[0]);
    let GcsObj = CRS_4544.project(center_latLng);
    let x = GcsObj.x;
    let y = GcsObj.y;
    if (!isNaN(x) && !isNaN(y)) {
      return [x, y];
    }
    return undefined;
  },
  wgsToGcsXY: function (Cesium, SuperMap, LonLat) {
    if (!Cesium || !SuperMap || LonLat.length != 2) {
      return undefined;
    }
    const CRS_4544 = new L.Proj.CRS('EPSG:4544', {
      def: '+proj=tmerc +lat_0=0 +lon_0=105 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'
    });
    let center_latLng = L.latLng(LonLat[1], LonLat[0]);
    return CRS_4544.project(center_latLng);
  },
  gcsToWgs: function (Cesium, SuperMap, LonLat) {
    if (!Cesium || !SuperMap || LonLat.length != 2) {
      return undefined;
    }
    const CRS_4544 = new L.Proj.CRS('EPSG:4544', {
      def: '+proj=tmerc +lat_0=0 +lon_0=105 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'
    });
    let point_old = new L.Point(LonLat[0], LonLat[1]);
    let point_new = CRS_4544.unproject(point_old);
    let x = point_new.lng;
    let y = point_new.lat;
    if (x && y) {
      return [x, y];
    }
    return undefined;
  }
};
