export default {
    tempEntities: [],
    pointNum: 0,
    floatingPoint: undefined,
    activeShape: undefined,
    viewer:null,
    /**
     * 根据类型绘制对象
     * @param {} type 绘制类型
     * @param {*} view cesium view
     */
    draw(type, viewer) {
        this.viewer=viewer
        let position = []
        let tempPoints = []
        let activeShapePoints = []
        //创建场景的canvas元素
        let handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
        switch(type){
            //绘制线
            case 'Polyline':
                //监听鼠标移动
                handler.setInputAction(function(movement){
                    if(Cesium.defined(this.floatingPoint)){
                        let newPosition=this.viewer.scene.pickPosition(movement.endPosition)
                        if(Cesium.defined(newPosition)){
                           this.floatingPoint.position.setValue(newPosition)
                            activeShapePoints.pop()
                            activeShapePoints.push(newPosition)
                        }
                    }
                },Cesium.ScreenSpaceEventType.MOUSE_MOVE)
                //左键单击开始画线
                handler.setInputAction(function(click){
                    let earthPosition=viewer.scene.pickPosition(click.position)
                    if(Cesium.defined(earthPosition)){
                       this.floatingPoint=this.drawPoint(earthPosition)
                     
                    }
                  
                    //获取位置信息
                    //从相机位置创建一条射线,这条射线通过世界中movement.position像素所在的坐标,返回Cartesian3坐标
                    let ray=this.viewer.camera.getPickRay(click.position)
                    //找到射线与渲染的地球表面之间的交点,射线必须以世界坐标给出,返回Cartesian3坐标
                    position=this.viewer.scene.globe.pick(ray,this.viewer.scene)
                    tempPoints.push(position)//记录点位
                    this.pointNum+=1
                    let tempLength=tempPoints.length//记录点数
                    //调用绘制点的接口
                    let point=this.drawPointLabel(tempPoints[tempPoints.length-1],JSON.stringify(this.pointNum))
                    tempEntities.push(point)
                    //存在超过一个点的时候
                    if(tempLength>1){
                        //绘制线
                        let pointline=this.drawPolyline([temPoints[tempPoints.length-2],tempPoints[tempPoints.length-1]])
                        tempEntities.push(pointline)//保存记录
                    }
                })
        }
    },
    //绘制点
    drawPoint(position){
        //本质上就是添加一个点的实体
        return this.viewer.entities.add({
            position:position,
            point:{
                color:Cesium.Color.WHEAT,
                pixelSize:5,
                outlineWidth:3,
                disableDepthTestDistance:Number.POSITIVE_INFINITY,
                heightReference:Cesium.HeightReference.CLAMP_TO_GROUND//规定贴地
            }
        })
    },
    //绘制面几何对象
    drawPolygon(positions){
        if(positions.length<2) return
        return this.viewer.entities.add({
            name:'面几何对象',
            polygon:{
                hierarchy:positions,
                material:new Cesium.ColorMaterialProperty(
                    Cesium.Color.WHEAT.withAlpha(0.4)
                )
            }
        })
    },
    drawPointLabel(position,pointNum){
        //本质上就是添加一个点的实体
        return this.viewer.entities.add({
            name:'点几何对象',
            position:position,
            point:{
                color:Cesium.Color.WHEAT,
                pixelSize:5,
                outlineWidth:3,
                disableDepthTestDistance:Number.POSITIVE_INFINITY,
                heightReference:Cesium.HeightReference.CLAMP_TO_GROUND//规定贴地
            },
            label:{
                text:pointNum,
                font:'30px sans-serif',
                fillColor:Cesium.Color.WHITE,
                outlineWidth:2,
                backgroundColor:Cesium.Color.BLACK,
                showBackground:true,
                style:Cesium.LabelStyle.FILL,
                verticalOrigin:Cesium.VerticalOrigin.BOTTOM,
                horizontalOrigin:Cesium.HorizontalOrigin.CENTER
            }
        })


    },
    drawPolyline(positions){
        if(positions.length<1) return
        return this.viewer.entities.add({
            name:'线几何对象',
            polyline:{
                position:positions,
                width:5.0,
                material:new Cesium.PolylineGlowMaterialProperty({
                    color:Cesium.Color.WHITE
                }),
                depthFailMaterial:new Cesium.PolylineGlowMaterialProperty({
                    
                })
            }
        })
    }


}