import { asyncDelay, onHomeworkFinish } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {

    addPreloadImage() {
        // TODO 根据自己的配置预加载图片资源
    }

    addPreloadAudio() {
        // TODO 根据自己的配置预加载音频资源
    }

    addPreloadAnima() {

    }

    onLoadEnd() {
        // TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考
        this.initData();
        this.initView();
        this.initListener();
    }

    _cantouch = null;
    initData() {
        // 所有全局变量 默认都是null 
        this._cantouch = true;
    }

    initView() {
        this.initBg();
        this.initBtn();
    }

    initBg() {
    }


    initBtn() {

    }

    currentIndex = 0;
    leftMove() {
        const node = cc.find(`Canvas/bg/L${this.currentIndex+1}`);
        const nextNode = cc.find(`Canvas/bg/L${this.currentIndex+2}`);
        this._cantouch = false;
        cc.tween(node)
            .to(.3, { x: -4000 }, { easing: 'cubicInOut' })
            .call(() => {
                cc.tween(nextNode)
                    .to(.3, { x: 0 }, { easing: 'cubicInOut' })
                    .call(() => {
                        this.currentIndex ++;
                        cc.find(`Canvas/bg/right`).active = this.currentIndex<4;
                        cc.find(`Canvas/bg/left`).active = this.currentIndex>0;
                        this._cantouch = true;
                    })
                    .start();
            })
            .start();
    }

    rightMove() {
        const node = cc.find(`Canvas/bg/L${this.currentIndex+1}`);
        const beforeNode = cc.find(`Canvas/bg/L${this.currentIndex}`);
        this._cantouch = false;
        cc.tween(node)
            .to(.3, { x: 4000 }, { easing: 'cubicInOut' })
            .call(() => {
                cc.tween(beforeNode)
                    .to(.3, { x: 0 }, { easing: 'cubicInOut' })
                    .call(() => {
                        this.currentIndex --;
                        cc.find(`Canvas/bg/right`).active = this.currentIndex<4;
                        cc.find(`Canvas/bg/left`).active = this.currentIndex>0;
                        this._cantouch = true;
                    })
                    .start();
            })
            .start();
    }
    // update (dt) {},


    initListener() {
        this.node.on('touchstart', this.onEventStart, this);
        this.node.on('touchend', this.onEventEnd, this);
    }

    touchX;
    onEventStart(event) {
        const worldPoint = event.getLocation();
        this.touchX = worldPoint.x;
    }
    onEventEnd(event) {
        const worldPoint = event.getLocation();
        const x = worldPoint.x;
        if (x > this.touchX + 100) {
            if (this.currentIndex<=0) {
                // 已经是第一个了
                return;
            }
            console.log("======向右滑动=======");
            this.rightMove();

        } 
        if (x < this.touchX - 100) {
            if (this.currentIndex>=4) {
                // 已经是最后一个了
                return;
            }
            console.log("======向左滑动=======");
            this.leftMove()
        }
    }

    playLocalAudio(audioName) {
        const audio = cc.find(`Canvas/res/audio/${audioName}`).getComponent(cc.AudioSource);
        return new Promise((resolve, reject) => {
            const id = cc.audioEngine.playEffect(audio.clip, false);
            cc.audioEngine.setFinishCallback(id, () => {
                resolve(id);
            });
        })
    }
}
