import { DesignComp } from "../../objects/DesignTemp/DesignComp";
import { Matrix } from "./matrix";
import { DisplayObject } from "./objects/displayObject";
import { isRectInter } from "./objects/mathUtils";
import { Rectangle } from "./objects/rectangle";

function designSizeToPx(value: number) {
    return value / 2.0;
}
function pxToDesignSize(value: number) {
    return value * 2.0;
}
export class CompObject extends DisplayObject {
    comp:DesignComp;
    rect = new Rectangle();

    _width = 0;
    _height = 0;

    constructor(c:DesignComp) {
        super();
        this.comp = c;
        if (c.layout.size ) {
            if (c.layout.size[0]) {
                this._width =  designSizeToPx(c.layout.size[0]);
            }
            if (c.layout.size[1]) {
                this._height =  designSizeToPx(c.layout.size[1]);
            }
        }
        if (c.$el) {
            if (!c.layout.size) {
                this._width = c.$el.clientWidth;
                this._height = c.$el.clientHeight;
                c.layout.size = [pxToDesignSize(this._width), pxToDesignSize(this._height)]
            } else if (!c.layout.size[0]) {
                this._width = c.$el.clientWidth;
                c.layout.size[0] = pxToDesignSize(this._width)

            }else if (!c.layout.size[1]) {
                this._height = c.$el.clientHeight;
                c.layout.size[1] = pxToDesignSize(this._height)
            }
        }
        this.transform.setFromMatrix(Matrix.createFromMatrixStr(c.layout.transformMatrix || "matrix(1,0,0,1,0,0)"));
        this.updateTransform();
        this._boundsID++;
    }

    get width() {
        return  this._width
    }
    set width( value ) {
        this._width = value;
        this.comp.layout.size[0] = pxToDesignSize(this._width);
    }
    set height( value ) {
        this._height = value;
        this.comp.layout.size[1] = pxToDesignSize(this._height);
    }
    
    get height() {
        return this._height
    }

    getRect(){
        let p1 = {x:0,y:0} as any;
        this.worldTransform.apply(p1, p1);

        let p2 = {x: this.width , y:0} as any;
        this.worldTransform.apply(p2, p2);

        let p3 = {x:this.width,y:this.height} as any;
        this.worldTransform.apply(p3, p3);

        let p4 = {x:0,y:this.height} as any;
        this.worldTransform.apply(p4, p4);

        return {p1,p2,p3,p4};
    }

    //判定兩個矩形有没有相交
    //依据四个定点是否有相交
    testRect(posSize:any, more = true) {

        let {x,y,w,h} = posSize;

        let rect1 = {p1:{x,y},p2:{x:x+w,y},p3:{x:x+w,y:y+h},p4:{x:x,y:y+h}};

        let rect2 = this.getRect();

        if( more ) return isRectInter(rect1, rect2);

        //框选模式,包含才匹配
        let minX = x, maxX = x + w;
        let minY = y, maxY = y + h;

        let ponts = [rect2.p1, rect2.p2,  rect2.p3, rect2.p4];
        let n = ponts.length;
        while( n-- ) {
            let p = ponts[n];
            if( p.x < minX || p.x > maxX || p.y < minY || p.y > maxY ) return false;
        }

        return true;
    }

    calculateBounds()
    {
        this._bounds.clear();
        this._bounds.addFrame(this.transform, 0,0, this.width, this.height);
        return this._bounds;
    }

    testClick(sx:number,sy:number)
    {
        let w = this.width;
        let h = this.height;
        let local = {x:0,y:0} as any;
        this.worldTransform.applyInverse({x:sx,y:sy} as any, local);

        if(  local.x < 0 || local.x > w ) return false;
        if(  local.y < 0 || local.y > h ) return false;

        return true;
    }

    getBox()
    {
        let rect = this.getBounds(false, this.rect);
        return {x:rect.x, y:rect.y, w:rect.width, h:rect.height};
    }

    getAabb() {
        let aabb = this.getBounds(false, this.rect)

        return {xmin:aabb.left,ymin:aabb.top, xmax:aabb.right, ymax:aabb.bottom};
    }

    center() {
        let p = {x:this.width/2,y:this.height /2} as any;
        this.worldTransform.apply(p,p);
        return p;
    }
}