import { BattleBase } from "../base/BattleBase";
import { BattleEventManager } from "../base/BattleEventManager";
import { BattleEventData_BulletAttack, BattleEventData_BulletMove, BattleEventTarget, BattleEventType, HurtEventState } from "../base/BattleEventUtil";
import { BulletType } from "../data/BattleEnum";
import { BattleUtil } from "../data/BattleUtil";
import { BuffData } from "../data/BuffData";
import { BulletData } from "../data/BulletData";
import { EnemyData } from "../data/EnemyData";
import { HeroData } from "../data/HeroData";
import { MapData,PosData } from "../data/MapData";
import { SkillControl } from "./SkillControl";

//子弹控制类
export class BulletControl extends BattleBase{

    map:MapData = null
    attackPosDataMap:Map<number,PosData> = null;
    enemyDataList:Array<EnemyData> = null;
    skillControl:SkillControl = null;
    battleEventManager: BattleEventManager;

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

        this.skillControl = SkillControl.GetInstance()
    }


    //每帧更新一次
    update(){
        // this.attackPosList = this.map.attackPosDataList
        let bulletList = this.map.bulletList
        for(let i = 0; i < bulletList.length; i++){
            let bullet = bulletList[i];
            let hero = bullet.srcHero
            let enemy = bullet.targetEnemy
            if(hero && enemy && hero.ID == bullet.srcHeroID && enemy.ID == bullet.targetEnemyID){

                let direction = BattleUtil.Vector2.Sub(bullet.position,enemy.position)
                direction.normalize()
                direction.multiply(bullet.speedCur)

                let eventData:BattleEventData_BulletMove = {
                    eventType: BattleEventType.BulletMove,
                    ID: bullet.ID,
                    speedVector: direction
                }
                this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)

                bullet.position = BattleUtil.Vector2.Add(bullet.position,bullet.speedVector);
                if(BattleUtil.Vector2.Sub(bullet.position,enemy.position).length() < bullet.boxRadius){
                    this.attack(hero,enemy) 
                    if(hero.attackType == BulletType.GroupBullet){
                        this.enemyDataList.forEach(enemyOther => {
                            if(enemyOther.ID != bullet.targetEnemyID){
                                if(BattleUtil.Vector2.Sub(bullet.position,enemyOther.position).length() < bullet.attackRadius){
                                    enemy.life -= hero.attackDamage
                                    let eventData:BattleEventData_BulletAttack = {
                                        eventType: BattleEventType.BulletAttack,
                                        typeID: hero.typeID,
                                        hurt: hero.attackDamage,
                                        targetID: enemy.ID,
                                        status: HurtEventState.Normal
                                    }
                                    this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
                                }
                            }
                        })
                    }                   
                }
            }
            else{
                this.map.removeBullet(bullet)
                i--
            }
        }
    }


    //攻击直接伤害
    attack(hero: HeroData, enemy: EnemyData) {
        //先普通攻击
        enemy.life -= hero.attackDamage
        let eventData:BattleEventData_BulletAttack = {
            eventType: BattleEventType.BulletAttack,
            typeID: hero.typeID,
            hurt: hero.attackDamage,
            targetID: enemy.ID,
            status: HurtEventState.Normal
        }
        let addBuffData:BuffData[] = null
        //在加技能攻击
        hero.skillBulletList.forEach(skillID => {
            let buffDataList = this.skillControl.useSkill(hero, enemy, skillID)
            buffDataList.forEach(buffData => {
                if(addBuffData == null){
                    addBuffData = []
                }
                addBuffData.push(buffData)
            })
        })

        this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)

    }
}