import { Role } from "./Role";
import { RunAfterSkill } from "./afterSkill";
import { BuffBase } from "./fightBuff/buffBase";
import * as BuffClass from './fightBuff/buffExtends';
import { FightControl } from "./fightCntrol";
import { getSkillConf, randNumber, randomSkill } from "./fightFun";
import { buffDataType, dpsinfoType, joinFightData, modifyHpType, roleDataType } from "./fightType";
import { PassiveSkillBase } from "./passiveSkill/passiveSkillBase";
import { runPassiveSkillBase } from "./passiveSkill/runPassiveSkill";
import { SkillBase } from "./skill/skillBase";

export class Player {
    rid: string;
    curAtkSkill: SkillBase;
    /**记录技能的选中目标 */
    skillTargets: k_v<Role[]> = {};
    lastSkillTargets: Role[] = [];
    lastSkillId: string;
    lastHitMe: Role = null;
    buffs: k_v<any> = {};
    passiveSkills: k_v<any> = {};
    /**主动复活英雄数量*/
    activeResurrect = 0;
    get isDead() { return false; }
    get lockHp() { return false; }
    get rolePlayer() { return this }

    release(){
        for(let buffid in this.buffs){
            this.buffs[buffid].release();
        }
        for(let passiveid in this.passiveSkills){
            this.passiveSkills[passiveid].release();
        }
        this.curAtkSkill?.release();
        this.curAtkSkill = null;
        this.skillTargets = null;
        this.lastSkillTargets = null;
        this.lastSkillId = null;
        this.lastHitMe = null;
        this.buffs = null;
        this.passiveSkills = null;
    }

    skillIdx = 0;
    skillLv = 1;
    hudunzhi = 0;
    addpkNum = 0;
    fightSkills: string[] = [];
    get skillId() {
        let skillid = this.fightSkills[this.skillIdx];
        const xljhConf = G.gc.xunlianjihua;
        if (skillid != "") this.skillLv = this.roleData.skills[skillid];
        let skillConf: any = xljhConf[skillid];
        let mofa = this.getData('mofa');
        let nuqi = skillConf?.nuqi || 0;
        if (mofa < nuqi) return "";
        this.setData('mofa', mofa - nuqi);
        this.fightControl.record({
            act: 'playerNuqi',
            fromRole: this.rid,
            maxnuqi: this.getData('maxmofa'),
            nuqi: this.getData('mofa')
        });
        this.skillIdx++;
        this.skillIdx = this.skillIdx > this.fightSkills.length - 1 ? 0 : this.skillIdx;

        return skillConf?.skill || "";
    }

    isHasFightSkills: boolean;
    get _isHasFightSkills() {
        if (!this.roleData.fightSkills) return false;
        let _fightSkills = Object.values(this.roleData.fightSkills);
        let fightSkills = _fightSkills.filter(v => v != '');
        return fightSkills.length > 0;
    }
    get isCanAtk() { return false; }
    getLastTargetsBySkillID(skillid: string) {
        /**获取最后一次触发skillid技能时，该技能的选敌目标*/
        return this.skillTargets[skillid] || [];
    }
    constructor(public fightControl: FightControl, public roleData: joinFightData['player']) {
        this.rid = 'player_' + roleData.side + '_' + roleData.pos;
        this.isHasFightSkills = this._isHasFightSkills;
        if (this.isHasFightSkills) {
            let fightSkills = Object.values(this.roleData.fightSkills);
            fightSkills = fightSkills.filter(v => v != '');
            fightSkills = fightSkills.concat(["", ""]);
            this.fightSkills = fightSkills;
        }

        if (this.roleData?.buff?.backmp) {
            this.fightControl.event.on('fightEvent_roundNums', this.backmp.bind(this), this);
        }
    }
    backmp(data: { turn: number; }) {
        let mofa = this.getData('mofa');
        mofa += this.roleData?.buff?.backmp;
        mofa = mofa >= this.getData('maxmofa') ? this.getData('maxmofa') : mofa;
        this.setData('mofa', mofa);
        this.fightControl.record({
            act: 'playerNuqi',
            fromRole: this.rid,
            maxnuqi: this.getData('maxmofa'),
            nuqi: this.getData('mofa')
        });
    }

    runSkillClass() {
        let _skillId = this.skillId;
        if (_skillId == "") return;
        let skillconf = getSkillConf(_skillId);
        if (!skillconf) console.error('没有技能-->' + _skillId);
        return new SkillBase(this, skillconf);
    }

    atk() {

        this.curAtkSkill = this.runSkillClass();
    }
    bloodReturn() { }
    attr = {
        speed: 99999,
        atk: this.roleData.buff?.zj_atk || 0,
        def: this.roleData.buff?.zj_def || 0,
        zhishi: this.roleData.buff?.zj_zhishi || 0,
        liliang: this.roleData.buff?.zj_liliang || 0,
        mofa: this.roleData.buff?.zj_mofa || 0,
        maxmofa: this.roleData.buff?.zj_mofa || 0,
    }

    setData(key: string, val: number) {
        this.attr[key] = val;
    }
    getData(key: string, defaultValue: number = 0) {
        return this.attr[key] || this.roleData[key] || defaultValue;
    }
    getDataPro(key: string) {
        return this.attr[key] || this.roleData[key] || 0;
    }

    modifyHp(dpsinfo: dpsinfoType, extData: modifyHpType) { }
    _modifyHp(dpsinfo: dpsinfoType, extData: modifyHpType) { }

    isWillDead(dpsinfo: dpsinfoType, extData: modifyHpType) { return false; }

    Dead() { }

    /**
     * 
     * @param skillinfo ：「施放者 作用目标 施放的主技能 当前技能 作用目标数」
     */
    runAfterSkill(skillinfo: { fromRole: Role, toRole: Role, fromSkill: SkillBase, skillConf: k_v<any>, targetCount: number; }, where?: string) {
        let skillconf = skillinfo.skillConf;
        let runSkillInfo = null;
        switch (skillconf.type) {
            case 1:
                // 主动技能
                console.error('主动技能不可通过runskill来调用');
                break;
            case 2:
                // 被动技能
                var passiveid = skillinfo.skillConf.skillid;
                skillinfo.toRole.clearAllPassiveSkill('clear', function (skill) {
                    return skill.skillConf.skillid == passiveid;
                });
                runPassiveSkillBase(skillinfo);
                break;
            case 3:
                // 攻击后技能
                runSkillInfo = RunAfterSkill.run(skillinfo, where);
                break;
        }
        return runSkillInfo;
    }

    addBuff(buffdata: buffDataType) { }
    getBuffByType(sxleixing: string | string[]) { return []; }
    getBuffByGroup(buffgroup: string) { return null; }
    hasbuff(buffid: string): boolean { return false; }
    clearAllBuff(why: string, filter?: (skill: k_v<any>) => boolean) { }
    clearAllPassiveSkill(why: string, filter?: (skill: k_v<any>) => boolean) { }
    clearBuff(sxleixing: string) { }
    debug(callback: () => void) {
        //callback();
    }
}


