import { getSpriteFrimeByUrl, goNextPage, onHomeworkFinish } from "../script/util_DG25";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent_DG25";
import Boy from "../script/items/boy_DG25";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {
    @property(cc.Node)
    boyNode: cc.Node = null;
    @property(cc.Node)
    bgIcon: cc.Node = null;
    @property(cc.Node)
    rect: cc.Node = null;
    @property(cc.Node)
    box: cc.Node = null;
    @property(cc.Node)
    btn_next: cc.Node = null;
    _bgIconUrl: any;
    _item_1_data: any;
    _item_2_data: any;
    _itemOffsetx: any;
    _itemOffsety: any;
    private _itemList: any;

    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.start_audio_url });
        this._audioResList.push({ url: this.data.end_audio_url });
    }

    addPreloadAnima() {

    }

    async onLoadEnd() {

        cc.log(JSON.stringify(this.data, null, 2))
        // TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考
        this.initData();
        this.initView();
        this.initListener();
    }

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

    initView() {
        this.initBg();
        this.initPageData();
        this.initPageView();
        this.boyNode.getComponent(Boy).playtalk(-1);
        this.playAudioByUrl(this.data.start_audio_url, () => {
            this.boyNode.getComponent(Boy).playNormal();
        })

    }

    initBg() {
        const bgNode = cc.find('Canvas/bg');
        bgNode.scale = this._mapScaleMax;
        this.btn_next.on("click", () => {
            cc.audioEngine.stopAllEffects();
            goNextPage()
        })
    }

    initPageData() {

        this._bgIconUrl = this.data.bgItem.url;

        this._itemList = this.data.hotZoneItemArr || [];


    }

    initPageView() {
        this.btn_next.active = false;
        if (this.data.end_btn_text) {
            this.btn_next.active = true;
            this.btn_next.getChildByName("lb").getComponent(cc.Label).string = this.data.end_btn_text;
        }

        this.rect.removeAllChildren();
        if (this._bgIconUrl) {
            getSpriteFrimeByUrl(this._bgIconUrl, (spf) => {
                this.bgIcon.getComponent(cc.Sprite).spriteFrame = spf;
                this.bgIcon.width = this.data.bgItem.rect.width;
                this.bgIcon.height = this.data.bgItem.rect.height;
                this.rect.getComponent(cc.Widget).updateAlignment();
                this.bgIcon.scale = Math.min(1080 / this.bgIcon.width, 660 / this.bgIcon.height)
            })
        }

        let doneAudioIndex = 0;
        for (let i = 0; i < this._itemList.length; ++i) {

            let itemInfo = this._itemList[i];
            let item = cc.instantiate(this.box);
            item.parent = this.rect;
            item.width = itemInfo.rect.width;
            item.height = itemInfo.rect.height;
            item.x = itemInfo.rect.x + itemInfo.rect.width / 2;
            item.y = -itemInfo.rect.y - itemInfo.rect.height / 2;
            item.addComponent(cc.Button);
            cc.log("item scale == " + item.scale)
            item.on("click", () => {
                cc.audioEngine.stopAllEffects();
                this.boyNode.getComponent(Boy).playNormal();
                this.rect.children.forEach((val) => {
                    val.stopAllActions();
                    val.scale = 1;
                })
                if (!item["doneAudio"]) {
                    item["doneAudio"] = true;
                    doneAudioIndex++;
                }
                cc.tween(item).to(0.5, { scale: 1.05 }).to(0.5, { scale: 1 }).start()
                this.playAudioByUrl(itemInfo.audio_url, () => {
                    this.ckeckEnd(doneAudioIndex);
                })
            })

            if (itemInfo.pic_url) {
                item.getChildByName("icon").active = true;
                getSpriteFrimeByUrl(itemInfo.pic_url, (spf) => {
                    item.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = spf;
                    item.getChildByName("icon").scale = itemInfo.imgScale;
                    // this.bgIcon.scale = Math.min(1100 / this.bgIcon.width, 660 / this.bgIcon.height)
                })
            } else {
                item.getChildByName("icon").active = false;
            }
            if (itemInfo.text) {
                item.getChildByName("lb").active = true;
                item.getChildByName("lb").getComponent(cc.Label).string = itemInfo.text;
            } else {
                item.getChildByName("lb").active = false;
            }
        }
    }

    ckeckEnd(nowIndex) {

        if (nowIndex == this._itemList.length) {
            cc.log("donve");
            this.boyNode.getComponent(Boy).playtalk(-1);
            this.playAudioByUrl(this.data.end_audio_url, () => {
                this.boyNode.getComponent(Boy).playNormal();
            })
        }

    }




    initListener() {

    }

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