import { Item } from './Item';
import { MySprite, tweenChange } from './Unit';
import { playAudio } from './AudioUtil';

export class Button extends Item{

    btnPic : MySprite;

    private _outX = 0;

    set outX(v){
        this._outX =v ;
    }
    get outX(){
        return this._outX;
    }

    private _outY = 0;

    set outY(v){
        this._outY =v ;
    }
    get outY(){
        return this._outY;
    }

    init(picName){

        const btnPic = this.getPic(picName);
        this.width = btnPic.width;
        this.height = btnPic.height;
        btnPic.x = this.x;
        btnPic.y = this.y;
        this.btnPic = btnPic;
    }

    click(callback = null){

        playAudio(this.audioObj['click'], true);

        //放大
        tweenChange(this.btnPic, {scaleX: this.scaleX * 1.3, scaleY : this.scaleY * 1.3}, 0.1, ()=>{
            
            //恢复
            tweenChange(this.btnPic, {scaleX: this.scaleX, scaleY : this.scaleY}, 0.1, ()=>{

                //移出
                tweenChange(this.btnPic, {x : this.outX, y:this.outY}, 0.3, ()=>{
                    if(callback){
                        callback();
                    }
                })
            });
        })
        
    }

    show(callback = null){

        this.btnPic.x = this.x;
        this.btnPic.y = this.y;

        this.btnPic.visible = true;

        if(callback){
            callback();
        }
    }
}