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

//move方向事件茬点使用
const { ccclass, property } = cc._decorator;

enum MOVEDIR {
    UP,
    DOWN,
    LEFT,
    RIGHT,
}

@ccclass
export default class PageSwitchScript extends cc.Component {

    @property(cc.Integer)
    directvalue: number = 100;

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

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

    @property({ type: [cc.Component.EventHandler], tooltip: 'Switch回调' })
    switchCallBack: cc.Component.EventHandler[] = [];


    // LIFE-CYCLE CALLBACKS:
    touchStartPosi = null;
    contentIndex = 0;

    onLoad() {
        this.contentIndex = 0;
        for (let index = 0; index < this.content.children.length; index++) {
            if (index == this.contentIndex) {
                this.content.children[index].active = true;
            } else {
                this.content.children[index].active = false;
            }
        }

        if (!this.emitTarget) {
            this.emitTarget = this.node
        }
        this.openTouchEvent(this.emitTarget)

        this.node.attr({ scr: this });
    }

    start() {

    }

    cancelTouchEvent() {
        this.closeTouchEvent(this.emitTarget);
    }

    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
        let posi = event.getLocation()//世界坐标
        this.touchStartPosi = posi
        console.log('touchStartNode')

        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: false });
    }

    touchMoveNode(event) {
        let target = event.target
        let posi = event.getLocation()//世界坐标
    }

    touchEndNode(event) {
        let target = event.target
        let posi = event.getLocation()//世界坐标

        if ((posi.x - this.touchStartPosi.x) >= this.directvalue) {
            console.log('LEFT')
            this.switchView(MOVEDIR.LEFT)
        } else if ((this.touchStartPosi.x - posi.x) >= this.directvalue) {
            console.log('RIGHT')
            this.switchView(MOVEDIR.RIGHT)
        }

        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: true });
    }

    switchView(flag: MOVEDIR) {
        let preIndex = this.contentIndex;
        if (flag == MOVEDIR.RIGHT) {
            if (this.contentIndex < this.content.children.length - 1) {
                this.contentIndex++;
            }
        } else if (flag == MOVEDIR.LEFT) {
            if (this.contentIndex > 0) {
                this.contentIndex--;
            }
        }

        if (preIndex != this.contentIndex) {
            this.content.children[preIndex].active = false;
            this.content.children[this.contentIndex].active = true;
            for (const call of this.switchCallBack) {
                call.emit([preIndex, this.contentIndex]);
            }
        }
    }


    // update (dt) {}
}