import Model from "../Core/Model";
import { defaultData } from "../defaultData";

export default class MenuModel extends Model{

    public stageIdx=0;
    private data;
    public async init() {
		let getData = this.getData.bind(this);
		if (window && window.courseware) {
			getData = window.courseware.getData;
		}

		getData((data) => {
			cc.log('get data:' + data);
			this.data = data || this.getDefaultData();
			this.data = JSON.parse(JSON.stringify(this.data))
		})
    }

    private getDefaultData() {
        return defaultData;
    }
    
	private getData(func) {
		if (window && window.courseware) {
			window.courseware.getData(func, 'scene');
			return;
		}

		const middleLayer = cc.find('middleLayer');
		if (middleLayer) {
			const middleLayerComponent = middleLayer.getComponent('middleLayer');
			middleLayerComponent.getData(func);
			return;
		}

		func(this.getDefaultData());
	}
    
    public getFont(){
        let node=cc.find("res/font/MMTextBook-Bold");
        let label=node.getComponent(cc.Label);
        return label.font;
    }
    public getConfigs(){
        return this.data;
    }

    public getItems(){
        return this.data.items;
    }

    public getItem(idx:number){
        return this.data.items[idx];
    }

    public getStartAudio(){
        return this.data.startAudio;
    }

    public getWaitAudio(){
        return this.data.waitAudio;
    }

    public getFinishAudio(){
        return this.data.finishAudio;
    }
    
    public getFinishWaitAudio(){
        return this.data.finishWaitAudio;
    }

    public getBG(){
        return this.data.bg;
    }

    public getImages(){
        return this.data.images;
    }

    public getImage(idx:number){
        return this.data.images[idx];
    }
    
    release() {
    
    }

}