import { asyncDelay, onHomeworkFinish } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";
import pg from "./tool/pg";
import { GameNGT14 } from "./tool/Game";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {

    addPreloadImage() {
        // TODO 根据自己的配置预加载图片资源
        this._imageResList.push({ url: this.data.pic_url });
        this._imageResList.push({ url: this.data.pic_url_2 });
    }

    addPreloadAudio() {
        // TODO 根据自己的配置预加载音频资源
        this._audioResList.push({ url: this.data.audio_url });
    }

    addPreloadAnima() {

    }

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

    _cantouch = null;
    initData() {
        // 所有全局变量 默认都是null 
        this._cantouch = true;
        GameNGT14.getIns().initData(this.data);
        GameNGT14.getIns().reset();
        let data = GameNGT14.getIns().getCurrent();
        console.log('gamestart', data)
    }

    initView() {
        this.initBlocks();
    }

    initListener() {

    }
    private layout_blocks: cc.Node;
    initBlocks() {
        this.layout_blocks = pg.view.find(this, 'layout_blocks');
        this.layout_blocks.removeAllChildren()
        let block = pg.view.find(this, 'block');
        //动态创建，并给每一个打上编号
        let space = { x: 9 + 80, y: 10 + 80 }
        for (let y = 0; y < 9; y++) {
            for (let x = 0; x < 12; x++) {
                let node = cc.instantiate(block);
                let data = { x, y };
                node.data = data;
                node.x = -495 + space.x * x;
                node.y = 365 - space.y * y;
                node.active = !!GameNGT14.getIns().getBlockVal(x, y);
                this.layout_blocks.addChild(node);
            }
        }
    }
    gameStart() {
    }

    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);
            });
        })
    }
}
