import GFSMBase, { GFSMProcessEnum, GFSMProcessInfo, GFSMProcessMode } from "../../base/fsm/GFSMBase";
import GOnHookMode from "../GOnHookMode";
import { GNormalModePlayerEnum } from "../default/GNormalModeBase";

//流程枚举
enum ProcessEnum {
    SeekEnemy = 0, //寻找敌人(场上没有敌人 或者敌人都击杀完)
    GenNextFight = 1, //生成下一个战斗位置
    LeaveFight = 2, //前往战斗
    GenEnemy = 3, //生成敌人
    Fight = 4, //战斗
}

//无限模式的状态类
export default class GFSMOnHookMode extends GFSMBase{
    mode:GOnHookMode;

    constructor(mode:GOnHookMode){
        super();
        this.mode = mode;
    }

    start:number = ProcessEnum.SeekEnemy;

    process:{[key:number]:GFSMProcessInfo} = {
        [ProcessEnum.SeekEnemy]:{
            title:"寻找敌人",
            mode:GFSMProcessMode.Execute,
            execute: this.onSeekEnemyProcess.bind(this),
            to:[ProcessEnum.GenNextFight,ProcessEnum.Fight]
        },
        [ProcessEnum.GenNextFight]:{
            title:"生成下一个战斗位置",
            mode:GFSMProcessMode.Execute,
            execute: this.onGenNextFightProcess.bind(this),
            to:[ProcessEnum.GenEnemy]
        },
        [ProcessEnum.GenEnemy]:{
            title:"生成敌人",
            mode:GFSMProcessMode.Execute,
            execute: this.onGenEnemyProcess.bind(this),
            to:[ProcessEnum.LeaveFight]
        },
        [ProcessEnum.LeaveFight]:{
            title:"前往战斗",
            mode:GFSMProcessMode.WaitExecute,
            execute: this.onLeaveFightProcess.bind(this),
            to:[ProcessEnum.Fight]
        },
        [ProcessEnum.Fight]:{
            title:"战斗",
            mode:GFSMProcessMode.WaitExecute,
            execute: this.onFightProcess.bind(this),
            to:[ProcessEnum.SeekEnemy]
        }
    }

    //寻找敌人
    onSeekEnemyProcess(dt:number){

        //判断是否还有敌人
        if(this.mode.isHaveEnemy()){
            return ProcessEnum.Fight; //如果有则攻击
        }else{
            return ProcessEnum.GenNextFight; //没有则生成下一个战斗位置
        }

    }

    //生成下一个战斗位置
    onGenNextFightProcess(){

        this.mode.isAllowAttack = false;
        this.mode.onNextTarget();
        return ProcessEnum.GenEnemy; //生成敌人

    }

    //生成敌人
    onGenEnemyProcess(){

        this.mode.onResetFight();
        return ProcessEnum.LeaveFight; //前往战斗
        
    }


    //前往战斗
    onLeaveFightProcess(){

        //判断玩家是否前往到战斗位置
        let roles = this.mode.getOnesRoleAlive(GNormalModePlayerEnum.PLAYER);
        if(roles.length == 0) return GFSMProcessEnum.Reset;

        for (let index = 0; index < roles.length; index++) {
            const role = roles[index];
            if(!(role.isTacticalPos())){
                //如果不在位置则等待
                return GFSMProcessEnum.Wait;
            }
        }

        //到达位置生成敌人
        return ProcessEnum.Fight;
        
    }

    //战斗
    onFightProcess(){

        
        this.mode.isAllowAttack = true;
        //等待敌人击杀完
        if(!(this.mode.isHaveEnemy())){
            return ProcessEnum.SeekEnemy; //击杀完寻找敌人
        }

        //没有击杀完则等待击杀
        return GFSMProcessEnum.Wait;
        
    }
    
}


