import { HeroConf } from "../conf/HeroConf";
import { UptypeConf } from "../conf/UptypeConf";
import { BattleDataPool } from "./BattleDataPool";
import { BulletType, ObjectValueType } from "./BattleEnum";
import { BattleUtil } from "./BattleUtil";
import { BuffData, BuffDataPool } from "./BuffData";
import { DataBase } from "./DataBase";
import { EnemyData } from "./EnemyData";
import { SkillData, SkillDataPool } from "./SkillData";


export class HeroData extends DataBase{
    level: number;

    //所属格子 -1表示暂无
    posID: number;
    //当前坐标
    protected _position:BattleUtil.Vector2;

    // //动画
    // //前摇时间
    // preTime: number;
    // //攻击动画时间
    // attackTime: number;
    
    //攻击类型 瞬发单体/瞬发群体/弹道单体/弹道群体
    attackType: BulletType;
    //攻击速度 攻击间隔
    protected _speed: number;
    //攻击范围
    protected _attackRadius: number;
    //攻击力
    protected _attackDamage: number;
    //暴击率
    protected _critical: number;
    //暴击伤害
    protected _criticalDamage: number;

    //攻击速度 攻击间隔
    protected _speedCur: number;
    //攻击范围
    protected _attackRadiusCur: number;
    //攻击力
    protected _attackDamageCur: number;
    //暴击率
    protected _criticalCur: number;
    //暴击伤害
    protected _criticalDamageCur: number;
    
    //特殊攻击
    // spaceValueList:Map<ObjectValueType,BattleUtil.SpecialValue[]> = new Map();
    
    //技能列表
    skillList: number[];
    //实例化后的技能列表
    skillDataList: Array<SkillData> = [];
    
    //子弹命中后释放的技能列表
    skillBulletList: number[];
    skillDataBulletList: Array<SkillData> = [];

    //buff列表
    buffList: Array<BuffData> = [];
    //目标敌人
    targetEnemy:EnemyData;
    //冷却剩余时间
    coolDown: number;


    init(typeID:number, level:number, posID: number) {
        super._init();
        this.level = level;
        this.typeID = typeID;
        this.posID = posID;

        //读地图
        this.position = new BattleUtil.Vector2(0, 0);
        let heroConf = HeroConf.data[typeID]
        if (heroConf) {
            this.attackType = heroConf.AttackType;
            this._speed = heroConf.Speed;
            this.coolDown = this._speed
            this._attackRadius = heroConf.Radius;
            this._attackDamage = heroConf.Damage;
            this._critical = heroConf.Critical;
            this._criticalDamage = heroConf.CriticalDamage;
            this.skillList = heroConf.SkillArray

            if(this.skillList && this.skillList.length > 0){
                for (let i = 0; i < this.skillList.length; i++) {
                    let skillID = this.skillList[i];
                    let skillData = SkillDataPool.getObject();
                    skillData.init(skillID, this);
                    this.skillDataList.push(skillData);
                }
            }

            this.skillBulletList = heroConf.SkillBulletArray
            if(this.skillBulletList && this.skillBulletList.length > 0){
                for (let i = 0; i < this.skillBulletList.length; i++) {
                    let skillID = this.skillBulletList[i];
                    let skillData = SkillDataPool.getObject();
                    skillData.init(skillID, this);
                    this.skillDataBulletList.push(skillData);
                }

            }

            this.upDataLevel()
            return this;
        }
        return null;
    }

    set position(pos:BattleUtil.Vector2) {
        this._position = pos;
    }

    get position():BattleUtil.Vector2 {
        return this._position;
    }   


        //攻击速度 攻击间隔
    get speed(): number{
        return this._speedCur;
    }
        //攻击范围
    get attackRadius(): number{
        return this._attackRadiusCur;
    }
        //攻击力
    get attackDamage(): number{
        return this._attackDamageCur;
    }
        //暴击率
    get critical(): number{
        return this._criticalCur;
    }
        //暴击伤害
    get criticalDamage(): number{
        return this._criticalDamageCur;
    }
    //只对buff进行记录
    addBuff(buff:BuffData) {
        this.buffList.push(buff);
        return this;
    }

    levelUp() {
        this.level++;
        this.upDataLevel()
    }
    levelTo(newLevel: number){
        this.level = newLevel;   
        this.upDataLevel()
    }
    upDataLevel() {

        let conf = UptypeConf.data
        this._speedCur = this._speed / (conf.HeroSpeedColArr[this.level]||1);
        this._attackRadiusCur = this._attackRadius;
        this._attackDamageCur = this._attackDamage* (conf.HeroAttackColArr[this.level]||1);
        this._criticalCur = this._critical;
        this._criticalDamageCur = this._criticalDamage;
    }

    removeBuff(ID: number) {
        for (let i = 0; i < this.buffList.length; i++) {
            let buff = this.buffList[i];
            if (buff.ID == ID) {
                this.buffList.splice(i,1);
                break;
            }
        }
        return this;
    }

    // addSkill(skill:SkillData) {
    //     this.skillDataList.push(skill);
    //     return this;
    // }

    clear() {

        this.level = 0;
        this.typeID = BattleUtil.TypeID_Init;
        this.posID = BattleUtil.PosID_Init;

        //读表
        this.attackType = BulletType.Single;
        this._speed = 0;
        this._attackRadius = 0;
        this._attackDamage = 0;
        this._critical = 0;
        this._criticalDamage = 0;
        this._attackDamageCur = 0;
        this._attackRadiusCur = 0;
        this._criticalCur = 0;
        this._criticalDamageCur = 0;
        this._speedCur = 0;
        this.coolDown = 0;

        // this.spaceValueList.clear();
        for (let i = 0; i < this.skillDataList.length; i++) {
            SkillDataPool.putObject(this.skillDataList[i]);
        }
        this.skillDataList = [];
        this.skillList = [];

        for (let i = 0; i < this.skillDataBulletList.length; i++) {
            SkillDataPool.putObject(this.skillDataBulletList[i]);
        }
        this.skillDataBulletList = [];
        this.skillBulletList = [];

        this.buffList = [];
        this.targetEnemy = null;
    }
}
export let HeroDataPool = new BattleDataPool(HeroData,30)


