import EventMgr from "./EventMgr";

cc.Class({
    extends: cc.Component,

    properties: {
        labWord: cc.Label,
		audDrop:cc.AudioClip,
    },

    onLoad() {
        this.node.on('touchmove', this.onTouchMove, this);
        this.node.on('touchend', this.onTouchEnd, this);
        this.node.on('touchcancel', this.onTouchEnd, this);

        EventMgr.on('onTouchmove', this.onMove, this);
        EventMgr.on('onBlockInput', this.onBlockInput, this);
        EventMgr.on('onCancelBlockInput', this.onCancelBlockInput, this);
    },
    onDestroy() {
        this.node.off('touchmove', this.onTouchMove, this);
        this.node.off('touchend', this.onTouchEnd, this);
        this.node.off('touchcancel', this.onTouchEnd, this);

        EventMgr.off('onTouchmove', this.onMove, this);
        EventMgr.off('onBlockInput', this.onBlockInput, this);
        EventMgr.off('onCancelBlockInput', this.onCancelBlockInput, this);
    },

    init(index, data, scene) {
        this.scene = scene;
        this.index = index;
        this.data = data;
        this.labWord.string = data.text;
        this.blockInput = false;
        this.scheduleOnce(() => {
            this.canCollision = true;
        })
    },

    onBlockInput() {
        this.blockInput = true;
    },
    onCancelBlockInput() {
        this.blockInput = false;
    },

    onMove(eventName, params) {
        if (this.isActive) {
            return;
        }
        if (params.x > 0) {
            if (this.index != params.index + 1) {
                return;
            }

        } else {
            if (this.index != params.index - 1) {
                return;
            }
        }
        let item = this;
        let itemList = [item];
        while (item.leftItem) {
            item = item.leftItem;
            itemList.push(item);
        }
        item = this;
        while (item.rightItem) {
            item = item.rightItem;
            itemList.push(item);
        }

        for (let i = 0; i < itemList.length; i++) {
            itemList[i].node.x -= params.x;
        }
    },

    onTouchMove(event) {
        let delta = event.getDelta();
        if (Math.abs(delta.x) > 5) {
            this.hadMove = true;
        }
        if (this.blockInput) {
            return;
        }
        this.isActive = true;

        let item = this;
        let itemList = [item];
        while (item.leftItem) {
            item = item.leftItem;
            itemList.unshift(item);
        }
        item = this;
        while (item.rightItem) {
            item = item.rightItem;
            itemList.push(item);
        }

        for (let i = 0; i < itemList.length; i++) {
            let pos = itemList[i].node.position.add(delta);
            itemList[i].node.x = pos.x;
        }
        if (delta.x > 0) {
            EventMgr.emit('onTouchmove', { x: delta.x, index: itemList[itemList.length - 1].index });
        } else {
            EventMgr.emit('onTouchmove', { x: delta.x, index: itemList[0].index });
        }
    },
    playAudioDrop() {
		GameData.audioCount++;
		let id = cc.audioEngine.play(this.audDrop, false, 1);
		cc.audioEngine.setFinishCallback(id, () => {
			GameData.audioCount--;
		})
	},
    onTouchEnd() {
        if (!this.hadMove) {
            this.playAudio();
        }else{
            this.playAudioDrop();
        }
        this.hadMove = false;
        this.isActive = false;
        this.blockInput = false;
        EventMgr.emit('onCancelBlockInput');
    },

    onCollisionEnter: function (other, self) {
        if (!this.canCollision) {
            return;
        }
        EventMgr.emit('onBlockInput');
        let otherItem = other.node.getComponent('ItemPart');
        let selfItem = self.node.getComponent('ItemPart');
        if (otherItem.index > this.index) {
            this.rightItem = otherItem;
            otherItem.leftItem = this;
        } else {
            this.leftItem = otherItem;
            otherItem.rightItem = this;
        }
        this.correctPosition(otherItem, selfItem);

        this.showEffect(otherItem, selfItem);

        this.checkDone();
    },

    // 修正坐标
    correctPosition(otherItem, selfItem) {
        if (otherItem.index < selfItem.index) {
            return;
        }
        let item = otherItem;
        let itemList = [];
        let startX = otherItem.node.x;
        if (otherItem.index > selfItem.index) {
            while (item.leftItem) {
                item = item.leftItem;
                itemList.push(item);
            }
            for (let i = 0; i < itemList.length; i++) {
                itemList[i].node.x = startX - 184 * (i + 1);
            }
        } else {
            while (item.rightItem) {
                item = item.rightItem;
                itemList.push(item);
            }
            for (let i = 0; i < itemList.length; i++) {
                itemList[i].node.x = startX + 184 * (i + 1);
            }
        }
    },

    showEffect(otherItem, selfItem) {
        if (otherItem.index < selfItem.index) {
            return;
        }
        this.scene.showCollisionEffect(cc.v2(otherItem.node.x - 92, otherItem.node.y));
    },

    checkDone() {
        let item = this;
        let itemList = [item];
        while (item.leftItem) {
            item = item.leftItem;
            itemList.push(item);
        }
        item = this;
        while (item.rightItem) {
            item = item.rightItem;
            itemList.push(item);
        }
        let x = 0;
        for (let i = 0; i < itemList.length; i++) {
            x += itemList[i].node.x;
        }
        let pos = cc.v2(x / itemList.length, this.node.y);
        this.scene.checkDone(itemList.length, pos);
    },

    playAudio() {
        GameData.audioCount++;
        let url = this.data.optionAudio;
        cc.assetManager.loadRemote(url, null, (err, clip) => {
            if (err) {
                console.log(err)
                return;
            }
            let id = cc.audioEngine.play(clip, false, 1);
            cc.audioEngine.setFinishCallback(id, () => {
                GameData.audioCount--;
            })
        });
    },

    // update (dt) {},
});
