import ComponentBase from "../components/ComponentBase";

export default class Loading extends ComponentBase {
    private static _parent: eui.Group;
    private static _instance: Loading;
    public waitImg: eui.Image;
    static get instace(): Loading {
        return Loading._instance || (Loading._instance = new Loading());
    }
    constructor() {
        super();
        this.horizontalCenter = 0;
        this.verticalCenter = 0;
    }

    show() {
        Loading._parent.addChild(this);
        if (this.skin && !this.hasEventListener(egret.Event.ENTER_FRAME)) {
            this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
        }

        Loading._parent.visible = true;
    }

    hide() {
        if (Loading._parent.getChildIndex(this) == -1) return;

        Loading._parent.removeChild(this);
        if (this.hasEventListener(egret.Event.ENTER_FRAME)) {
            this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
        }

        if (Loading._parent.numChildren == 0) {
            Loading._parent.visible = false;
        }
    }

    static init(parent: eui.Group) {
        Loading._parent = parent;
    }

    onSkinComplete(): any {
        super.onSkinComplete();
        if (!this.hasEventListener(egret.Event.ENTER_FRAME))
            this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
    }

    onEnterFrame() {
        this.waitImg.rotation += 5;
    }

    public get skinKey() { return 'Loading' }
    public get groupName() { return 'loading' }
}
