import getAngle from "../physics/utils/getAngle";
import { award, awardMask } from "./collisionConfig";
import createItem from "./items/createItem";
import LabelType from "./LabelType";
import StartScene from "./StartScene";

const startY = 984-100-60;
const startX = 100;
const restitution = 0.7;
// const types = [LabelType.Candy2,LabelType.Donut,LabelType.Egg];
const types = [LabelType.Boom, LabelType.IceCream, LabelType.Donut, LabelType.Candy2, LabelType.Gift2, LabelType.Egg];
const frameInterval =60;

export default (_this: StartScene) => {
    let pastFrame = 0;
    const ontick = () => {
        if (pastFrame < frameInterval) {
            pastFrame++;
            return
        };
        if (pastFrame >= frameInterval) pastFrame = 0;
        const i = Math.floor(Math.random() * types.length);
        const type = types[i];
        let body;
        if (type == LabelType.Candy2) {
            const forceScale = 15;
            const leftStart = { x: startX, y: startY, forceX: 0.01 * forceScale };
            const rightStart = { x: 750 - startX, y: startY, forceX: -0.01 * forceScale };
            const start = Math.random() > 0.5 ? leftStart : rightStart;
            const scale = 2;
            const baseItem = createItem(scale, type);
            body = _this._egretRender.rectangle(start.x, start.y, 49 * scale, 18 * scale, baseItem,
                {
                    isStatic: false,
                    force: { x: start.forceX, y: 0 },
                    friction: 0,
                    restitution: restitution,
                    label: type,
                    angle: getAngle(10),
                    collisionFilter: { group: award, category: award, mask: awardMask }
                }
            );
        } else {
            const forceScale = 10;
            const leftStart = { x: startX, y: startY, forceX: 0.01 * forceScale };
            const rightStart = { x: 750 - startX, y: startY, forceX: -0.01 * forceScale };
            const start = Math.random() > 0.5 ? leftStart : rightStart;
            const size = 49 * 1.15;
            const maxsize = 49;
            const baseItem = createItem(size / maxsize, type);
            body = _this._egretRender.circle(start.x, start.y, size / 2, baseItem,
                {
                    label: type,
                    isStatic: false,
                    force: { x: start.forceX, y: 0 },
                    friction: 0,
                    restitution: restitution,
                    collisionFilter: { group: award, category: award, mask: awardMask }
                }
            );
        }
        return false;
    }

    egret.startTick(ontick, this)
}