import { PublicShared } from "../../public/public";
import { buffDataType, FightObj } from "../fightType";
import { Role } from "../Role";

let idx = 0;
export class BuffBase {
    altAttr: k_v<number> = {};
    role: FightObj;
    overlapTimes = 1;
    /** buff生效的剩余回合数 */
    remainingRounds = 0;
    _BuffId: string;
    startRound: number;
    constructor(public data: buffDataType) {
        this.role = this.data.toRole;
        idx++;
        this._BuffId = 'BuffId_' + idx;
        this.startRound = this.role.fightControl.roundNums;
    }

    release(){
        this.clear('release');
        this.altAttr = null;
        this.role = null;
    }

    start() {
        let skillConf = this.data.skillConf;
        this.remainingRounds = skillConf.round;

        this.role.fightControl.event.on('fightEvent_roundNums', this.fightEvent_roundNums.bind(this), this);
        this.fightEvent_roundNums({ turn: this.role.fightControl.roundNums });
        this.dobuff();
    }

    /**
     * 回合开始前buff剩余回合减一
     * 到-1的时候清除buff，
     * remainingRounds依然会继续减
     * @param data
     */
    fightEvent_roundNums(data: { turn: number; }) {
        this.remainingRounds--;
        if (this.remainingRounds == -1) {
            this.clear('duration');
        }/*  else {
            this.dobuff();
        } */
    }

    dobuff() {
        if (this.role.isDead) {
            this.clear('dead');
            return;
        }

        this.overlaponly(this.data);
    }

    //叠加效果
    overlaponly(newBuffData: buffDataType, newBuff = false) {
        if (newBuff) {
            //超出了叠加层数
            let diejiacengshu = newBuffData.skillConf.diejiacengshu;
            if (this.overlapTimes >= diejiacengshu) return false;
            this.overlapTimes++;
        };
        this._overlaponly(newBuffData);
        return true;
    }

    /**
     * 叠加buff效果的具体实现
     * @param newBuffData
     */
    _overlaponly(newBuffData: buffDataType) {
        let skillConf = newBuffData.skillConf;
        if (skillConf.sxleixing != 'attr') return;
        this.role.debug(function () {
            console.log('### buff overlaponly', newBuffData.buffid, skillConf.xiaoguocanshu);
        });

        let skillLv = newBuffData.toRole.skillLv;
        if (newBuffData.toRole.rid != newBuffData.fromRole.rid) {
            skillLv = newBuffData.fromRole.skillLv;
        }

        let xiaoguocanshu = skillConf.xiaoguocanshu;
        for (var k in xiaoguocanshu) {
            if (k == 'gs') continue;
            this.altAttr[k] = this.altAttr[k] || 0;
            let attr = xiaoguocanshu[k];
            if (typeof attr == 'string') {
                attr = PublicShared.eval(attr, { slv: skillLv });
            }
            this.altAttr[k] += attr;
            this.role.roleData.attr[k] = this.role.roleData.attr[k] || 0;
            let toAttr = this.role.roleData.attr[k] + attr;
            this.role.setData(k, toAttr);
        }
    }

    // 重置回合
    retime() {
        this.remainingRounds = this.data.skillConf.round;
    }

    _delAttr() {
        for (var key in this.altAttr) {
            if (key == "hp") continue;
            var toAttr = this.role.getData(key) - this.altAttr[key];
            this.role.setData(key, toAttr);
        };
        this.altAttr = {};
        this.overlapTimes == 1;
    }

    willClear() { }
    onClear() { }
    clear(when: string) {
        this._delAttr();
        let buffType = this.data.skillConf.sxleixing;
        if (buffType == 'attr') {
            // 多个效果需要分别返回，为兼容其它字符串格式，此处处理成逗号分隔的字符串格式
            // 解决单个buff有多重特效，解除buff时仅返回单一特效的问题。
            buffType = Object.keys(this.data.skillConf.xiaoguocanshu).join(',');
        }
        // 字符串以逗号分隔转换为数组
        if(when != "release"){
            let buffTypeArr = buffType.split(',')
            buffTypeArr.forEach(buffTypeStr => {
                this.role.fightControl.record({
                    act: 'delBuff',
                    fromRole: this.role.rid,
                    skillId: this.data.buffid,
                    buffType: buffTypeStr,
                    buffId: this._BuffId,
                    why: when
                });
            })
        }
        this.willClear();
        // this.role.fightControl.event.emit('fight_buffClear', this.data);
        let buffid = this.data.buffid;
        this.role.buffs[buffid] = null;
        delete this.role.buffs[buffid];
        this.role.fightControl.event.off('fightEvent_roundNums', this.fightEvent_roundNums, this);
        this.onClear();
    }

}