import { assetManager, AudioSource, find, Node, _decorator } from 'cc';
import { BaseLayer } from '../common/BaseLayer';
import { cocosUtil } from '../utils/cocosUtil';
import { constants } from './data/constants';
import { localText } from './data/localText';
import { msgac } from './data/msgac';
import { audioManager } from './manager/audioManager';
import { designManager } from './manager/designManager';
import { iconManager } from './manager/iconManager';
import { layerManager } from './manager/layerManager';
import { levelManager } from './manager/levelManager';
import { resManager } from './manager/resManager';
import { skillManager } from './manager/skillManager';
import { playerModel } from './model/playerModel';
const { ccclass, property } = _decorator;

@ccclass('GameLaunch')
export class GameLaunch extends BaseLayer {

    onLoad() {
        cocosUtil.init();

        super.onLoad();

        let btnTest = this.getNodeByPath("btnTest");
        if (cocosUtil.isDesktopBrowser()) {
            btnTest.active = true;
        } else {
            btnTest.active = false;
        }

        let layer1 = find("layer1", this.node);
        let layerHint = find("layerHint", this.node);
        let audioSource = this.node.getComponent(AudioSource);

        layerManager.instance.init(layer1, layerHint);
        audioManager.instance.init(audioSource);
        playerModel.instance.init();

        // 多语言适配
        localText.init();

        let loadingName = constants.layers.LoadingLayer.name;
        this.getNodeByPath(loadingName).addComponent(loadingName);

        let noticeName = constants.layers.NoticeLayer.name;
        find(noticeName, layerHint).addComponent(noticeName);
    }

    onEnable() {
        this.addEventArr([
            msgac.openLayer,
            msgac.showNotice,
        ]);
    }

    onDisable() {
        this.removeEventArr([
            msgac.openLayer,
            msgac.showNotice,
        ]);
    }

    openLayerRet(data) {
        this.openLayer(constants.layers[data.name], data);
    }

    showNoticeRet(data) {
        this.createNotice(data);
    }

    process_event(event) {
        let ac = event.ac;
        let data = event.data;
        switch (ac) {
            case msgac.showNotice:
                this.showNoticeRet(data);
                break;
            case msgac.openLayer:
                this.openLayerRet(data);
                break;
            default:
                break;
        }
    }

    onButtonClick(node: Node, name: string) {
        switch (name) {
            case "btnTest":
                this.onClickBtnTest(node);
                break;
            default:
                break;
        }
    }

    onClickBtnTest(node: Node) {
        console.log(assetManager);
        console.log(resManager.instance);
        console.log(designManager.instance);
        console.log(levelManager.instance);
        console.log(audioManager.instance);
        console.log(iconManager.instance);
        console.log(skillManager.instance);
    }

}

