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 XianSuoMoveTouchScript extends cc.Component {

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

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

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

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

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

    @property(cc.Node)
    checkNodes: cc.Node[] = [];

    private itemInfo = null;

    // onLoad() {}

    setTouchNodeIcon(itemInfo) {
        this.itemInfo = itemInfo;
        this.hideNode.active = true;
        this.showNode.active = false;
        console.log(itemInfo.url);
        Common5.getSpriteFrameFromBundle(itemInfo.bandleName, itemInfo.url, this.touchNode.getComponent(cc.Sprite));
    }

    showTouchNodeIcon() {
        this.hideNode.active = false;
        this.showNode.active = true;
        this.touchNode.active = true;

        this.node.parent.x = -this.node.x;
        cc.tween(this.showNode)
            .set({ color: cc.Color.YELLOW })
            .delay(0.5)
            .set({ color: cc.Color.WHITE })
            .start();
    }

    onTouchEvent(checkNodeSet: cc.Node[]) {
        var attrs = {
            checkNodeSet: checkNodeSet,
            recoveposi: this.touchNode.getPosition(),//初始位置
            recoveparent: this.touchNode.parent,
        };
        this.touchNode.attr(attrs);
        this.openTouchEvent(this.touchNode);
    }

    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 as cc.Node;
        this.scrollView.enabled = false;
        target.parent = this.moveNode;

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

    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;

        let checkNode = null;
        for (const iterator of target.checkNodeSet as cc.Node[]) {
            if (iterator.active && Common5.checkIntersectsBox(target, iterator)) {
                checkNode = iterator;
            }
        }

        if (checkNode) {
            target.active = false;
            if (checkNode.XianSuoBarScr) {
                var data_ = {
                    xianSuoIndex: this.node.parent.children.indexOf(this.node),
                    XianSuoMoveTouchScr: this,
                    itemInfo: this.itemInfo,
                };
                checkNode.XianSuoBarScr.addNode(data_);
            }
        }

        if (target["recoveposi"]) {
            if (target["recoveparent"]) {
                this.nodeMoveToRecovery(target, target["recoveposi"], target["recoveparent"]);
            } else {
                this.nodeMoveToRecovery(target, target["recoveposi"]);
            }
        }
    }

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

}