import VectorType from '../enum/VectorType.js'
import Icon from '../Geometry/Icon.js'
import { mathUtil } from '../MathUtil.js'
import { floorplanService } from './FloorplanService'
import Constant from '../Constant'

export default class IconService {
    constructor() {
        this.values = [];
    }

    getNewValue(){
        let value = 1;
        while(true){
            if(this.values.includes(value)){
                ++value;
                continue;
            }
            else{
                this.values.push(value);
                break;
            }
        }
        return value;
    }

    createIcon(leftTopPosition,rightDownPosition,value,vectorId,floor) {
        const radius = mathUtil.getDistance(leftTopPosition,rightDownPosition)/2
        const center = {
            x:(leftTopPosition.x+rightDownPosition.x)/2,
            y:(leftTopPosition.y+rightDownPosition.y)/2
        }
        if(value == null){
            value = this.getNewValue();
        }
        else{
            if(!this.values.includes(value)){
                this.values.push(value);
            }
        }
        const icon = new Icon(center, radius,value,vectorId,floor)
        floorplanService.addIcon(icon)
        return icon
    }

    createIcon2(center,radius,value,vectorId,floor) {
        const icon = new Icon(center, radius,value,vectorId,floor)
        floorplanService.addIcon(icon)
        return icon
    }

    updateIconVertex(vectorId,newPosition,index){
        const icon = floorplanService.getIcon(vectorId)
        icon.updatePoints(newPosition,index)
    }

    setIconInfo(iconInfo) {
        let icon = floorplanService.getIcon(iconInfo.vectorId)
        icon.vectorId = iconInfo.vectorId
        icon.radius = iconInfo.radius
        icon.value = iconInfo.value
        icon.angle = iconInfo.angle
        icon.center = JSON.parse(JSON.stringify(iconInfo.center))
        icon.points = JSON.parse(JSON.stringify(iconInfo.points))
    }

    deleteIcon(iconId, floorNum) {
        const icon = floorplanService.getIcon(iconId)
        const index = this.values.indexOf(icon.value);
        this.values.splice(index,1)
        floorplanService.deleteIcon(iconId, floorNum)
    }
}

const iconService = new IconService()
export { iconService }