import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import GameReport from "../../../FrameWork/Report/ZyZyReport";
import Common5 from "../../../Platform/th/Common5";



const { ccclass, property } = cc._decorator;

@ccclass
export default class ANodeClickScript extends cc.Component {

    @property({
        type: cc.Node,
        displayName: '触发节点'
    })
    emitTarget: cc.Node = null

    @property({
        displayName: '是否隐藏触发节点'
    })
    isEmitTargetHide: boolean = false

    @property({
        displayName: '点击次数'
    })
    emitTimes: number = 1;

    @property({
        type: [cc.Node],
        displayName: '需展示的节点'
    })
    tiggerNodeArray: cc.Node[] = []

    @property({
        type: [cc.Node],
        displayName: '需隐藏的节点'
    })
    hideTirNodeArray: cc.Node[] = []

    @property({
        type: cc.Node,
        displayName: '允许触发节点',
        tooltip: '该节点active为true时才能触发事件',
    })
    allowEmitNode: cc.Node = null

    @property({
        displayName: '是否触发动画'
    })
    isSpineAnim: boolean = false

    @property({
        displayName: '动画名'
    })
    spineAnim: string = ''

    @property({
        type: cc.Node,
        displayName: '动画节点'
    })
    spineNode: cc.Node = null

    @property({
        type: cc.Node,
        displayName: '遮罩节点',
        tooltip: '触发动画时防点击遮罩节点',
    })
    maskNode: cc.Node = null

    @property({
        displayName: '隐藏动画',
        tooltip: '动画播放完毕后是否隐藏',
    })
    isSpineNodeHide: boolean = false

    @property({
        type: cc.AudioSource,
        displayName: '音效'
    })
    effectUrl: cc.AudioSource = null

    @property({
        type: [cc.Node],
        displayName: '绑定节点'
    })
    attrNodeArray: cc.Node[] = []

    @property({
        displayName: '是否发送通知'
    })
    isReport: boolean = true

    // LIFE-CYCLE CALLBACKS:

    curEmitTimes = 0;

    onLoad() {
        if (!this.emitTarget) {
            this.emitTarget = this.node
        }
        this.openTouchEvent(this.emitTarget)
        this.curEmitTimes = 0;
    }

    start() {

    }


    openTouchEvent(node) {
        node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.on(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    closeTouchEvent(node: cc.Node) {
        node.off(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    touchStartNode(event) {
        // Common5.playEffect("点击音效");
        let target = event.target
        console.log('touchStartNode')
        this.addEmitTimes();
    }

    addEmitTimes() {
        if (this.curEmitTimes == 0) {
            this.curEmitTimes++;
            this.scheduleOnce(() => {
                this.curEmitTimes = 0;
            }, 2);
        } else {
            this.curEmitTimes++;
        }
    }

    touchMoveNode(event) {
    }

    touchEndNode(event) {
        if (this.curEmitTimes < this.emitTimes) {
            return;
        }

        let target = event.target;

        if (!this.allowEmitNode || this.allowEmitNode.active) {
            if (this.effectUrl) {
                this.effectUrl.play()
            }

            if (this.isEmitTargetHide) {
                target.active = false;
            }

            let callbacks = () => {
                for (const iterator of this.tiggerNodeArray) {
                    iterator.active = true;
                }

                for (const iterator of this.hideTirNodeArray) {
                    iterator.active = false;
                }

                if (this.maskNode) {
                    this.maskNode.active = false;
                }

                if (this.isSpineNodeHide && this.spineNode) {
                    this.spineNode.active = false;
                }

                target.attrNodeArray = this.attrNodeArray;

                if (this.isReport && Common5.selectGameInfo) {
                    //GameReport.BtnsReport(target.name, Common5.selectGameInfo.titleUrl);

                    console.log("[GameReport]++++++++++++++++++++++>" + target.name);
                }

                EventMgr.emitEvent_custom(ryw_Event.NormalTouchEndCheck, { targetNode: target })
            }

            if (this.isSpineAnim) {

                if (this.maskNode) {
                    this.maskNode.active = true;
                }

                this.spineNode.active = true
                this.spineNode.getComponent(sp.Skeleton).setAnimation(0, this.spineAnim, false)
                this.spineNode.getComponent(sp.Skeleton).setCompleteListener(callbacks);
            } else {
                callbacks();
            }

        }

    }

    nodeMoveToRecovery(node, oldPosi: cc.Vec2, func?: Function) {
        cc.tween(node)
            .to(0.1, { x: oldPosi.x, y: oldPosi.y })
            .call(() => {

                if (func) {
                    func()
                }
            })
            .start()
    }



    // update (dt) {}
}