import { _decorator, Component, Node, SkeletalAnimationComponent, AnimationComponent, loader, Prefab, CameraComponent, CCInteger, url } from 'cc';
import { GUIManager } from '../../engines/gui/GUIManager';
import { UIConst } from '../ui/UIConst';
import { GameController } from '../ui/fightings/GameController';
import { GameModel } from '../models/GameModel';
import GameConfigManager from '../models/GameConfigManager';
import { LoadingView } from '../../engines/loadingView/LoadingView';
const { ccclass, property } = _decorator;

@ccclass('FightingScene')
export class FightingScene extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;

    @property({
        type:AnimationComponent
    })
    leftHand:AnimationComponent=null;

    @property({
        type:AnimationComponent
    })
    rightHand:AnimationComponent=null;

    @property({
        type:CameraComponent
    })
    camrea:CameraComponent=null;

    @property({
        type:Node
    })
    LeftEndPos:Node=null;

    @property({
        type:Node
    })
    MiddleEndPos:Node=null;
    
    @property({
        type:Node
    })
    RightEndPos:Node=null;

    @property({
        type:Node
    })
    LeftStartPos:Node=null;

    @property({
        type:Node
    })
    MiddleStartPos:Node=null;
    
    @property({
        type:Node
    })
    RightStartPos:Node=null;
    
    /**
     * 资源引用
     */
    private assetsRef:Prefab[];

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

    private LoadAssets():void{
        let urls:string[]=[];
        //武器
        let weaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
        urls.push(weaponConfig.prefab);

        //栅栏
        let fenceConfig:any=GameConfigManager.GetFenceConfig(GameModel.single.currentFenceId);
        urls.push(fenceConfig.prefab);
        
        //本关用到的怪物资源
        let levelConfig:any=GameConfigManager.GetLevelConfig(GameModel.single.currentLevel);
        let monsterConfig:any;
        levelConfig.monsters.forEach(element => {
            monsterConfig=GameConfigManager.GetMonsterConfig(element.monsterId);
            if(urls.indexOf(monsterConfig.prefab)<0){
                urls.push(monsterConfig.prefab);
            }
        });

        //枪口火焰
        urls.push("d3d/gunFire/GunFire");

        //怪物血条
        urls.push("ui/components/MonsterHPUI");

        //加载
        loader.loadResArray(urls,Prefab,this.LoadAssetsProgress.bind(this),this.LoadAssetsComplete.bind(this));
    }

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

    private LoadAssetsComplete(error:Error,assets):void{
        if(error){
            console.error(error);
        }
        this.assetsRef=assets;
        GUIManager.single.Show(UIConst.FIGHTING_UI);
    }


    start () {
        // GameController.single.Init(this.leftHand,this.rightHand);
        
        // // Your initialization goes here.
        // GUIManager.single.Show(UIConst.FIGHTING_UI);

        this.LoadAssets();
    }

    update (deltaTime: number) {
        GameController.single.Update(deltaTime);
    }
}
