import { _decorator, assetManager, AudioClip, AudioSource, Component, Node } from 'cc';
import { attributes_data, scene_item_data, task_data } from '../../data/data';
import { config } from '../config';
import { game_run } from './game_run';
import { gameManager } from './gameManager';
import { tools } from '../tools';
import { ClientEvent } from '../clientEvent';
const { ccclass, property } = _decorator;

@ccclass('sceneManager')
export class sceneManager extends Component {
    private mSceneTask :task_data = null;
    private mPageList: scene_item_data[] = []; //每个分页场景
    protected mCurPage:number = 0; //当前场景分页
    private mGameManager:gameManager = null;
    private mSceneData: scene_item_data = null;
    public init(gm:gameManager){
        this.mGameManager = gm;
        ClientEvent.off(config.EventRun.TOGGLE_YIN_YUE,this.updateStatus.bind(this),this)
        ClientEvent.on(config.EventRun.TOGGLE_YIN_YUE,this.updateStatus.bind(this),this)
    }

    public updateStatus(){
        if(!gameManager.getUserData().isOpenYinYue){
            this.getSound().stop()
        }else{
            this.getSound().loop = true;
            this.getSound().play()
        }
    }

    public getSceneTask(){
        return this.mSceneTask;
    }

    public playMusic(path:string, loop: boolean){
        if(path.length<=0){
            return
        }
        let call_back = (err: any, clip: AudioClip)=> {
            if(!err){
                gameManager.mp3_cache.set(path,clip)
                this.playClip(clip); 
            }
        }
        if(gameManager.mp3_cache.get(path)){
            let clip = gameManager.mp3_cache.get(path);
            call_back(null,clip)
        }else{
            assetManager.loadRemote(path, call_back );
        }
    }
    public playClip (clip: AudioClip) {
        this.getSound().stop()
        this.getSound().loop = true;
        this.getSound().clip = clip;
        if(gameManager.getUserData().isOpenYinYue){
            this.getSound().play()
        }
    }

    public stopMusic(){
        this.getSound().pause()
    }

    public play_music(){
        if(this.getSound().clip!=null){
            this.playClip(this.getSound().clip); 
        }else{
            gameManager.Singleton.getSceneManager().playMusic(gameManager.getSysData().content.sys_piped_music,true)
        }
    
    }

    getSound(){
        if(this.node.getComponent(AudioSource)!=null){
            return this.node.getComponent(AudioSource)
        }
        return this.node.addComponent(AudioSource)
    }

    public getSceneData(){
        // if(this.mSceneData.att==null){
        //     if(this.mSceneData.page_list[0].page_list.length>0){
        //         return this.mSceneData.page_list[0].page_list[0];
        //     }
        //     return this.mSceneData.page_list[0];
        // }
        let data = this.mSceneData.page_list[0];
        // while (data.page_list.length>0) {
        //     data = data.page_list[0]
        // }
        return data;
    }

    public GetPageList():scene_item_data[]{
        return this.mPageList;
    }
    public startLevelGame(data: scene_item_data){
        this.mSceneTask = data._task_data;
        this.mPageList = data.page_list;
        this.mSceneData = data;
        if(data._task_data==null){
            return tools.showToast("场景任务没有配置！")
        }
        if(this.mPageList.length<=0){
            throw "不可以为空"
        }else{
            this.stopMusic()
            this.mGameManager.loadSceneTask(data._task_data)
            this.mGameManager.loadScene(this.mPageList,data.type)
            for (let index = 0; index < data.page_list.length; index++) {
                const element = data.page_list[index];
                this.mGameManager.loadUi(gameManager.getUIWidgetList(element))
                this.mGameManager.loadQuestion(gameManager.getQuestionwidgetList(element))
                this.mGameManager.loadTextSound(gameManager.getTextSoundWidgetList(element))
                this.mGameManager.initCountDownList(gameManager.getCountDownWidgetList(element))
            }
            this.mGameManager.initTaskUi(data._task_data)

            // this.mGameManager.loadScene(this.mPageList,data.type)
            // this.mGameManager.loadUi(gameManager.getUIWidgetList(data.page_list[this.mCurPage]))
            // this.mGameManager.loadQuestion(gameManager.getQuestionwidgetList(data.page_list[this.mCurPage]))
            // this.mGameManager.loadTextSound(gameManager.getTextSoundWidgetList(data.page_list[this.mCurPage]))
            // this.mGameManager.initTaskUi(data._task_data)
            // this.mGameManager.initCountDownList(gameManager.getCountDownWidgetList(data.page_list[this.mCurPage]))
        }
    }
}


