import Leg from "./Leg";
import GoldCoin from "./GoldCoin";
import DisplayCollider, { Tag } from "./DisplayCollider";
import GameMgr from "../Mgr/GameMgr";
import { MUtils } from "../MUtils";
import { MConst } from "../MConst";
import { Decorator } from "../Decorator/Decorator";
import PhysicalSystem from "./PhysicalSystem";
import { ThroughSubmitData } from "../../libs/tw/data/custom/throughSubmit/ThroughSubmitData";

@Decorator.OnUpdate
@Decorator.OnDestroy
export default class GameStage extends egret.DisplayObjectContainer {
    public onGoldCoinEated: (score: number) => void;
    private shakeDirection = 0;
    public shadowLayer: egret.DisplayObjectContainer = null;
    public legLayer: egret.DisplayObjectContainer = null;
    public antLayer: egret.DisplayObjectContainer = null;
    public goldCoinLayer: egret.DisplayObjectContainer = null;

    constructor(randomBg: boolean) {
        super();
        let bgId: number = 1;
        if (randomBg) {
            bgId = MUtils.randomInt(1, 4);
        }
        let bg = new egret.Bitmap(RES.getRes(`bg${bgId}_jpg`));
        bg.y = -10;
        this.addChild(bg);

        this.shadowLayer = new egret.DisplayObjectContainer();
        this.addChild(this.shadowLayer);

        this.legLayer = new egret.DisplayObjectContainer();
        this.addChild(this.legLayer);

        this.goldCoinLayer = new egret.DisplayObjectContainer();
        this.addChild(this.goldCoinLayer);

        this.antLayer = new egret.DisplayObjectContainer();
        this.addChild(this.antLayer);

        let leftWall = new DisplayCollider(Tag.Wall, new egret.Rectangle(0, 0, 100, 750));
        leftWall.width = 100;
        leftWall.height = 750;
        leftWall.x = -100;
        this.addChild(leftWall);
        let rightWall = new DisplayCollider(Tag.Wall, new egret.Rectangle(0, 0, 100, 750));
        rightWall.width = 100;
        rightWall.height = 750;
        rightWall.x = GameMgr.instance.stage.stageWidth;
        this.addChild(rightWall);

        //创建地面碰撞器
        let groundCollider = new DisplayCollider(Tag.Ground, MConst.GroundLocalRect);
        this.addChild(groundCollider);

        // egret.startTick(this.onFixedUpdate, this);
    }

    onDestroy() {
        // egret.startTick(this.onFixedUpdate, this);
    }

    onFixedUpdate() {
        //物理引擎运行
        PhysicalSystem.instance.detectCollision();
        return false;
    }

    onUpdate() {
        if (this.shakeDirection > 0) {
            this.y += 1;
            if (this.y >= 9) {
                this.shakeDirection = -1;
            }
        } else if (this.shakeDirection < 0) {
            this.y -= 1;
            if (this.y <= 0) {
                this.y = 0;
                this.shakeDirection = 0;
            }
        }
        this.onFixedUpdate();
    }

    public shake() {
        /* return new Promise((resolve) => {
            let callback = () => {
                if (this.shakeDirection == 0) {
                    this.removeEventListener(egret.Event.ENTER_FRAME, callback, this);
                    resolve();
                }
            };
            
            this.addEventListener(egret.Event.ENTER_FRAME, callback, this);
        }); */

        if (this.shakeDirection == 0) {
            this.shakeDirection = 1;
        }
    }

    createGoldCoin(x: number) {
        let goldCoin = new GoldCoin(this);
        goldCoin.onEated = this.onGoldCoinEated;
        goldCoin.land(x, -500);
        this.goldCoinLayer.addChild(goldCoin);
        return goldCoin;
    }


    private legList: Leg[] = [];
    public getLegList() {
        return this.legList;
    }
    public addLeg(leg: Leg) {
        this.legList.push(leg);
    }
    public removeLeg(leg: Leg) {
        this.legList.splice(this.legList.indexOf(leg), 1);
    }
}