import VectorType from '../enum/VectorType.js'
import Geometry from './Geometry'
import { mathUtil } from '../MathUtil.js'
import SelectState from '../enum/SelectState.js'
import { coordinate } from '../Coordinate'

export default class CustomImage extends Geometry {
    constructor(url,center,vectorId) {
        super()
        this.center = center   //实际上是左下角
        this.url = url;
        this.image = null;
        this.width = 40;
        this.height = 30;
        this.angle = 0 //逆时针为负，顺时针为正。单位是：°
        this.scale = 1 //缩放比例
        this.ratio = 1;
        this.points = [];
        this.geoType = VectorType.CustomImage
        this.setId(vectorId)
    }

    isContain(position) {
        let p0 = {
            x:this.center.x - this.ratio*this.width/coordinate.res*this.scale,
            y:this.center.y + this.ratio*this.height/coordinate.res*this.scale
        }

        let p1 = {
            x:this.center.x,
            y:this.center.y + this.ratio*this.height/coordinate.res*this.scale
        }

        let p2 = {
            x:this.center.x,
            y:this.center.y
        }

        let p3 = {
            x:this.center.x- this.ratio*this.width/coordinate.res*this.scale,
            y:this.center.y
        }
        let center = {
            x:this.center.x - this.ratio*this.width/2/coordinate.res*this.scale,
            y:this.center.y + this.ratio*this.height/2/coordinate.res*this.scale
        }

        p0 = this.rotatePoint(p0, center, this.angle)
        p1 = this.rotatePoint(p1, center, this.angle)
        p2 = this.rotatePoint(p2, center, this.angle)
        p3 = this.rotatePoint(p3, center, this.angle)

        //let points = [];
        this.points = [];
        this.points.push(p0)
        this.points.push(p3)
        this.points.push(p2)
        this.points.push(p1)
        if(mathUtil.isPointInPoly(position, this.points)){
            return SelectState.Select
        }else {
            return null
        }
    }

    // ptSrc: 圆上某点(初始点);
    // ptRotationCenter: 圆心点;
    // angle: 旋转角度°  -- [angle * M_PI / 180]:将角度换算为弧度
    // 【注意】angle 逆时针为正，顺时针为负
    rotatePoint(ptSrc, ptRotationCenter, angle) {
        angle = -1 * angle //设计是逆时针为负，顺时针为正
        var a = ptRotationCenter.x
        var b = ptRotationCenter.y
        var x0 = ptSrc.x
        var y0 = ptSrc.y
        var rx = a + (x0 - a) * Math.cos((angle * Math.PI) / 180) - (y0 - b) * Math.sin((angle * Math.PI) / 180)
        var ry = b + (x0 - a) * Math.sin((angle * Math.PI) / 180) + (y0 - b) * Math.cos((angle * Math.PI) / 180)
        var json = { x: rx, y: ry }
        return json
    }

    setImageData(imgData){
        this.image = imgData;
    }

    setUrl(url){
        this.url = url;
    }

    setAngle(angle){
        this.angle = angle;
    }

    setScale(scale){
        this.scale = scale;
    }

    setRatio(ratio){
        this.ratio = ratio;
    }
    
}