import { award } from "./collisionConfig";
import LabelType from "./LabelType";
import { addItem, isAllLevelPass, isLevelPass, nextLevel, clearLevel } from "./level";
import StartScene from "./StartScene";
import vibrate from "./vibrate";

const check = (_this: StartScene) => {
    const center = 109;
    const paddingX = 30;
    const ypos = 1386;
    const h = 100;

    const center2 = 643;
    const paddingX2 = 40 + 5;
    const ypos2 = 1386 - 20 - 5;
    const h2 = 50;

    // _this._egretRender._root.graphics.beginFill(0xff0000, 1);
    // _this._egretRender._root.graphics.drawRect(center - paddingX, ypos, paddingX * 2, h);
    // _this._egretRender._root.graphics.endFill();
    // _this._egretRender._root.graphics.beginFill(0xff0000, 1);
    // _this._egretRender._root.graphics.drawRect(center2 - paddingX2, ypos2, paddingX2 * 2, h2);
    // _this._egretRender._root.graphics.endFill();

    const bodies = Matter.Composite.allBodies(_this._engine.world);
    const isAward = (i: Matter.Body) => { return i.collisionFilter.group == award }
    bodies.forEach(i => {
        if (!isAward(i)) return;
        if (i['timer']) {
            const t0 = Date.now() - i['timer'];
            if (t0 > 3000)//3s之内不可再用
            {
                delete i['timer'];
                console.log('重新可用', Date.now() / 1000)
            }
        }
        const { x, y } = i.position;
        if (y > 1550) {
            _this._egretRender.remove(i);
            return;
        }
        const xResult = x > (center - paddingX) && x < (center + paddingX);
        const yResult = y > ypos && y < (ypos + h);
        const xResult2 = x > (center2 - paddingX2) && x < (center2 + paddingX2);
        const yResult2 = y > ypos2 && y < (ypos2 + h2);
        const fallingIn = xResult && yResult || xResult2 && yResult2;
        if (fallingIn && i.velocity.y > 0 && !i['timer']) {
            i['timer'] = Date.now();
            // console.log(i.label);
            if (i.label == LabelType.Boom) {
                _this._egretRender.remove(i);
                if (xResult && yResult) {
                    //左边ok
                } else {
                    //右边clear
                    clearLevel();
                    vibrate(_this, 2);
                }
            }
            else {
                if (xResult2 && yResult2) {
                    addItem(i.label);
                    if (isLevelPass()) {
                        if (isAllLevelPass()) {

                        } else {
                            nextLevel();
                        }
                    }
                }
                // console.log('不是boom', Date.now() / 1000);
                _this._egretRender.remove(i);
            }
        }
    })


}

export default check