import Constant from './Constant'

// const pad = {
//     top: 40,
//     bottom: 40,
//     left: 40,
//     right: 40,
// }

export default class Coordinate {
    constructor() {
        this.center = {
            x:0,
            y:0
        } // 世界坐标，中心点
        this.zoom = 100 // 当前缩放比例，不会改变世界坐标系的坐标，只是改变渲染时转换的屏幕坐标
        this.res = 100 //默认情况，100个像素对应1米
        this.ratio = 1

        this.initRes = null
        this.initWidth = null
        this.initHeight = null
    }

    // 初始化
    init(canvas) {
        this.updateForCanvas(canvas)
    }

    setRes(res) {
        this.res = res
        Constant.minRealDis = Constant.minPixLen / this.res
        Constant.minAdsorb = Constant.minPixLen / this.res
    }

    setCenter(point) {
        this.center = {
            x: point.x,
            y: point.y,
        }
    }

    // 世界坐标转换成屏幕坐标
    getScreenXY(point) {
        if (this.width == null || this.height == null) {
            return null
        }

        let pt = {
            x: point.x,
            y: point.y,
        }

        let x = this.width / 2 + ((pt.x - this.center.x) * this.res * this.zoom) / 100 
        let y = this.height / 2 - ((pt.y - this.center.y) * this.res * this.zoom) / 100 

        x = (0.5 + x) << 0
        y = (0.5 + y) << 0
        return {
            x: Math.floor(x) * this.ratio,
            y: Math.floor(y) * this.ratio,
        }
    }

    // 屏幕坐标转换成世界坐标
    getXYFromScreen(screenPoint) {
        const point = {}
        point.x = (((screenPoint.x - this.width / 2) / this.res) * 100) / this.zoom + this.center.x
        point.y = (((this.height / 2 - screenPoint.y) / this.res) * 100) / this.zoom + this.center.y
        return point
    }

    updateForCanvas(canvas) {
        if (canvas) {
            this.width = canvas.clientWidth
            this.height = canvas.clientHeight

            canvas.width = canvas.clientWidth
            canvas.height = canvas.clientHeight
        }
    }

    updateZoom(zoom) {
        this.zoom = zoom
    }

    moveTo(zoom, screenPosition) {
        const position = this.getXYFromScreen(screenPosition)
        this.zoom = zoom
        const dx = ((screenPosition.x * 100) / this.zoom - this.width / 2) / this.res
        const dy = (this.height / 2 - (screenPosition.y * 100) / this.zoom) / this.res
        this.center.x = position.x - dx
        this.center.y = position.y - dy
    }

    getPixelRatio(context) {
        var backingStore =
            context.backingStorePixelRatio ||
            context.webkitBackingStorePixelRatio ||
            context.mozBackingStorePixelRatio ||
            context.msBackingStorePixelRatio ||
            context.oBackingStorePixelRatio ||
            context.backingStorePixelRatio ||
            1
        return (window.devicePixelRatio || 1) / backingStore
    }

    reSet() {
        this.center = {
            x: 0,
            y: 0,
        }
        this.res = 100
        this.zoom = 100
    }

    clear() {
        this.center = {
            x: 0,
            y: 0,
        }
        this.zoom = 100
        this.res = 100
        this.ratio = 1
    }
}

const coordinate = new Coordinate()
export { coordinate }
