import { _decorator, Button, Component, EventHandler, Node } from 'cc';
import { AudioMgr } from '../manager/AudioMgr';
import { EventType } from '../enum/EventType';
import { UIMgr } from '../manager/UIMgr';
const { ccclass, property, disallowMultiple, requireComponent } = _decorator;

@ccclass('UIExtend/BtnExtend')
@disallowMultiple(true)
@requireComponent(Button)
export class BtnExtend extends Component {
    @property({ range: [0, 1, 0.1], slide: true })
    private safeTime: number = 0.5
    @property
    private zoomScale:number = 1.1
    @property
    private clickSfx: string = 'btnClick'
    @property
    private openUI: string = ''
    @property
    private closeUI: string = ''

    private btn: Button = null

    private clickEvents: EventHandler[]

    private isSafe: boolean = true

    protected onLoad() {

      
        console.log('zh:btnExtend.ts    onLoad');
       // console.log('zh:btnExtend.ts   EventType=',EventType)
        this.btn = this.getComponent(Button)
        //console.log('zh:btnExtend.ts btn  Button=',this.btn)
        this.btn.zoomScale = this.zoomScale
        this.btn.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.btn.node.on(EventType.Click, this.onClick, this)
    }

    protected onTouchStart(): void {
        this.handleClickSfx()
    }

    protected onClick(): void {
        console.log('zh:btnExtend.ts    onClick');
        this.handleSafe()
        if (this.openUI) UIMgr.open(this.openUI)
        if (this.closeUI) UIMgr.close(this.closeUI)
    }

    private handleSafe(): void {
        if (!this.isSafe) return
        this.isSafe = false
        this.clickEvents = this.btn.clickEvents
        console.log('zh:btnExtend.ts    handleSafe')
        this.btn.clickEvents = []
        this.scheduleOnce(() => {
            this.isSafe = true
            this.btn.clickEvents = this.clickEvents
        }, this.safeTime)
    }

    private handleClickSfx(): void {
        console.log('zh:btnExtend.ts    handleClickSfx');
        this.clickSfx && AudioMgr.playSfx(this.clickSfx)
    }
}


