import { _decorator, Component, Node, sys,find, CCString, AudioSource, Button, EventHandler } from 'cc';
import { CarColorsGlobalInstance } from './CarColorsGlobalInstance';
import { ToastSysterm } from '../../UIFrame/ToastSysterm/ToastSysterm';
import { BundleCarColorsSysterm } from './Systerms/BundleCarColorsSysterm';
import { UICarColorsSysterm } from './Systerms/UICarColorsSysterm';
import { UINames } from './CarColorsGlobalTypes';
import { RoleCarColorsSysterm } from './Systerms/RoleCarColorsSysterm';
import { GlobalData, ConfigKeys } from '../../ScriptFrame/GlobalData';
import { CarCarColorsSysterm } from './Systerms/CarCarColorsSysterm';
import { PlatformManager } from '../../ScriptFrame/Frame/platformManager';
import { AudioManager } from '../../ScriptFrame/Frame/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('CarColorsGameEntry')
export class CarColorsGameEntry extends Component {
    @property(CCString)
    insADid: string = ""
    @property(CCString)
    rewardADid: string = ""
    @property(CCString)
    bannerADid: string = ""
    @property(CCString)
    bookId: string = ""

    async start() {
        find("UI/zombie-icon").on("click",()=>{
            PlatformManager.instance.goToMiniGame()
        })
        PlatformManager.adsInsId = this.insADid
        PlatformManager.adsVideoId = this.rewardADid
        PlatformManager.adsBannerId = this.bannerADid
        if (sys.platform === sys.Platform.WECHAT_GAME){
            wx.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
              })
        }
        this.initData()
        // 挂载toast弹窗
        CarColorsGlobalInstance.instance.toastSysterm = find("UI/ToastSysterm").getComponent(ToastSysterm)

        // 挂载资源包管理系统
        CarColorsGlobalInstance.instance.bundleSysterm = this.node.addComponent(BundleCarColorsSysterm)
        await CarColorsGlobalInstance.instance.bundleSysterm.loadBundles()
        
        // 挂载音频系统
        const clips = await Promise.all(CarColorsGlobalInstance.instance.bundleSysterm.loadAudioClips())
        AudioManager.instance.init(this.getComponent(AudioSource), clips)

        // 人物管理系统
        CarColorsGlobalInstance.instance.roleSysterm = this.node.addComponent(RoleCarColorsSysterm)
        await CarColorsGlobalInstance.instance.roleSysterm.loadRolePrefabs()

        // 汽车管理系统
        CarColorsGlobalInstance.instance.carSysterm = this.node.addComponent(CarCarColorsSysterm)
        
        // 挂载UI系统
        CarColorsGlobalInstance.instance.uiSysterm = this.node.addComponent(UICarColorsSysterm)
        await CarColorsGlobalInstance.instance.uiSysterm.loadUIPrefabs()
        
        CarColorsGlobalInstance.instance.uiSysterm.showUI(UINames.HomePage)
        find("UI/BundleLoading").active = false
    }

    initData(){
        let  gameSaveData: {level: number} = GlobalData.instance.getConfigData(ConfigKeys.GameSaveData)
        if (gameSaveData === null){
            gameSaveData = {
                level: 1
            }
        }
        GlobalData.instance.setConfigData(ConfigKeys.GameSaveData, gameSaveData)
    }
}


