import { _decorator, Button, Component, EventHandler, js, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('NewComponent')
export class NewComponent extends Component {
    @property(Node)
    touchnod:Node = null;
    @property(Node)
    buttonnod:Node = null;

    protected onLoad(): void {
        this.AddButtonSound()
        this.touchnod.on(Node.EventType.TOUCH_END,this.nodeclick,this)
        // this.touchnod.on(Node.EventType.TOUCH_END,this.nodeclick,this)
        // this.touchnod.on(Node.EventType.TOUCH_END,this.nodeclick,this)
    }
    start() {

    }

    update(deltaTime: number) {
        
    }
    buttonclick(){
        console.log("按钮被点击了")
    }
    nodeclick(){
        console.log("节点被点击了")
    }

    addColseEvent() {
        let mask = this.buttonnod;//.getChildByName("mask");
        if (mask) {
            // this.node.on(Node.EventType.TOUCH_END,this.CloseUI,this)


            let button = mask.getComponent(Button)
            if (!button) {
                button = mask.addComponent(Button)
            }

            let name = js.getClassName(this)
            const clickEventHandler = new EventHandler();
            clickEventHandler.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
            clickEventHandler.component = name;// 这个是脚本类名
            clickEventHandler.handler = 'buttonclick';
            button.clickEvents.push(clickEventHandler);
        }

    }
    AddButtonSound() {
        if (Button.prototype["touchEndedClone"]) return;
        Button.prototype["touchEndedClone"] = Button.prototype["_onTouchEnded"];

        if (Button.prototype["touchBeganClone"]) return;
        Button.prototype["touchBeganClone"] = Button.prototype["_onTouchBegan"];

        Button.prototype["_onTouchBegan"] = function (event) {
            
            // console.log("点击声音------1")
            // console.log("点击声音------1 this.interactable  "+this.interactable)
            // console.log("点击声音------1 this.enabledInHierarchy "+this.enabledInHierarchy)
            if (this.interactable && this.enabledInHierarchy) {
                // console.log("点击声音------2")
                // 播放自己的按钮音效

                // AudioMgr.SoundPlay(bundle, url);
                if(this['useCustomSound']){
                    // console.log("点击声音------3")
                }else{
                    // console.log("点击声音------4")
                    // mGl.audio.playSound(mGl.audioPath.btnClick)
                }
            }
            // console.log("点击声音------5")
            this.touchBeganClone(event);

        }

        Button.prototype["_onTouchEnded"] = function (event) {
            if (this.node['ClickTime']) {
                if ((new Date().getTime() - this.node['ClickTime']) < 500) {
                    // this["_zoomBack"] && this["_zoomBack"]();

                    this._pressed = false;
                    this._updateState();

                    if (event) {
                        event.propagationStopped = true;
                    }

                    // console.log("点击间隔")
                    return
                }
            }
            this.node['ClickTime'] = new Date().getTime();
            if (this.interactable && this.enabledInHierarchy) {

                // 播放自己的按钮音效

                // AudioMgr.SoundPlay(bundle, url);
                // if(this['useCustomSound']){

                // }else{
                //     mGl.audio.playSound(mGl.audioPath.btnClick)
                // }
            }

            this.touchEndedClone(event);

        }

    }
}


