import GameView from "./GameView";
import LabelType from "./LabelType";
import { award } from "./collisionConfig";
import vibrate from "./vibrate";
import { playBoom, playSuccess } from "./playMovieClip";
import { getScore, getLevelData } from "./goldData";
import { createNewBubble } from "./createItems";
import { playTextAni } from "./playTextAni";


export function check (_this:GameView) {
    const egretRenderContainer = _this.egretRenderContainer;
    const center = 375;
    const paddingX = 50;
    const ypos = 570;
    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(egretRenderContainer);
                _this.onBoom();
            }
            else if (i.label == LabelType.egg) {
                _this._egretRender.remove(i);
                //PanelCtrl.instance.show(ModuleTypes.SLOT_PANEL);
            }
            else if (i.label == LabelType.candy) {
                _this._egretRender.remove(i);
                //PanelCtrl.instance.show(ModuleTypes.ROTATE_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);
                playTextAni(_this)
            }
        }
    })
}

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

export default check