cc.Class({
    extends: cc.Component,

    properties: {
        pfbLetter: cc.Prefab,
        audClick: cc.AudioClip,
    },

    onLoad() {
        this.layerLetters = this.node.getChildByName('letters');
        this.button = this.node.getChildByName('button');
        this.button.on('click', this.onClick, this);
    },

    setData(data, index) {
        this.data = data;
        this.index = index;
        this.initLetters();
        this.initLayerLettersScale();
    },

    initLetters() {
        for (let i = 0; i < this.data.text.length; i++) {
            let node = cc.instantiate(this.pfbLetter);
            node.parent = this.layerLetters;
            let colorType = this.index % 4;
            let color = 'normal';
            if (colorType == 0) {
                color = 'yellow';
            } else if (colorType == 1) {
                color = 'green';
            } else if (colorType == 2) {
                color = 'red';
            } else if (colorType == 3) {
                color = 'blue';
            }
            let letter = this.data.text.charAt(i);
            if (this.data.letterIndex == i) {
                node.getComponent('ItemLetter').setData(color, letter);
            } else {
                node.getComponent('ItemLetter').setData('normal', letter);
            }
        }
        this.layerLetters.getComponent(cc.Layout).updateLayout();

        if (this.data.text.length <= 7) {
            return;
        }
        let maxWidth = 107.45;
        let scale = maxWidth / this.layerLetters.width;
        if (!window.minWordScale) {
            window.minWordScale = scale;
        } else if (scale < window.minWordScale) {
            window.minWordScale = scale;
        }
    },

    initLayerLettersScale() {
        this.scheduleOnce(() => {
            this.layerLetters.scale = window.minWordScale;
        })
    },

    onClick() {
        if (window.isPlayEnter) {
            return
        }
        if (!window.isTrainMove) {
			return;
		}
        cc.audioEngine.play(this.audClick, false, 1);

        let event = new cc.Event.EventCustom('stopMove', true);
        event.setUserData(this.index);
        this.node.dispatchEvent(event);
        // this.data.shortAudio_url = 'http://127.0.0.1/iamflying.mp3';
        cc.assetManager.loadRemote(this.data.shortAudio_url, null, (err, clip) => {
            if (err) {
                console.log(err)
                return;
            }
            let id = cc.audioEngine.play(clip, false, 1);
            let duration = cc.audioEngine.getDuration(id);
            cc.tween(this.layerLetters)
                .call(() => {
                    for (let i = 0; i < this.layerLetters.children.length; i++) {
                        let node = this.layerLetters.children[i];
                        node.getComponent('ItemLetter').setRed();
                    }
                })
                .to(0.3, { scale: window.minWordScale * 1.1 })
                .to(0.3, { scale: window.minWordScale })
                .call(() => {
                    for (let i = 0; i < this.layerLetters.children.length; i++) {
                        let node = this.layerLetters.children[i];
                        node.getComponent('ItemLetter').setOldColor();
                    }
                })
                .start();
            this.scheduleOnce(() => {
                let event = new cc.Event.EventCustom('showResult', true);
                event.setUserData(this.index);
                this.node.dispatchEvent(event);
            }, Math.max(duration, 0.8))
        });
    },
    // update (dt) {},
});
