import { Vec2 } from "cc";
import GDefaultMode from "./default/GDefaultMode";
import { TB } from "../../config/data/schema";
import GRoleDefault from "../base/role/GRoleDefault";
import { _decorator } from "cc";
import GBaseMode from "../GBaseMode";
import { Prefab } from "cc";
import { instantiate } from "cc";
import GRoleCGCrystal from "../base/role/CampGuardian/GRoleCGCrystal";
import { TD, app } from "../../App";
import { GTowards } from "../base/GObject";
import GRoleBase from "../base/role/GRoleBase";
import { GTactical } from "../entity/GTactical";
import JNFrameTime from "../../../../extensions/ngame/assets/ngame/sync/frame/game/time/JNFrameTime";
import { TbGPetId } from "../../config/TbGPet";
import GModeTools from "./GModeTools";
import GPetAttribute from "../base/values/attribute/role/GPetAttribute";
import RandomUtil from "../../../../extensions/ngame/assets/ngame/util/RandomUtil";
import { GModeEvent } from "./GMode";
import { Camera } from "cc";
const { ccclass, property } = _decorator;

//阵营守护 角色
export enum GCampGuardianEnum{
    PLAYER, //玩家
    ENEMY, //敌人
}

//阵营守护
//玩家派兵攻击对方阵营 游戏参考
@ccclass('GCampGuardianMode')
export default class GCampGuardianMode extends GBaseMode<{},{}>{

    //玩家水晶位置
    playerPos: Vec2 = new Vec2(-800,-50);
    //敌方水晶位置
    enemyPos: Vec2 = new Vec2(800,-50);

    //我方水晶
    
    //敌方水晶

    //水晶预制体
    @property(Prefab)
    crystalPrefab: Prefab;

    //角色预制体
    @property(Prefab)
    petPrefab: Prefab;

    //玩家宠物
    playerRoles: GRoleBase<{}>[] = [];
    //敌方宠物
    enemyRoles: GRoleBase<{}>[] = [];

    //模式相机
    @property(Camera)
    modeCamera:Camera;

    onSyncInitSuccess(){
        
        //生成水晶
        this.onGenCrystal(GCampGuardianEnum.PLAYER);
        this.onGenCrystal(GCampGuardianEnum.ENEMY);

        //定时器生成
        JNFrameTime.getInstance().setInterval(() => {
            let max = TD.TbGRole.getDataList().length;
            this.onGenRole(GCampGuardianEnum.PLAYER,TD.TbGRole.getDataList()[Math.floor(this.getSync().SyncRandomInt(0,max - 1))]);
            this.onGenRole(GCampGuardianEnum.ENEMY,TD.TbGRole.getDataList()[Math.floor(this.getSync().SyncRandomInt(0,max - 1))]);
        },2000)

    }

    //生成水晶
    onGenCrystal(type:GCampGuardianEnum){

        let crystalNode = instantiate(this.crystalPrefab);
        let crystal = crystalNode.getComponent(GRoleCGCrystal);
        crystal.init(TD.TbGRole.get(TbGPetId.雷吉艾斯));

        switch(type){
            case GCampGuardianEnum.PLAYER:
                this.addGObject(crystal,this.playerPos)
                break;
            case GCampGuardianEnum.ENEMY:
                crystal.setTowards(GTowards.LEFT)
                this.addGObject(crystal,this.enemyPos)
                break;
        }

    }

    //生成宠物
    onGenRole(type:GCampGuardianEnum,info:TB.TbGRole):GRoleDefault {

        let tactical = GTactical.getTactical();
        let pos:Vec2;
        
        switch(type){
            case GCampGuardianEnum.PLAYER:
                tactical.setOffset(this.playerPos);
                pos = tactical.getPosXYPosition(1,1,this.playerPos);
                break;
            case GCampGuardianEnum.ENEMY:
                tactical.setOffset(this.enemyPos);
                pos = tactical.getPosXYPosition(1,1,this.enemyPos);
                break;
        }


        if(!pos) return;
        let role = instantiate(this.petPrefab);

        let entity = role.getComponent(GRoleDefault);
        //初始化
        entity.onInit(type,info,tactical,tactical.getPosXY(1,1));

        //绑定寻敌
        entity.onQueryEunmy = () => {
            return GModeTools.getNearbyEnumy(entity,this.getOnesRoleAlive(type == GCampGuardianEnum.PLAYER ? GCampGuardianEnum.ENEMY : GCampGuardianEnum.PLAYER));
        }

        //绑定死亡回调
        entity.addKillBackEvent(this.onRoleKillBack.bind(this))

        //添加宠物属性
        entity.onEffectiveValues(new GPetAttribute({
            petId:info.id,
            petPlayerId:0,
            petTbId:info.id,
            petLevel:0,
            petStar:0,
            petStarExp:0,
        }));
        //绑定受击回调
        entity.addHitCallback(this.onHitBack.bind(this));

        this.addGObject(entity,pos);
        this.getOnesRole(type).push(entity);
        return entity;

    }

    //获取阵营宠物
    getOnesRole(type: GCampGuardianEnum):GRoleBase<{}>[]{
        if(type == GCampGuardianEnum.PLAYER) return this.playerRoles;
        if(type == GCampGuardianEnum.ENEMY) return this.enemyRoles;
    }

    //获取存活的宠物
    getOnesRoleAlive(type: GCampGuardianEnum):GRoleBase<{}>[]{
        if(type == GCampGuardianEnum.PLAYER) return this.playerRoles.filter(role => !!role.get());
        if(type == GCampGuardianEnum.ENEMY) return this.enemyRoles.filter(role => !!role.get());
    }

    //角色死亡回调
    onRoleKillBack(role:GRoleDefault){

        //死亡销毁
        JNFrameTime.getInstance().setTimeout(() => {
            if(role.isValid)
                role.node.destroy()
        },3000)

    }

    //角色受击回调
    onHitBack(role:GRoleDefault,hit:number){

        if(!role.get()) return;

        //添加受击显示
        app.event.emit(GModeEvent.HIT,{
            mode:this,
            role:role,
            hit:hit,
            world:role.v2World,
            camera:this.modeCamera,
        });

    }
    
}


