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

//不靠墙
export default class Tag extends Geometry {
    constructor(center, vectorId) {
        super()
        this.center = center
        this.points2d = []
        this.value = '文本';

        this.sideWidth = 30 //像素
        this.sideThickness = 30 //像素

        this.color = 'rgba(0,0,0,1)';
        this.fontSize = 12;

        this.geoType = VectorType.Tag
        this.setId(vectorId)
    }

    isContain(position) {
        let points = []
        points.push(this.points2d[0])
        points.push(this.points2d[1])
        points.push(this.points2d[2])
        points.push(this.points2d[3])
        return mathUtil.isPointInPoly(position, points)
    }

    setFontLenAndHeight(){
        let height = 1;
        let row = 0;
        let textValues = [];
        textValues[0] = '';
        for(let i=0;i<this.value.length;++i){
            if(this.value[i] == '\n'){
                ++height;
                ++row;
                textValues[row] = '';
            }else{
                textValues[row] += this.value[i]
            }
        }
        return {
            textValues:textValues,
            height:height
        }
    }

    setPoints2d() {
        this.points2d = []
        const minX = this.center.x - ((this.sideWidth / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2
        const minY = this.center.y - ((this.sideThickness / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2
        const maxX = this.center.x + ((this.sideWidth / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2
        const maxY = this.center.y + ((this.sideThickness / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2

        const point1 = {
            x: minX,
            y: maxY,
        }

        const point2 = {
            x: maxX,
            y: maxY,
        }

        const point3 = {
            x: maxX,
            y: minY,
        }

        const point4 = {
            x: minX,
            y: minY,
        }

        this.points2d.push(point1)
        this.points2d.push(point2)
        this.points2d.push(point3)
        this.points2d.push(point4)

        //-
        let dx = (point1.x - this.center.x) / 2
        //+
        let dy = (point1.y - this.center.y) / 2

        this.points2d.push({
            x: point1.x - dx,
            y: point1.y - dy,
        })

        this.points2d.push({
            x: point2.x + dx,
            y: point1.y - dy,
        })

        this.points2d.push({
            x: this.center.x,
            y: point1.y - dy,
        })

        this.points2d.push({
            x: this.center.x,
            y: point3.y + dy,
        })
    }

    setValue(value) {
        this.value = value
    }

    setColor(color) {
        this.color = color
    }

    setFontSize(fontSize) {
        this.fontSize = fontSize
    }
}
