
import { _decorator, AssetManager, assetManager, Component, find, instantiate, Label, Node, Prefab, sys, screen, view, ResolutionPolicy } from 'cc';
import { Config, nav_info } from './NavInfo';
import { UtilsPanel } from './UtilsPanel';
const { ccclass } = _decorator;

@ccclass('MainScene')
export class MainScene extends Component {
	private node_tip: Node = null;
	private lb_tip: Label = null;

    protected onLoad(): void {
        // if (sys.isBrowser || sys.isMobile) screen.requestFullScreen()
        if(!sys.isMobile){
            view.setResolutionPolicy(ResolutionPolicy.SHOW_ALL);
        }
        UtilsPanel.getAllNeedCom(this, find("Canvas"), true);
        nav_info.init();
        nav_info.scene = this;
        Config.init();
        this.loadPrefab();    
    }

    private loadPrefab(){
        assetManager.loadBundle(nav_info.bundle, (err:any, bundle:AssetManager.Bundle)=>{
            if(err)return;
            bundle.load("prefab/" + nav_info.pre, Prefab, (err:any, pre:Prefab)=>{
                let new_node = instantiate(pre);
                new_node.setParent(find("Canvas"));
                this.node_tip.setSiblingIndex(-1);
            })
        })
    }

    public showTip(str:string){
        this.unschedule(this.hideTip);
        this.node_tip.active = true;
        this.lb_tip.string = str;
        this.scheduleOnce(this.hideTip, 2)
    }
    private hideTip(){
        this.node_tip.active = false;
    }
    
    public showByUrl(url: string){
        let bundle = assetManager.getBundle(nav_info.bundle);
        if(!bundle)return;
        bundle.load(url, Prefab, (err:any, pre:Prefab)=>{
            if(err)return;
            let new_node = instantiate(pre);
            new_node.setParent(find("Canvas"));
            this.node_tip.setSiblingIndex(-1);
        })
    }
}
