import { _decorator } from "cc";
import { TB, TbGEntity } from "../../config/data/schema";
import GNormalModeBase, { GNormalModePlayerEnum } from "./default/GNormalModeBase";
import { TD, app } from "../../App";
import GOnHookData from "../../data/GOnHookData";
import PlayerTacticalData from "../../data/PlayerTacticalData";
import PlayerPetData from "../../data/PlayerPetData";
import { GUI } from "../../ui/UIConfig";
import GBattleData from "../../data/GBattleData";
import GAttributeData from "../base/values/GAttributeData";
import JNFrameTime from "../../../../extensions/ngame/assets/ngame/sync/frame/game/time/JNFrameTime";
import { GAPI } from "../../consts/GAPI";
import { Node } from "cc";
import DungeonData from "../../data/DungeonData";
const { ccclass, property } = _decorator;

export interface GDungeonModeData{
    dungeonId:number,           //副本Id
    dungeonItemId:number,       //副本关卡Id
    data:TbGEntity.TDungeon     //副本信息
}

/**
 * 副本(默认) 模式
 */
@ccclass('GDungeonMode')
export default class GDungeonMode extends GNormalModeBase<{},GDungeonModeData>{

    onSyncInitSuccess(){
        if(!this.data) return;

        console.log("初始化 GDungeonMode 模式",this.data);
        super.onSyncInitSuccess();
        this.onUpdateWorld();

        //生成玩家宠物
        this.onGenPlayerPet();
        //生成Boss
        this.onGenBoss();
    }

    //更新地图
    onUpdateWorld(){
        let info = TD.TbGOnHookMaps.get(GOnHookData.getIns().info.onHookMap);
        this.setWorldMap(info.mapId);
    }

    onGenPlayerPet(){
        //获取玩家阵容
        let infos = PlayerTacticalData.getIns().getTacticalInfo();

        infos.forEach((petId,index) => {
            if(!petId) return;

            //获取要生成的宠物
            let info = PlayerPetData.getIns().petIdQueryPetInfo(petId);
            if(!info){
                app.layer.Open(GUI.Tips,{text:"未拥有当前上阵的宠物"});
                return;
            }
            let role = this.onGenRole(GNormalModePlayerEnum.PLAYER,index + 1,TD.TbGRole.get(info.petTbId));

            //添加宠物属性
            role.onEffectiveValue(GBattleData.getIns().data.getPetAttribute(petId));

        })

    }

    onGenBoss(){
        let enemy = this.onGenRole(GNormalModePlayerEnum.ENEMY,5,TD.TbGRole.get(this.data.data.boss));
        enemy.onEffectiveValue(GAttributeData.TAttributeValue(this.data.data.attributes))
    }

    //战斗结束
    onBattleEnd(win:GNormalModePlayerEnum){
        //结束游戏
        JNFrameTime.getInstance().setTimeout(async () => {
            if(win == GNormalModePlayerEnum.PLAYER){
                await DungeonData.getIns().challenge(this.data.dungeonId,this.data.dungeonItemId);
            }
            this.Close();
        },3000)
    }
    
}

