import testImg from '../img/test.png'
const Cesium = (<any>window).Cesium

export default class GraphicUtil {

    colorBox = {
        'red': Cesium.Color.RED,
        'green': Cesium.Color.GREEN,
        'blue': Cesium.Color.BLUE,
        'pink': Cesium.Color.PINK,
    }

    constructor() {

    }
    // entity 入口
    getEntity(type, positions, style: any = {}) {
        let entity = null
        switch (type) {
            case 'point': 
                let color = this.colorBox[style.color] || Cesium.Color.fromCssColorString(style.color)
                entity = this.createEPoint(positions, color, color)
                break
            case 'img': 
                entity = this.createImg(positions, style.img)
                break
            case 'line':
                let pcolor = this.colorBox[style.color] || Cesium.Color.fromCssColorString(style.color)
                entity = this.createELine(positions, pcolor)
            default:
        }
        return entity
    }
    // primity 入口
    getPrimity(type, positions, style: any = {}, data?) {
        let primitive = null
        switch (type) {
            case 'line':
                let lcolor = this.colorBox[style.color] || Cesium.Color.fromCssColorString(style.color)
                primitive = this.createPLine(positions, lcolor || Cesium.Color.RED, style.width, data)
                break
            case 'point':
                let pcolor = this.colorBox[style.color] || Cesium.Color.fromCssColorString(style.color)
                primitive = this.createPPoint(positions, pcolor)
                break
            default:
                return null
        }
        return primitive
    }

    // 实体点
    createEPoint (position, color?, outlineColor?) {
        return new Cesium.Entity({
            position,
            point: {
                color: color || Cesium.Color.RED,
                outlineColor: outlineColor || Cesium.Color.YELLOW,
                outlineWidth: 5,
                pixelSize: 10,
                show: true,
                heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND
            }
        })
    }

    createELine (positions, color) {
        return new Cesium.Entity({
            corridor: {
                positions,
                width: 3.5,
                material: color
            }
        })
    }
    // 实体图片广告牌
    createImg(position, img?) { 
        return new Cesium.Entity({
            position,
            billboard: { //图标
                image: img || testImg,
                width: 20,
                height: 20,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                rotation: 0,
                // sizeInMeters: true
            },
        })
    }
    // 图元 贴地线
    createPLine(positions, color = Cesium.Color.RED, width, data) {
        return new Cesium.GroundPolylinePrimitive({
            releaseGeometryInstances: false,
            geometryInstances: new Cesium.GeometryInstance({
                geometry: new Cesium.GroundPolylineGeometry({ positions, width: width || 5 }),
                id: data || 'id',
            }),
            appearance: new Cesium.PolylineMaterialAppearance({
                material: Cesium.Material.fromType('Color', { color })
            })
        })
    }
    // 贴地点
    createPPoint (center, color) {
        const instance = new Cesium.GeometryInstance({
            geometry: new Cesium.EllipseGeometry({
                center: center,
                semiMajorAxis: 1,
                semiMinorAxis: 1,
                height: 100.0
            }),
            attributes: {
                color: color ? Cesium.ColorGeometryInstanceAttribute.fromColor(color) : new Cesium.ColorGeometryInstanceAttribute(0.0, 1.0, 1.0, 0.5)
            },
            id: 'id'
        });
        const circle = new Cesium.GroundPrimitive({
            geometryInstances: instance,
            appearance : new Cesium.PerInstanceColorAppearance({
                flat : true,
                translucent : false
            }),
            classificationType: Cesium.ClassificationType.TERRAIN
        })
        return circle;
    }
    // 获取材质
    getMaterail (img) {
        return Cesium.Material.fromType(Cesium.Material.ImageType, {
            repeat: false,
            image: img
        })
    }
}