import { BattleBase } from "../base/BattleBase";
import { BattleEventManager } from "../base/BattleEventManager";
import { BattleEventData_EnemyBuff, BattleEventData_EnemyDie, BattleEventData_EnemyHurt, BattleEventData_EnemyMove, BattleEventTarget, BattleEventType, BuffEventState, HurtEventState } from "../base/BattleEventUtil";
import { BuffType } from "../data/BattleEnum";
import { BattleUtil } from "../data/BattleUtil";
import { BuffData ,BuffDataPool} from "../data/BuffData";
import { DataBase } from "../data/DataBase";
import { EnemyData } from "../data/EnemyData";
import { HeroData } from "../data/HeroData";
import { MapData } from "../data/MapData";

//子弹控制类
export class BuffControl extends BattleBase{
    
    map:MapData = null
    buffMap:Map<number,BuffData> = null;
    battleEventManager: BattleEventManager = null;

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

    //每次加一帧
    update(){
        this.buffMap.forEach((buff:BuffData)=>{
            buff.duration--
            buff.lifeTime++
            this.updateTimeBuff(buff)
            if(buff.duration <= 0){
                let eventData:BattleEventData_EnemyBuff = {
                    eventType: BattleEventType.EnemyBuff,
                    ID: buff.targetDataBaseID,
                    buffID: buff.ID,
                    eventState: BuffEventState.Remove
                }
                this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
               this.map.removeBuff(buff.ID)
            }
        })
    }

    //处理新加buff
    updateNewBuff(){
        this.map.buffMap.forEach((buff:BuffData)=>{
            if(buff.lifeTime == 0){
                let eventData:BattleEventData_EnemyBuff = {
                    eventType: BattleEventType.EnemyBuff,
                    ID: buff.targetDataBaseID,
                    buffID: buff.ID,
                    eventState: BuffEventState.Add
                }
                this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
                this.updateAddBuff(buff)
            }
        })
    }


    //时间变化引起的buff计算
    updateTimeBuff(buff:BuffData){

        switch(buff.typeID){
            case BuffType.Poison:{//中毒
                let target = buff.targetDataBase as EnemyData
                let src = buff.srcDataBase as HeroData
                if(buff.lifeTime%BattleUtil.FrameRate == 1){
                    target.life -= buff.valueList[0];
                    let eventData:BattleEventData_EnemyHurt = {
                        eventType: BattleEventType.EnemyHurt,
                        targetID: target.ID,
                        hurt: buff.valueList[0],
                        status: HurtEventState.Poison,
                        typeID:src.typeID
                    }
                    this.battleEventManager.fireEvent(BattleEventTarget.Update,eventData)
                    
                }
            }
                break;
        }
        
      
    }



    //处理加buff时，有影响的buff
    updateAddBuff(buff:BuffData){
        let src = buff.srcDataBase
        let target = buff.targetDataBase

        if(target instanceof HeroData){
            this.updateHeroAddBuff(buff,target)
        }else if(target instanceof EnemyData){
            this.updateEnemyAddBuff(buff,target)
        }
    }
    private updateEnemyAddBuff(buff: BuffData, target: EnemyData) {
        switch(buff.typeID){
            case BuffType.Snow://减速 目前只有一种不做过渡计算
                target.speedCur = target.speedBase*buff.valueList[0];
                // console.log("减速:",target.ID)
                break;
            case BuffType.Ice://冰冻
                target.speedCur = 0;
                // console.log("冰冻:",target.ID)
                break;

        }
    }
    private updateHeroAddBuff(buff: BuffData, target: HeroData) {
        throw new Error("Method not implemented.");
    }





    //处理结算时有影响的buff
    updateRemoveBuff(buff:BuffData){
        let src = buff.srcDataBase
        let target = buff.targetDataBase

        if(target instanceof HeroData){
            this.updateHeroRemoveBuff(buff,target)
        }else if(target instanceof EnemyData){
            this.updateEnemyRemoveBuff(buff,target)
        }
        
    }
    
    private updateEnemyRemoveBuff(buff: BuffData,target: EnemyData) {
        switch(buff.typeID){
            case 2:{//减速 目前只有一种不做过渡计算
                target.speedCur = target.speedBase;

                break;
            }
            case 3:{//冰冻
                target.speedCur = target.speedBase;

                break;
            }
        }
    }

    //英雄暂时无buff
    private updateHeroRemoveBuff(buff: BuffData,target: HeroData) {
        throw new Error("Method not implemented.");
    }

}