import { BattleBase } from "../base/BattleBase";
import { BattleEventManager } from "../base/BattleEventManager";
import { BattleEventData_EnemyBorn, BattleEventData_EnemyDie, BattleEventData_EnemyMove, BattleEventData_Over, BattleEventTarget, BattleEventType } from "../base/BattleEventUtil";
import { BattleUtil } from "../data/BattleUtil";
import { BulletData } from "../data/BulletData";
import { EnemyData, EnemyDataPool } from "../data/EnemyData";
import { HeroData } from "../data/HeroData";
import { MapData, PosData,EnemyBornData } from "../data/MapData";

//敌人控制类
export class EnemyControl extends BattleBase{
    map:MapData = null
    mapPosList:Array<BattleUtil.Vector2> = null
    lengthUnit:number
    enemyDataList: any;
    battleEventManager:BattleEventManager = null

    init(){
        this.battleEventManager = BattleEventManager.instance
        this.map = MapData.GetInstance()
        this.mapPosList = this.map.mapPosList

        this.lengthUnit = 1//this.map.sizeUnit.length()
        this.enemyDataList = this.map.enemyDataList
    }

    //每次加一帧
    update(){
        this.map.enemyDataList.sort((a,b)=>{
            return b.moveLength - a.moveLength
        })
        this.map.enemyDataList.forEach(enemy => {
            
            //判断是否死亡  
            if(enemy.life <= 0){
                let eventData:BattleEventData_EnemyDie = {
                    eventType: BattleEventType.EnemyDie,
                    ID: enemy.ID
                }
                this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
                // console.log("死亡",enemy.ID)
                this.map.removeEnemy(enemy)
                return
            }
            //更新位置
            let oldPos = this.mapPosList[enemy.posID]
            let newPos = this.mapPosList[enemy.nextPosID]
            if(!newPos){
                console.log("没有下一个点则表示到达终点 有可能配置出错导致有bug")
                this.map.battleEnd = true
                this.map.bWin = false
                let eventData:BattleEventData_Over = {
                    eventType: BattleEventType.Over,
                    bWin: false
                }
                this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
                return
            }
            let length = BattleUtil.Vector2.Sub(enemy.position,newPos).length()
            //判断方向
            let direction = BattleUtil.Vector2.Sub(newPos,enemy.position)
            direction.normalize()
            direction.multiply(enemy.speedCur)
            let moveLenght = direction.length()
            //暂时没有近点补偿，不考虑多线路长度不一致问题
            enemy.moveLength += moveLenght
            //暂不考虑跳跃，只有近点移动
            if(length > moveLenght){
                enemy.position = BattleUtil.Vector2.Add(enemy.position,direction)
                enemy.speedVector =  direction
            }else{
                enemy.posID = enemy.nextPosID
                let nextPos = this.mapPosList[enemy.nextPosID]
                if(nextPos){
                    if(this.arePointsCollinear(nextPos,oldPos,newPos)){
                        enemy.position = BattleUtil.Vector2.Add(enemy.position,direction)
                        enemy.speedVector =  direction
                    }
                    else{
                        let newDirection = BattleUtil.Vector2.Sub(nextPos,newPos)
                        let subLength = moveLenght - length
                        enemy.speedVector =  BattleUtil.Vector2.Multiply(newDirection,enemy.speedCur)
                        newDirection.multiply(subLength/this.lengthUnit)
                        enemy.position = BattleUtil.Vector2.Add(enemy.position,newDirection)
                    }
                }
                else{
                    //没有下一个点则表示到达终点  有可能配置出错导致有bug
                    console.log("没有下一个点则表示到达终点1")
                    this.map.battleEnd = true
                    this.map.bWin = false
                    let eventData:BattleEventData_Over = {
                        eventType: BattleEventType.Over,
                        bWin: false
                    }
                    this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
                    return
                }

            }

            if(enemy.speedChange){
                enemy.speedChange = false
                let eventData:BattleEventData_EnemyMove = {
                    eventType: BattleEventType.EnemyMove,
                    ID: enemy.ID,
                    speedVector: enemy.speedVector
                }
                this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
            }
            
        })

        if(this.map.enemyBornMap.has(this.map.curTurn)){
            let enemyBornList = this.map.enemyBornMap.get(this.map.curTurn)
            enemyBornList.forEach((enemyBornData:EnemyBornData)=>{
                let enemy = this.map.addEnemy(enemyBornData.typeID,enemyBornData.level,enemyBornData.roldID,enemyBornData.positionID)
                let eventData:BattleEventData_EnemyBorn = {
                    eventType: BattleEventType.EnemyBorn,
                    ID: enemy.ID,
                    typeID: enemy.typeID,
                    position: enemy.position,
                    speedVector: enemy.speedVector,
                    life: enemy.life,
                    lifeMax: enemy.lifeMax
                }
                this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
            }) 
           
        }
    }

    arePointsCollinear(p1: BattleUtil.Vector2, p2: BattleUtil.Vector2, p3: BattleUtil.Vector2): boolean {
        // 使用乘法形式比较斜率是否相等
        return (p2.y - p1.y) * (p3.x - p1.x) === (p3.y - p1.y) * (p2.x - p1.x);
    }
}