export enum SwitchButtonState
{
    Close,Open
}

export class SwitchButton extends Laya.Script
{
    private openSprite:Laya.Sprite;
    private closeSprite:Laya.Sprite;
    private button:Laya.Button;

    private openHandle:Laya.Handler;
    private closeHandle:Laya.Handler;

    private ButtonState:SwitchButtonState = SwitchButtonState.Close;
    constructor()
    {
        super();
    }

    onAwake()
    {
        this.openSprite = this.owner.getChildByName("openSprite")as Laya.Sprite;
        this.closeSprite = this.owner.getChildByName("closeSprite")as Laya.Sprite;
        this.button = this.owner.getChildByName("button")as Laya.Button;

        this.button.clickHandler = Laya.Handler.create(this,()=>
        {
            this.ChangeState(this.ButtonState);
            this.ButtonState ++;
            if(this.ButtonState > 1)
            {
                this.ButtonState = 0;
            }
        },null,false);
    }

    public Init(open = true,_openHandle:Laya.Handler = null,_closeHnadle:Laya.Handler = null)
    {
        this.ButtonState = open?SwitchButtonState.Open:SwitchButtonState.Close;
        this.openHandle = _openHandle;
        this.closeHandle = _closeHnadle;

        this.ChangeState(this.ButtonState);
        this.ButtonState ++;
        if(this.ButtonState > 1)
        {
            this.ButtonState = 0;
        }
    }

    public get CurState():SwitchButtonState
    {
        return this.ButtonState;
    }

    public ChangeState(_state:SwitchButtonState)
    {
        switch(this.ButtonState)
        {
            case SwitchButtonState.Close:
            this.openSprite.visible = false;
            this.closeSprite.visible = true;
            this.closeHandle.run();
            break;
            case SwitchButtonState.Open:
            this.openSprite.visible = true;
            this.closeSprite.visible = false;
            this.openHandle.run();
            break;
        }
    }
}