import { award } from "./collisionConfig";
import StartScene from "./StartScene";
import { playBoom, playSuccess } from "./playMovieClip";
import vibrate from "./vibrate";
import LabelType from "./items/LabelType";
import { createNewBubble } from "./createItems";
import { getCurrentScore, getLevelData } from "./levels";
import PanelCtrl from "../../libs/new_wx/ctrls/panelCtrl";
import { ModuleTypes } from "../../libs/new_wx/types/sceneTypes";

const check = (_this: StartScene) => {
    const center = 382;
    const paddingX = 50;
    const ypos = 440;
    const h = 40;
    // _this._egretRender._root.graphics.beginFill(0xff0000, 1);
    // _this._egretRender._root.graphics.drawRect(center - paddingX, ypos, paddingX * 2, h);
    // _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;
        const xResult = x > (center - paddingX) && x < (center + paddingX);
        const yResult = y > ypos && y < (ypos + h);
        if (xResult && yResult && i.velocity.y > 0 && !i['timer']) {
            i['timer'] = Date.now();
            if (i.label == LabelType.boom) {
                _this._egretRender.remove(i);
                vibrate(_this, 2);
                playBoom(_this.egretRenderContainer);
                _this.onBoom();
            }
            else if (i.label == LabelType.egg) {
                _this._egretRender.remove(i);
                PanelCtrl.instance.show(ModuleTypes.TREASURE_PANEL);
                // PanelCtrl.instance.show(ModuleTypes.SLOT_PANEL);
            }
            else if (i.label == LabelType.candy) {
                _this._egretRender.remove(i);
                PanelCtrl.instance.show(ModuleTypes.TREASURE_PANEL);
            }
            else if (i.label == LabelType.gift) {
                _this._egretRender.remove(i);
                PanelCtrl.instance.show(ModuleTypes.TREASURE_PANEL);
            } else {
                // console.log('不是boom，重用', Date.now() / 1000);
                // playScoreAni(_this.egretRenderContainer, 1)
                playSuccess(_this.egretRenderContainer);
                _this._egretRender.remove(i);
                _this.addScore();
                checkAdd(_this);
            }
        }
    })
}

const checkAdd = (that: StartScene) => {
    const oldscore = getCurrentScore();
    const awards = getLevelData().awards;
    for (const award of awards) {
        if (award[0] == oldscore) {
            const type = award[1];
            createNewBubble(that, type);
            break;
        }
    }
}

export default check