import { BattleBase } from "../base/BattleBase";
import { BattleEventManager } from "../base/BattleEventManager";
import { BattleEventData_EnemyHurt, BattleEventData_HeroAction, BattleEventData_HeroAttack, BattleEventData_HeroAttackBullet, BattleEventHeroAction, BattleEventTarget, BattleEventType, HeroActionType, 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 HeroControl extends BattleBase{

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

    actionList:Array<BattleEventHeroAction> = []

    constructor(){
        super();
        this.battleEventManager = BattleEventManager.instance
        BattleEventManager.instance.addEvent(BattleEventTarget.HeroAction, this.heroActionEvent.bind(this), -1);
    }

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

        this.skillControl = SkillControl.GetInstance()

        
    }

    //每次加一帧
    update(){
        this.actionList.forEach(actionData=>{
            let hero = this.attackPosDataMap.get(this.map.attackPosition[actionData.posIndex]).hero
            if(hero){
                this.checkAttack(hero)
            }
        })
        this.actionList = []

        this.attackPosDataMap.forEach((posData:PosData)=>{
            let hero = posData.hero
            if(hero){
                hero.coolDown -= 1
                if(hero.coolDown <= 0){
                    let targetID = -1
                    for(let i = 0;i<this.enemyDataList.length;i++){
                        let enemy = this.enemyDataList[i]
                        if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
                            targetID = enemy.ID
                            
                            hero.coolDown = hero.speed
                            break;
                        }
                    }

                    let eventData:BattleEventData_HeroAction = {
                        eventType: BattleEventType.HeroAction,
                        posID: hero.posID,
                        targetID: targetID,
                        action: HeroActionType.Normal
                    }
                    this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
                }
            }
        
        })

    }


    checkAttack(hero:HeroData){
        let bAttack = false
        if(hero.attackType == BulletType.Single ){
            for(let i = 0;i<this.enemyDataList.length;i++){
                let enemy = this.enemyDataList[i]
                if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
                    this.attack(hero,enemy)
                    bAttack = true
                    break;
                }
            }
        }
        else if(hero.attackType == BulletType.Group){
            // let attackList:Array<EnemyData> = []
            for(let i = 0;i<this.enemyDataList.length;i++){
                let enemy = this.enemyDataList[i]
                if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
                    this.attack(hero,enemy)
                    bAttack = true
                }
            }

        }
        else if(hero.attackType == BulletType.SingleBullet){
            //创建一颗子弹

            for(let i = 0;i<this.enemyDataList.length;i++){
                let enemy = this.enemyDataList[i]
                if(this.isInRadius(hero.position,enemy.position,hero.attackRadius)){
                    let bulletData:BulletData = this.map.addBullet(1,hero,enemy)

                    this.attackBullet(hero,enemy,bulletData)

                    bAttack = true
                    break;
                }
            }
           
        }
        else if(hero.attackType == BulletType.GroupBullet){
            
        }
        return bAttack
    }

    //攻击直接伤害
    attack(hero: HeroData, enemy: EnemyData) {
        //先普通攻击
        enemy.life -= hero.attackDamage
        let eventData:BattleEventData_HeroAttack = {
            eventType: BattleEventType.HeroAttack,
            posID: hero.posID,
            hurt: hero.attackDamage,
            status: HurtEventState.Normal,
            targetID: enemy.ID
        }
        this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
        let addBuffData:BuffData[] = null
        //在加技能攻击
        hero.skillList.forEach(skillID => {
            addBuffData = this.skillControl.useSkill(hero, enemy, skillID)
        })
    }

    //攻击打出子弹
    attackBullet(hero: HeroData, enemy: EnemyData,bulletData:BulletData) {
        let eventData:BattleEventData_HeroAttackBullet = {
            eventType: BattleEventType.HeroAttackBullet,
            posID: hero.posID,
            targetID: enemy.ID,
            bulletID: bulletData.ID
        }
        this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
        let addBuffData:BuffData[] = null
        //在加技能攻击
        hero.skillList.forEach(skillID => {
            addBuffData = this.skillControl.useSkill(hero, enemy, skillID)
        })

        bulletData.buffDataList = addBuffData
    }
    isInRadius(position: BattleUtil.Vector2, position1: BattleUtil.Vector2, attackRadius: number) {
        return BattleUtil.Vector2.Sub(position, position1).length() <= attackRadius
        // return true
    }

    heroActionEvent(eventData:BattleEventHeroAction){

        this.actionList.push(eventData)
    }

    
}