import { _decorator, Button, Component, Constructor, Label, Node, PageView, Sprite } from 'cc';
import { GameMgr } from './GameMgr';
const { ccclass, property } = _decorator;

@ccclass('ModulerBase')
export class ModulerBase extends Component {
    //
    protected _nodes: Map<string, Node> = new Map();
    protected _nodeName: string = "Node";
    get NodeName() { return this._nodeName };
    set NodeName(name: string) { this._nodeName = name }
    //

    init(){
        //
        this._visit(this.node);
        this.onStart();
        //
    }

    //
    protected onStart() {

    }
    //

    //刷新
    refresh(){

    }

    //获取管理者中的其他模块
    getModuler<T extends ModulerBase>(type: Constructor<T>): T{
        return GameMgr.Instance.getModuler(type);
    }
    
    //销毁自己
    removeSelf(){
        this.node.destroy();
        this.clearSelf();
    }

    protected clearSelf(){
        
    }


    //
    private _visit(node: Node) {
            if (node.name.startsWith("_")) {
                this._nodes.set(node.name, node);
            }
            for (const child of node.children) {
                this._visit(child);
            }
        }
    
        show(){
            this.node.active = true;
            this.use;
        }
    
        showing(){
            return this.node.active;
        }
    
        hide(clear: boolean = false){
            this.unUse();
            if(clear){
                this.node.destroy();
                return;
            }
            this.node.active = false;
        }
    
        onBtnClick(name: string, callback: Function, ...arg){
            const node = this._nodes.get(name);
            if(!node) {
                Error("No find");
                return;
            }
            if(node){
                node.on(Button.EventType.CLICK,()=>{
                    callback.apply(this,[...arg]);
                })
            }
        }
    
        onPageView(name: string, type, callback: Function){
            this._nodes.get(name).on(type,callback);
        }
    
        getPageView(name: string): PageView{
            return this._nodes.get(name).getComponent(PageView);
        }
    
        getNode(name: string): Node{
            return this._nodes.get(name);
        }
    
        getLabel(name: string): Label{
            return this.getNode(name).getComponent(Label);
        }
    
        getSprite(name: string): Sprite{
            return this.getNode(name).getComponent(Sprite);
        }
    
        protected use(){
    
        }
    
        protected unUse(){
    
        }
    //
}


