import { award } from "./collisionConfig";
import LabelType from "./LabelType";
import { addItem, isAllLevelPass, isLevelPass, nextLevel, clearLevel } from "./level";
import StartScene from "./StartScene";
import vibrate from "./vibrate";
import { playBoom, playSuccess, playToast, playBoomAlert } from "./playMovieClip";
import { stopCreateItems } from "./createItems";

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

    const center2 = 643 + 8;
    const paddingX2 = 40 + 5;
    const ypos2 = 1386 - 20 - 5 - 110;
    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(async (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 > 1624 - 100) {
            _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);
            playToast(_this, i.label, xResult && yResult);
            if (xResult && yResult) {
                //leftbasket rightbasket
                egret.Tween.removeTweens(_this['leftbasket']);
                egret.Tween.removeTweens(_this['leftbasket1']);
                egret.Tween.get(_this['leftbasket']).to({ scaleX: 1.2 }, 100).to({ scaleX: 1 }, 100);
                egret.Tween.get(_this['leftbasket1']).to({ scaleX: 1.2 }, 100).to({ scaleX: 1 }, 100);
            } else {
                egret.Tween.removeTweens(_this['rightbasket']);
                egret.Tween.removeTweens(_this['rightbasket1']);
                egret.Tween.get(_this['rightbasket']).to({ scaleX: 1.2 }, 100).to({ scaleX: 1 }, 100);
                egret.Tween.get(_this['rightbasket1']).to({ scaleX: 1.2 }, 100).to({ scaleX: 1 }, 100);
            }
            if (i.label == LabelType.Boom) {
                _this._egretRender.remove(i);
                if (xResult && yResult) {
                    //左边ok
                } else {
                    //右边clear
                    clearLevel();
                    vibrate(_this, 2);
                    playBoomAlert(_this)
                    playBoom(_this['group'])
                }

            }
            else {
                if (xResult2 && yResult2) {
                    playSuccess(_this['group']);
                    await addItem(i.label);

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

        }
    })


}

export default check