export default class ArrowHelper {
    constructor(start, end) {
        this.start = start
        this.end = end
    }

    getStartArrow(angle, length, closed) {
        let arrowPoints = this.getStartArrowPoints(angle, length)
        if (arrowPoints) {
            let svgArrow = undefined
            if (closed) {
                svgArrow = document.createElementNS('http://www.w3.org/2000/svg', 'polygon')
            }
            else {
                svgArrow = document.createElementNS('http://www.w3.org/2000/svg', 'polyline')
            }

            let points = ''
            arrowPoints.forEach(element => {
                if (points.length > 0) {
                    points = points + ' '
                }
                points = points + `${element.x},${element.y}`
            });
            if (points.length > 0) {
                svgArrow.setAttribute('points', points)
                return svgArrow
            }
        }
        return undefined
    }

    getStartArrowPoints(angle, length) {
        let unitVector = this.#getNormalize()
        if (unitVector && length > 0) {
            unitVector={ x:unitVector.x*length,y:unitVector.y*length}
            //旋转
            //逆时针时θ为正数， 顺时针是θ为负数
            //(x*cosθ- y * sinθ, y*cosθ + x * sinθ)
            let clockWise = {
                x: unitVector.x * Math.cos(angle*Math.PI/180) - unitVector.y * Math.sin(angle*Math.PI/180),
                y: unitVector.y * Math.cos(angle*Math.PI/180) + unitVector.x * Math.sin(angle*Math.PI/180)
            }
            let antiClockWise = {
                x: unitVector.x * Math.cos(-angle*Math.PI/180) - unitVector.y * Math.sin(-angle*Math.PI/180),
                y: unitVector.y * Math.cos(-angle*Math.PI/180) + unitVector.x * Math.sin(-angle*Math.PI/180)
            }

            let arrowClockWise = { x: this.start.x + clockWise.x , y: this.start.y + clockWise.y }
            let arrowStart = { x: this.start.x, y: this.start.y }
            let arrowAntiClockWise = { x: this.start.x + antiClockWise.x, y: this.start.y + antiClockWise.y }
           
            return [arrowClockWise,arrowStart,arrowAntiClockWise]
        }
        return undefined
    }

    getEndArrow(angle,length,closed)
    {
        let arrowPoints = this.getEndArrowPoints(angle, length)
        if (arrowPoints) {
            let svgArrow = undefined
            if (closed) {
                svgArrow = document.createElementNS('http://www.w3.org/2000/svg', 'polygon')
            }
            else {
                svgArrow = document.createElementNS('http://www.w3.org/2000/svg', 'polyline')
            }

            let points = ''
            arrowPoints.forEach(element => {
                if (points.length > 0) {
                    points = points + ' '
                }
                points = points + `${element.x},${element.y}`
            });
            if (points.length > 0) {
                svgArrow.setAttribute('points', points)
                return svgArrow
            }
        }
        return undefined
    }

    getEndArrowPoints(angle, length) {
        let unitVector = this.#getNormalize()
        if (unitVector && length > 0) {
            unitVector={ x:unitVector.x*length,y:unitVector.y*length}
            //旋转
            //逆时针时θ为正数， 顺时针是θ为负数
            //(x*cosθ- y * sinθ, y*cosθ + x * sinθ)
            let clockWise = {
                x: unitVector.x * Math.cos(angle*Math.PI/180) - unitVector.y * Math.sin(angle*Math.PI/180),
                y: unitVector.y * Math.cos(angle*Math.PI/180) + unitVector.x * Math.sin(angle*Math.PI/180)
            }
            let antiClockWise = {
                x: unitVector.x * Math.cos(-angle*Math.PI/180) - unitVector.y * Math.sin(-angle*Math.PI/180),
                y: unitVector.y * Math.cos(-angle*Math.PI/180) + unitVector.x * Math.sin(-angle*Math.PI/180)
            }

            let arrowClockWise = { x: this.end.x - clockWise.x , y: this.end.y - clockWise.y }
            let arrowStart = { x: this.end.x, y: this.end.y }
            let arrowAntiClockWise = { x: this.end.x - antiClockWise.x, y: this.end.y - antiClockWise.y }
            return [arrowClockWise,arrowStart,arrowAntiClockWise]
        }
        return undefined
    }

    #getNormalize()
    {
       let lineVector=this.#getVector()
       let lineLength=this.#getLineLength()
       if(lineVector && lineLength>0)
       {
         return {
            x:lineVector.x/lineLength,
            y:lineVector.y/lineLength
         }
       }

        return undefined
    }
    
    #getVector()
    {
        if (this.start && this.end) {
            return {
                x: this.end.x - this.start.x,
                y: this.end.y - this.start.y
            }
        }
        return undefined
    }
    
    #getLineLength()
    {
        let lineVector=this.#getVector()
        if(lineVector)
        {
            return Math.sqrt(Math.pow(lineVector.x,2)+Math.pow(lineVector.y,2))
        }
        return 0
    }
}