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

export default class Loading extends ComponentBase {
    private static _parent: eui.UILayer;
    private static _instance: Loading;
    public waitImg:eui.Image;
    public mcGroup:eui.Group;
    public loadbg: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)){
             // 通过分组获取资源
            // console.log("233333")
            // alert("1212")
            // var data = RES.getRes("loadgif_json");
            // var txtr = RES.getRes("loadgif_png");
            // console.log("gif!!!!!", data,txtr );        // 我就是通过该方法找到问题的，未加载就是undefined 加载成功是object

            // var mcFactory: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(data,txtr);
            // var mc1: egret.MovieClip = new egret.MovieClip(mcFactory.generateMovieClipData("long3"));
            // console.log(mc1)
            // this.mcGroup.addChild(mc1);
            // console.log(this.mcGroup)
            // // mc1.play(0); 
            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.UILayer) {
        Loading._parent = parent;
    }

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

            var data = RES.getRes("loadgif_json");
            var txtr = RES.getRes("loadgif_png");
            var mcFactory: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(data,txtr);
            var mc1: egret.MovieClip = new egret.MovieClip(mcFactory.generateMovieClipData(""));
            
            this.mcGroup.addChild(mc1);
            console.log(this.mcGroup)

            mc1.play(-1);
            this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
        }
            
    }

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

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