import { SelectCtrl } from ".";

export class AssistCtrl {
    ctrl: SelectCtrl
    
    constructor(ctrl: SelectCtrl) {
        this.ctrl = ctrl;
    }

    _flashTimer?:any;
    _flashDraw(cb:()=>void) {
        this.ctrl.assistRuler?.draw();

        let total = 0
        const ctx = this.ctrl._selCtx;
        const size = this.ctrl._selCanvaseSize;

        if (this._flashTimer) {
            clearInterval(this._flashTimer);
            cb();
        }
        this._flashTimer = setInterval(()=>{
            total += 1;
            if (total > 5) {
                clearInterval(this._flashTimer);
                this._flashTimer =  null;
                this.ctrl.assistRuler?.draw();
                return;
            }
            cb();
        }, 100)
    }

    drawCardDists() {
        if ( !this.ctrl.objContainer ) return;

        const size = this.ctrl._selCanvaseSize;
        const ctx = this.ctrl._selCtx;
        const cardBox = this.ctrl.getCurrCardBox();
        const veiewBox = this.ctrl.getViewPortBox();

        const ObjC = this.ctrl.objContainer 
        const bound = ObjC.getBound()
        ctx.lineWidth = 2;
        ctx.beginPath(); // Start a new path
        ctx.strokeStyle = "orange"; 
        ctx.font = "36px Arial";
        ctx.fillStyle = "orange"; 

        //左边
    

        let x = (cardBox.x + bound.x)*2;
        let y = (cardBox.y + bound.y + bound.height / 2.0) *2
        const tl = ( x - cardBox.x*2).toFixed(0);
        const m = ctx.measureText(tl)
        ctx.moveTo(veiewBox.left *2, y); 
        ctx.lineTo(veiewBox.left *2 + 30, y);
        ctx.stroke();
	    ctx.closePath();
        ctx.fillText(tl, veiewBox.left *2, y - 18);

        //右边
        x = (cardBox.x + bound.x + bound.width);
        const x2 = (cardBox.x + cardBox.width);
        const tr = ((x2-x)*2).toFixed(0);
        const m2 = ctx.measureText(tr)

        ctx.beginPath(); // Start a new path
        ctx.moveTo(veiewBox.right*2.0, y); 
        ctx.lineTo( veiewBox.right*2.0 - 30, y); 
        ctx.stroke();
        ctx.closePath();
        ctx.fillText(tr, veiewBox.right*2.0 - m2.width, y-18);

        //上边
        x = cardBox.x + cardBox.width / 2.0;
        y = cardBox.y;

        ctx.beginPath(); // Start a new path
        ctx.moveTo(x*2, veiewBox.top*2); // Move the pen to (30, 50)
        ctx.lineTo( x*2, veiewBox.top*2 + 40 ); // Draw a line to (150, 100)
        ctx.stroke(); // Render the path
        let dy = (bound.y*2).toFixed(0);
        ctx.fillText(dy, x*2 + 10, veiewBox.top*2 +36);


        //下边
        x = cardBox.x + cardBox.width / 2.0;
        y = cardBox.y + cardBox.height;

        ctx.beginPath(); // Start a new path
        ctx.moveTo(x*2, veiewBox.bottom*2.0); // Move the pen to (30, 50)
        ctx.lineTo( x*2, veiewBox.bottom*2.0 - 40); // Draw a line to (150, 100)
        ctx.stroke(); // Render the path
        dy = ((cardBox.height - bound.y - bound.height)*2).toFixed(0);
        ctx.fillText(dy, x*2 + 10, veiewBox.bottom*2.0);
        
        //如果有旋转角度
        if ( Math.abs(ObjC.parent.rotation) > 0.001 ) {
            ctx.strokeRect((bound.x + cardBox.x)*2, (bound.y + cardBox.y)*2, bound.width *2, bound.height *2);
            //绘制角度
            
            const r = (ObjC.parent.rotation * 180 / Math.PI).toFixed(1) + "°";
            const m = ctx.measureText(r )

            ctx.fillText(r , (bound.x + cardBox.x)*2 + bound.width - m.width / 2, (bound.y + cardBox.y)*2 + bound.height - 18);
        }
    }

    flashDrawCardDists() {
        this._flashDraw(()=>{
            this.drawCardDists()
            this.ctrl.assistMagnet?.draw();
        });
    }
}