import { getAtrrNameById, ABI_TYPE_MAIN } from '../../consts';
import { gameData } from '../../pubUtils/data';
import { decodeArrayListStr } from '../../pubUtils/util';
import { ATTRIBUTE } from '../../pubUtils/dicParam';

export class AttributeCal {
    attrs: Map<number, number> = new Map<number, number>();
    ceAttrs: Map<number, number> = new Map<number, number>();
    ce?: number = 0;
    lv: number = 1;

    public setLv(lv: number) {
        this.lv = lv;
    }

    public setByWarJson(hid: number, attributes: {id: number, val: number}[], ratio: number = 1) {
        let dicHero = gameData.hero.get(hid);
        let dicJob = gameData.job.get(dicHero?.jobid);
        for(let {id, val} of attributes) {
            if(ABI_TYPE_MAIN.includes(id)) {
                this.attrs.set(id, Math.floor(val * ratio));
                this.ceAttrs.set(id, Math.floor(val * ratio));
            } else {
                this.attrs.set(id, Math.floor(val));
                let baseSubAttr = dicJob?.baseSubAttr?.find(cur => cur.id == id);
                let result = val - (baseSubAttr?.val||0);
                if(result < 0) result = 0;
                this.ceAttrs.set(id, Math.floor(result));
            }
        }
    }

    public setByCeArr(attributes: {id: number, val: number, ceVal: number}[], ratio: number = 1) {
        for(let {id, val, ceVal} of attributes) {
            if(ABI_TYPE_MAIN.includes(id)) {
                this.attrs.set(id, Math.floor(val * ratio));
                this.ceAttrs.set(id, Math.floor(ceVal * ratio));
            } else {
                this.attrs.set(id, Math.floor(val * ratio));
                this.ceAttrs.set(id, Math.floor(ceVal));
            }
        }
    }

    public setByMap(hid: number, attributes: Map<number, number>, ratio: number = 1 ) {
        let dicHero = gameData.hero.get(hid);
        let dicJob = gameData.job.get(dicHero?.jobid);
        for(let [id, val] of attributes) {
            if(ABI_TYPE_MAIN.includes(id)) {
                this.attrs.set(id, Math.floor(val * ratio));
                this.ceAttrs.set(id, Math.floor(val * ratio));
            } else {
                this.attrs.set(id, Math.floor(val));
                let baseSubAttr = dicJob?.baseSubAttr?.find(cur => cur.id == id);
                let result = val - (baseSubAttr?.val||0);
                if(result < 0) result = 0;
                this.ceAttrs.set(id, Math.floor(result));
            }
        }
    }

    private getAttrMap() {
        let newMap = new Map<number, number>();
        for(let [id, val] of this.attrs) {
            newMap.set(id, val);
        }
        return newMap
    }

    public getAttributes() {
        let attrs = new Attribute();
        attrs.setByMap(this.getAttrMap());
        return attrs;
    }

    public getAttributesToArr() {
        let arr: {id: number, val: number}[] = [];
        for(let [id, val] of this.getAttrMap()) {
            arr.push({ id, val });
        }
        return arr;
    }

    public getAttributesToString() {
        let arr = this.getAttributesToArr();
        return arr.map(cur => `${cur.id}&${cur.val}`).join('|');
    }

    public getMainAttributes() {
        let attribute = this.getAttributes();
        return attribute.getMainAttr();
    }

    // 战力计算
    // calType 1-全部 2-只算主属性 3-只算次属性
    public calCe(calType: number = 1) {
        let ce = gameData.ceRatio.reduce((pre, cur) => {
            let { type, val } = cur;
            if(ABI_TYPE_MAIN.includes(type)) {
                if(calType == 3) {
                    return 0
                } else {
                    return pre + val * (this.ceAttrs.get(type)|| 0)
                }
            } else {
                if(calType == 2) {
                    return 0
                } else {
                    return pre + val * (this.ceAttrs.get(type)|| 0) * this.lv / ATTRIBUTE.ATTRIBUTE_POWER_DIVISOR;
                }
            }
        }, 0);
        return Math.floor(ce);
    }

    public calSubAttrCe() {
        return Math.floor(this.calCe(3));
    }
}

export class Attribute {
    hp: number = 0;
    atk: number = 0;
    def: number = 0;
    mdef: number = 0;
    speed: number = 0;
    hit: number = 0;
    cri: number = 0;
    flee: number = 0;
    antCri: number = 0;
    damageIncrease: number = 0;
    damageDecrease: number = 0;
    defIngnore: number = 0;
    bloodSuck: number = 0;
    ap: number = 0;
    damageCri: number = 0;
    physicaldamageInc: number = 0;
    magicdamageInc: number = 0;
    physicaldamageDec: number = 0;
    magicdamageDec: number = 0;
    treatmentInc: number = 0;
    treatmentDec: number = 0;
    acceptTreatmentInc: number = 0;
    acceptTreatmentDec: number = 0;
    bloodRebound: number = 0;
    strikeBack: number = 0;

    setByMap( attributes: Map<number, number> ) {
        for(let [id, val] of attributes) {
            let attrName = getAtrrNameById(id);
            if(attrName in this) this[attrName] = val;
        }
    }

    setByStr(str: string) { // id&val|...
        let arr = decodeArrayListStr(str);
        for(let [id, val] of arr) {
            let attrName = getAtrrNameById(parseInt(id));
            if(attrName in this) this[attrName] = parseInt(val);
        }
    }

    getMainAttr() {
        return {
            hp: this.hp,
            atk: this.atk,
            def: this.def,
            mdef: this.mdef
        }
    }
}