import { Component, find, game, JsonAsset, loader, Node, _decorator } from 'cc';
import { LoadingView } from '../../engines/loadingView/LoadingView';
import { SceneManager } from '../../engines/scenes/SceneManager';
import { SubPackageLoader } from '../../engines/utils/SubPackageLoader';
import { LiangLiangSDK } from '../../libs/liangliangSDK';
import { NotPlatform } from '../../Platform/NotPlatform';
import { PlatformManager } from '../../Platform/PlatformManager';
import { WeChatPlatform } from '../../Platform/WeChat/WeChatPlatform';
import GameConfigManager from '../models/GameConfigManager';
import { GameModel } from '../models/GameModel';
const { ccclass, property } = _decorator;

@ccclass('PreloadingScene')
export class PreloadingScene extends Component {

    onLoad(){
        console.log("进入Preloading场景");
    }

    start () {
        let canvas:Node=find("Canvas");
        game.addPersistRootNode(canvas);
        if(window["wx"]){
            this.LoadSubPackage();
            PlatformManager.init(new WeChatPlatform())
        }else{
            this.LoadConfigs();
            PlatformManager.init(new NotPlatform())
        }
        PlatformManager.login(()=>{},()=>{});
    }
    
    private initAds():void{
        LiangLiangSDK.Init(()=>{
            console.log("平台初始化成功");
            this.AdsComplete();
        },()=>{
            console.log("平台初始化失败");
            this.AdsComplete();
        })
    }

    private AdsComplete():void{
        GameModel.single.gameName="QiangShenZhanJi";
        GameModel.single.userId=LiangLiangSDK.userID;
        this.LoadConfigs();
    }

    private LoadSubPackage():void{
        console.log("加载分包");
        LoadingView.single.UpdateLabel("加载分包");
        SubPackageLoader.load(["ui","barriers","gunFire","hands","monsters","scenes","textures","weapons"],(progress:number)=>{
            LoadingView.single.UpdateProgress(progress);
        },()=>{
            LoadingView.single.UpdateLabel("拉取平台数据");
            this.initAds();
        },(errMsg:string)=>{
            LoadingView.single.UpdateLabel(errMsg);
        })
    }

    LoadConfigs():void{
        LoadingView.single.UpdateLabel("加载配置");
        loader.loadResDir("configs",JsonAsset,this.onConfigLoadProgress.bind(this),this.onConfigLoadComplete.bind(this));
    }

    onConfigLoadProgress(index:number,total:number):void{
        LoadingView.single.UpdateProgress(index/total);
    }

    onConfigLoadComplete(error,asset):void{
        if(error!=null){
            LoadingView.single.UpdateLabel("加载配置出错!");
            return;
        }
        GameConfigManager.Init(asset);
        this.initLocalData();
    }
    
    private initLocalData():void{
        // //清除本地数据
        GameModel.single.ClearLocalData();
        GameModel.single.ReadByLocal();
        SceneManager.single.Swicth("PrepareScene");
    }
    
    // //假进度条
    // private time:number=0;
    // update (deltaTime: number) {
    //     this.time+=deltaTime;
    //     this.time=this.time%2;
    //     LoadingView.single.UpdateProgress(this.time/3);
    // }
}
