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

export default class Icon extends Geometry {
    constructor(position,radius, value,vectorId, floor) {
        super()
        this.center = position
        this.radius = radius
        this.value = value
        this.points = [];                       //顺时针
        this.angle = 0;
        this.setPoints()
        this.floor = floor?floor:0
        this.geoType = VectorType.Icon
        this.setId(vectorId)
    }

    setRadius(radius){
        this.radius = radius
    }

    setPoints(){
        this.points[0] = {
            x:this.center.x - this.radius,
            y:this.center.y + this.radius
        }
        this.points[1] = {
            x:this.center.x + this.radius,
            y:this.center.y + this.radius
        }
        this.points[2] = {
            x:this.center.x + this.radius,
            y:this.center.y - this.radius
        }
        this.points[3] = {
            x:this.center.x - this.radius,
            y:this.center.y - this.radius
        }
    }

    isContain(position){
        for(let i=0;i<this.points.length;++i){
            const dis = mathUtil.getDistance(position, this.points[i])
            if(dis < Constant.minAdsorb){
                return {
                    vertex:i
                };
            }
        }

        const dis = mathUtil.getDistance(position, this.center)
        if(dis<this.radius){
            return SelectState.All
        }

        return null;
    }

    updatePoints(newPosition,index){
        let nextIndex = this.getNextIndex(index)
        let oppositeIndex = this.getNextIndex(nextIndex)
        const line = mathUtil.createLine1(this.points[oppositeIndex],this.points[index])
        if(line == null){
            return
        }
        const join = mathUtil.getJoinLinePoint(newPosition, line)
        const radius = mathUtil.getDistance(join,this.points[oppositeIndex])/2/Math.sqrt(2)
        if(radius<Constant.minAdsorb){
            return;
        }

        mathUtil.clonePoint(this.points[index],join)
        this.center = {
            x:(this.points[index].x + this.points[oppositeIndex].x)/2,
            y:(this.points[index].y + this.points[oppositeIndex].y)/2
        }

        this.radius = radius
        if(index == 0){
            this.points[3] = {
                x:this.points[0].x,
                y:this.points[2].y
            }
            this.points[1] = {
                x:this.points[2].x,
                y:this.points[0].y
            }
        }
        else if(index == 1){
            this.points[0] = {
                x:this.points[3].x,
                y:this.points[1].y
            }
            this.points[2] = {
                x:this.points[1].x,
                y:this.points[3].y
            }
        }
        else if(index == 2){
            this.points[1] = {
                x:this.points[2].x,
                y:this.points[0].y
            }
            this.points[3] = {
                x:this.points[0].x,
                y:this.points[2].y
            }
        }
        else if(index == 3){
            this.points[2] = {
                x:this.points[1].x,
                y:this.points[3].y
            }
            this.points[0] = {
                x:this.points[3].x,
                y:this.points[1].y
            }
        }
    }

    getNextIndex(index){
        let nextIndex = index + 1;
        if(nextIndex == 4){
            nextIndex = 0;
        }
        return nextIndex;
    }

    getLastIndex(index){
        let lastIndex = index - 1;
        if(lastIndex < 0){
            lastIndex = 3
        }
        return lastIndex;
    }
}
