import AppX from '@/config/configure'
// 通用的cesium 公共方法

const Cesium = (<any>window).Cesium

export default class CesiumUtil {
    viewer = null
    private static baseTerrain = 'terrain'
    private static terrain = 'dixing'

    constructor(viewer) {
        this.viewer = viewer
    }

    static getInitViewer (dom) {
        console.log('获取组件')
        var configItem = AppX.appConfig.gisResource[this.baseTerrain]
        var { url, name } = configItem.config[this.terrain]
        return new Cesium.Viewer(dom, {
            terrainProvider: new Cesium.CesiumTerrainProvider({
                url,
                requestWaterMask: true,
                requestVertexNormals: true,
                credit: name,
                isSct: true
            }),
            navigation: true,
            contextOptions: {
                webgl: {
                    alpha: false,
                    depth: true,
                    stencil: true,
                    antialias: true,
                    premultipliedAlpha: true,
                    preserveDrawingBuffer: true, //通过canvas.toDataURL()实现截图需要将该项设置为true
                    failIfMajorPerformanceCaveat: false
                },
                allowTextureFilterAnisotropic: true
            },
            timeline: false,
            animation: false,
            infoBox: false,
            selectionIndicator: false //设置绿色框框不可见
        })
    }

    getCenterFormFeas (coors) {
        if (coors.length === 0) return
        let xmin, xmax, ymin, ymax
        coors.forEach(line => {
            let [startPoint, endPoint] = line
            let [sx, sy] = startPoint
            let [ex, ey] = endPoint
            xmin = xmin ? Math.min.call(null, sx, ex, xmin) : Math.min.call(null, sx, ex)
            xmax = xmax ? Math.max.call(null, sx, ex, xmax) : Math.max.call(null, sx, ex)
            ymin = ymin ? Math.min.call(null, sy, ey, ymin) : Math.min.call(null, sy, ey)
            ymax = ymax ? Math.max.call(null, sy, ey, ymax) : Math.max.call(null, sy, ey)
        })
        return [(xmin + xmax) / 2, (ymin + ymax) / 2]
    }

    // 移动到地点
    flyTo(target) {
        this.viewer.flyTo(target, {
            offset: {
                heading: Cesium.Math.toRadians(0),
                pitch: Cesium.Math.toRadians(-90),
                range: 3000
            },
            duration: 1
        })
    }
    // 设置视角
    setView(car3, callback?, orientation = { heading: 0, pitch: -90, roll: 0 }) {
        this.viewer.camera.flyTo({
            destination: car3,
            orientation: {
                heading: Cesium.Math.toRadians(orientation.heading),
                pitch: Cesium.Math.toRadians(orientation.pitch),
                roll: Cesium.Math.toRadians(orientation.roll)
            },
            duration: 1,
            complete: callback
        })
    }
    // 起点
    goHome() {
        let { initPosition: { lon, lat, height } } = AppX.appConfig
        let car3 = CesiumUtil.degrees2Car3(parseFloat(lon), parseFloat(lat), parseFloat(height))
        this.setView(car3)
    }
    // 屏幕坐标转笛卡尔
    xy2Car3 (position) {
        return this.viewer.scene.camera.pickEllipsoid(position, this.viewer.scene.globe.ellipsoid)
    }
    // 经纬度转笛卡尔坐标
    static degrees2Car3(lon, lat, h) {
        return Cesium.Cartesian3.fromDegrees(lon, lat, h)
    }

    static degreesArray2Car3 (pointsArray) {
        return Cesium.Cartesian3.fromDegreesArray(pointsArray)
    }   
    // 笛卡尔坐标系转经纬度
    static car32Degrees(car3) {
        let radian = Cesium.Cartographic.fromCartesian(car3)
        return this.radain2Degrees(radian)
    }
    // 弧度转经纬度
    static radain2Degrees({ longitude, latitude, height }) {
        return { lon: Cesium.Math.toDegrees(longitude), lat: Cesium.Math.toDegrees(latitude), height }
    }
    // 笛卡尔坐标 转 屏幕坐标
    car32WindowPosition (car3) {
        return Cesium.SceneTransforms.wgs84ToWindowCoordinates(this.viewer.scene, car3)
    }
}