export enum on_off{ on, off }

export  class Switch extends Laya.Script {

   private CloseSprite:Laya.Image;
   private OpenSprite:Laya.Image;
   private Button:Laya.Button;
   private kaiguan:on_off = on_off.off;

    OpenFunction:Laya.Handler;
    CloseFunction:Laya.Handler;
    
    constructor() { super() }
    onAwake(){
        this.CloseSprite = this.owner.getChildByName("close") as Laya.Image;
        this.OpenSprite = this.owner.getChildByName("open") as Laya.Image;
        this.Button = this.owner.getChildByName("btn") as Laya.Button;
        this.Button.clickHandler = Laya.Handler.create(this,()=>{
            this.ChangeMode(this.kaiguan);
            this.kaiguan++;
            if(this.kaiguan>1){
                this.kaiguan = 0;
            }
        },null,false)
    }
    ListenOn_Off(on = true,OpenFunction:Laya.Handler = null,CloseFunction:Laya.Handler =null){
        this.kaiguan = on?on_off.on : on_off.off;
        this.OpenFunction = OpenFunction;
        this.CloseFunction = CloseFunction;
        this.ChangeMode(this.kaiguan);
        this.kaiguan++;
        if(this.kaiguan>1){
            this.kaiguan = 0;
        }
    }
    ChangeMode(_mode:on_off){
        switch (_mode) {
            case on_off.off:
            this.OpenSprite.visible = false;
            this.CloseSprite.visible = true;
            this.CloseFunction.run();
                break;
            case on_off.on:
            this.OpenSprite.visible = true;
            this.CloseSprite.visible = false;
            this.OpenFunction.run();
                break;
        }
    }
}