import { BattleUtil } from "./BattleUtil";
import { DataBase } from "./DataBase";
import { BattleDataPool } from "./BattleDataPool";
import { EnemyConf } from "../conf/EnemyConf";
import { BuffData } from "./BuffData";

export class EnemyData extends DataBase{

    // level: number;

    //所属类型编号
    //行径
    rold:Array<number>
    //当前坐标
    protected _position: BattleUtil.Vector2;
    
    //当前所处路线id
    protected _roldID: number = 0;

    // //出生动画时间  出生保护时间
    // bornTime: number = 10;
    
    //生命
    protected _life: number;
    //最大生命
    lifeMax: number;
    //基础速度 30帧移动的格子速度,单位是1个格子 
    protected speed_30: number;
    protected _speedBase: number;
    //当前速度 //每帧的速度,单位是1个格子 
    _speedCur: number;
    //不初始化表现会慢一帧吗? 一帧应该没关系
    protected _speedVector: BattleUtil.Vector2 = new BattleUtil.Vector2(0, 0);
    speedChange: boolean = false;

    //技能列表
    skillList: number[];
    //buff列表
    buffList: BuffData[] = [];

    //已经行走的距离
    moveLength: number = 0;

    init(typeID: number,level:number,rold:Array<number>) {
        super._init();
        // this.level = level;
        this.typeID = typeID;

        let enemyConf = EnemyConf.data[typeID]
        if (enemyConf) {
            this.lifeMax = enemyConf.Life * level;
            this.life = this.lifeMax;
            this.speed_30 = enemyConf.Speed;
            this._speedBase = this.speed_30 / BattleUtil.FrameRate;
            this.speedCur = this._speedBase
            this.skillList = enemyConf.Skill
        }
        //读表

        this.skillList = [];
        this.buffList = [];
        this.rold = rold

        return this;
    }

    set speedCur(speed: number) {
        if (speed == 0) {
            return;
        }
        this._speedCur = speed;
    }
    get speedCur(): number {
        return this._speedCur;
    }

    set life(life: number) {
        this._life = life;
        // if(life <= 0){
        //     console.log(this.ID,"EnemyData life is <= 0")
        // }
    }
    get life(): number {
        return this._life;
    }

    set position(pos:BattleUtil.Vector2) {
        this._position = pos;
        if (Number.isNaN(this._position.x) || Number.isNaN(this._position.y)) {
            console.log("EnemyData position is NaN")
            this._position = new BattleUtil.Vector2(0, 0);
        }
        // if (this._position.x < 0 || this._position.y < 0) {
        //     console.log("EnemyData position is < 0")
        //     // this._position = new BattleUtil.Vector2(0, 0);
        // }
        // console.log("EnemyData position", this._position)
    }
    get position():BattleUtil.Vector2 {
        if (Number.isNaN(this._position.x) || Number.isNaN(this._position.y)) {
            console.log("EnemyData position is NaN")
            //this._position = new BattleUtil.Vector2(0, 0);
        }
        return this._position;
    }

    //当前所处格子
    get posID(): number {
        return this.rold[this._roldID] || BattleUtil.PosID_Init;
    }
    set posID(id: number) {
        for (let i = 0; i < this.rold.length; i++) {
            if (this.rold[i] == id) {
                this._roldID = i;
                return;
            }
        }
    }
    get nextPosID(): number {
        return this.rold[this._roldID + 1] || BattleUtil.PosID_Init;
    }

    get roldID(): number {
        return this._roldID;
    }
    set roldID(id: number) {
        this._roldID = id;
    }

    get speedBase(): number {
        return this._speedBase;
    }


    addBuff(buff: BuffData) {
        this.buffList.push(buff);
    }

    removeBuff(buff: BuffData) {
        this.buffList.splice(this.buffList.indexOf(buff), 1);
    }

    set speedVector(vector:BattleUtil.Vector2){
        if(this._speedVector.x == vector.x && this._speedVector.y == vector.y)
        {
           return  
        }
        // if((vector.x)**2 < 0.0000001 && vector.y**2 < 0.0000001){
        //     console.log("EnemyData speedVector is 0")
        // }

        this.speedChange = true;
        this._speedVector = vector;
    }
    get speedVector():BattleUtil.Vector2{
        return this._speedVector;
    }


    clear() {
        //读表
        this.life = 0;
        // this.level = 1;
        this.typeID = BattleUtil.TypeID_Init;
        this.posID = BattleUtil.PosID_Init;
        this.skillList = [];
        this.buffList = [];
        this.rold = []
        this.speedVector = new BattleUtil.Vector2(0, 0);
        this.moveLength = 0;
        super.clear();
    }
}


export let EnemyDataPool = new BattleDataPool(EnemyData,100)