import { asyncDelay, onHomeworkFinish } from "../script/util_dg29_museum";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent_dg29_museum";
import videoCtrl from "../script/ui/videoCtrl_DG16_dg29_museum";
import pg from "./pg_dg29_museum";
import dg29_museum_game from "./dg29_museum_game";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {

    // @property(cc.Node)
    // qustionLayer: cc.Node = null;

    // @property(cc.Node)
    // boyNode: cc.Node = null;

    @property(cc.Node)
    btn_next: cc.Node = null;

    @property(cc.Node)
    qustionLayerRoot: cc.Node = null;

    @property(cc.VideoPlayer)
    vplayer: cc.VideoPlayer = null;

    @property(cc.Node)
    videoCtrl: cc.Node = null;
    @property(cc.Node)
    touchlayer: cc.Node = null;



    // qustionCom: QuestionView = null;
    _totalIndex: any;
    _roundIndex: number;
    playGuide2: any;
    firstRound: boolean;
    doWrongTime: number;
    WrongList: any[];
    prepared: boolean;

    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_guide });
        this._audioResList.push({ url: this.data.audio_url_guide_2 });
        this._audioResList.push({ url: this.data.audio_url_right });
        this._audioResList.push({ url: this.data.audio_url_wrong });
        this._audioResList.push({ url: this.data.audio_url_end });
    }

    addPreloadAnima() {

    }

    @property(dg29_museum_game) root_game: dg29_museum_game = null;
    async onLoadEnd() {
        // TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考
        this.initData();
        this.initView();
        this.root_game.init(this.data);
    }

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

        this.touchlayer.on(cc.Node.EventType.TOUCH_START, this.tryPlayVideo, this)

        this.touchlayer._touchListener.setSwallowTouches(false)

        this.vplayer.node.active = true;
        this.vplayer.remoteURL = this.data.video;
        this.vplayer.resourceType = cc.VideoPlayer.ResourceType.REMOTE;
        this.vplayer.node.scale = 0.01

        pg.event.on('play_video', () => { this.playVideo() })
    }
    tryPlayVideo() {

        cc.log("touch layer--")
        if (this.prepared) {
            this.prepared = false;
            this.vplayer.volume = 0;
            this.vplayer.play();
            this.scheduleOnce(() => {
                this.vplayer.volume = 1;
                this.vplayer.pause();
            }, 1)
        }

    }
    initBg() {
        const bgNode = cc.find('Canvas/bg');
        bgNode.scale = this._mapScaleMax;
    }
    initView() {
        this.initBg();

        this.videoCtrl.active = false;

        this._roundIndex = 0;

        this.firstRound = true
        this.doWrongTime = 0;

        this.WrongList = [];
    }


    playVideo() {
        this.videoCtrl.active = true;
        this.node.getChildByName("bg").active = false;

        this.vplayer.node.active = true;
        this.videoCtrl.getComponent(videoCtrl).setVideCom(this.vplayer);
        this.vplayer.volume = 1;
        this.vplayer.node.scale = 1;
        this.vplayer.currentTime = 0;
        this.vplayer.play();
    }

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


    onVideoPlayerEvent(sender, event) {

        cc.log(event)
        if (event === cc.VideoPlayer.EventType.CLICKED) {

        }
        else if (event === cc.VideoPlayer.EventType.READY_TO_PLAY || event === cc.VideoPlayer.EventType.META_LOADED) {



        }
        else if (event === cc.VideoPlayer.EventType.PLAYING) {

            cc.log("=========")
            cc.log(sender);
            cc.log(sender._impl._h)
            cc.log(sender._impl._w);

            if (sender?._impl?._w) {
                this.vplayer.node.scale = cc.winSize.width / sender._impl._w;
            }

        }
        else if (event === cc.VideoPlayer.EventType.COMPLETED) {
            this.videoCtrl.active = false;
            this.node.getChildByName("bg").active = true;
            cc.log("video completed");
            this.vplayer.node.active = false;

            pg.event.emit('play_video_end')
        }
    }

}

