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

const { ccclass, property } = cc._decorator;

@ccclass
export default class ZhuangBanMoveTouchScript extends cc.Component {
    @property(cc.Node)
    touchNode: cc.Node = null;

    @property(cc.Node)
    checkNode: cc.Node = null;

    @property(cc.Node)
    targetNode: cc.Node = null;

    @property(cc.ScrollView)
    scrollView: cc.ScrollView = null;



    touchMoveCheckData = null;

    onLoad() {
        if (!this.touchNode) {
            this.touchNode = this.node.getChildByName("touchNode");
        }
    }

    setTouchNode(data_) {
        this.touchMoveCheckData = data_;
        Common5.getSpriteFrameFromBundle("WordGame", data_.picPath, this.touchNode.getComponent(cc.Sprite));
        this.initTouchEvent(this.touchNode, data_.checkNode, data_.targetNode);
    }


    initTouchEvent(node, checkNode, targetNode) {
        var attrs = {
            checkNode: checkNode,
            recoveposi: node.getPosition(),//初始位置
            targetNode: targetNode,
        };
        node.attr(attrs);
        this.openTouchEvent(node)
    }


    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) {
        let target = event.target
        this.scrollView.enabled = false
    }

    touchMoveNode(event) {

        let posi = event.getLocation()//世界坐标
        let target = event.target
        posi = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi)

    }

    touchEndNode(event) {
        let target = event.target;
        this.scrollView.enabled = true
        if (target.checkNode.active && Common5.checkIntersectsBox(target, target.checkNode)) {
            Common5.playEffect('放置成功')
            target.checkNode.active = false;
            this.node.active = false;

            if (target.targetNode) {
                target.targetNode.active = true;
            }

            EventMgr.emitEvent_custom(ryw_Event.touchMoveCheck, { targetNode: this.touchMoveCheckData })
            console.log('checkNode++++++++++true')
        } else {
            Common5.playEffect('放置错误')
            if (target["recoveposi"]) {
                this.nodeMoveToRecovery(target, target["recoveposi"])
            }
        }

    }

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

}