import { ReveData } from "../util/CHttp";
import CHttpEvent from "../util/CHttpEvent";
import Main from "./Main";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ViewObject extends cc.Component implements CHttpEvent {

    public main:Main

    /**
     * 当前界面从那个界面创建过来
     * 用于返回上个界面
     */
    public prev:ViewObject;
    /**
     * 关闭回调
     */
    public closeCallback:()=>void;
    public setCloseCallback(closeCallback:()=>void){
        this.closeCallback = closeCallback
    }
    /**
     * 
     * @param prev 父界面
     */
    public show(prev?:ViewObject){
        if(prev){
            this.prev = prev;
            this.prev.__close();
        }
        this.main.viewManage.popView(this.node);
        if(this.main && this.main.gameHttp){
            this.main.gameHttp.pushEvent(this);
        }
    }
    /**
     * 只是关闭界面，不销毁
     */
    public __close(){
        this.node.removeFromParent();
        if(this.closeCallback){
            this.closeCallback()
        }
    }
    /**
     * 退出销毁界面
     */
    public exitDistroy(){
        if(this.main && this.main.gameHttp){
            this.main.gameHttp.popEvent(this);
        }
        this.node.destroy();
        if(this.prev){
            //如果存在父界面将其显示出来
            this.prev.show();
        }
        if(this.closeCallback){
            this.closeCallback()
        }
    }
    /**
     * 销毁所有父界面
     */
    public __distroyAll(){
        if(this.main && this.main.gameHttp){
            this.main.gameHttp.popEvent(this);
        }
        if(this.prev && this.prev.isValid){
            this.prev.__distroyAll();
        }
        if(this.node.isValid){
            this.node.destroy();
        }
        if(this.closeCallback){
            this.closeCallback()
        }
    }
    /**
     * 监听网络
     * @param reveData 
     */
    public httpEvent(reveData:ReveData){
        // cc.log('viewObject reve : ',this);
    }
  
}
