import { getAtkTarget } from "../atkTarget";
import { getSkillConf, randNumber } from "../fightFun";
import { atkRoleInfo_Type, dpsinfoType, FightObj } from '../fightType';
import { Player } from "../Player";
import { Role } from "../Role";

export class SkillBase {
    toRole: Role = null;
    constructor(public fromRole: FightObj, public skillconf: k_v<any>) {
        this.fromRole.fightControl.event.on('fightEvent_onRoleClear', this.onRoleClear, this);
        this.checkAtk();
    }

    release(){
        this.clear();
        this.toRole = null;
    }

    onRoleClear(why?: string) {

    }

    clear() {
        this.fromRole.fightControl.event.off('fightEvent_onRoleClear', this.onRoleClear, this);
    }

    /**
     * 攻击前检查、选敌
     */
    checkAtk() {
        let targets = getAtkTarget(this.fromRole.fightControl, this.fromRole, this.skillconf);
        if (targets.length == 0) {
            this.fromRole.debug(() => {
                console.log('没有选到敌人-->', this.skillconf.skillid);
            });
        }
        this.fromRole.lastSkillTargets = targets;
        this.fromRole.lastSkillId = this.skillconf.skillid;

        targets.forEach((role: Role) => {
            role.lastHitMe = this.fromRole;
        });

        this.fromRole.fightControl.record({
            act: 'atk',
            fromRole: this.fromRole.rid,
            targets: [],
            skillId: this.skillconf.skillid,
            rolesInfo: {}
        });
        let recordIdx = this.fromRole.fightControl.getRecordIdx();
        let atkInfo = this.doAtk();
        this.fromRole.fightControl.fixRecordByIdx(recordIdx, atkInfo);

        this.fromRole.bloodReturn(atkInfo);
        this.fromRole.fightControl.event.emit('fightEvent_atkOver');
        atkInfo.targets.forEach(_role => {
            this.fromRole.fightControl.event.emit('fightEvent_masterSkills', {
                fromRole: this.fromRole,
                toRole: this.fromRole.fightControl.fightRoles[_role],
                skillId: this.skillconf.skillid,
                skillType: "masterSkills",
            });
        });
        this.clear();
    }

    doAtk() {
        //解析攻击后技能
        let skillType3: { targets: string[], rolesInfo: k_v<atkRoleInfo_Type>; } =
        {
            targets: [],
            rolesInfo: {}
        };

        let skills = this.skillconf.v;
        this.fromRole.fightControl.recordSkillDps[this.skillconf.skillid] = 0;
        skills.forEach((skillid: string, k) => {
            let askillconf = getSkillConf(skillid);
            if (!askillconf) return console.log('没有技能配置-->', skillid);
            let randnum = askillconf.randnum;
            if (randnum != '' && randNumber() > randnum) return;

            // 选择目标敌人
            let targets = getAtkTarget(this.fromRole.fightControl, this.fromRole, askillconf);
            if (targets.length == 0) {
                this.fromRole.debug(() => {
                    console.log('没有选到敌人-->', this.skillconf.skillid);
                });
            }
            targets.forEach((role: Role) => {
                let ov = role.getData('hp');
                let info = role.runAfterSkill({ fromRole: this.fromRole, toRole: role, fromSkill: this, skillConf: askillconf, targetCount: targets.length }, "skillBase") as dpsinfoType;
                if (!info) return;
                if (!skillType3.rolesInfo[role.rid]) {
                    skillType3.rolesInfo[role.rid] = {} as atkRoleInfo_Type;
                    skillType3.rolesInfo[role.rid]['ov'] = ov;
                }
                skillType3.rolesInfo[role.rid]['baoji'] = info.baoji;
                skillType3.rolesInfo[role.rid]['miss'] = info.miss;
                skillType3.rolesInfo[role.rid]['nv'] = role.getData('hp');
                /**记录技能类型为3的伤害 */
                if (skillType3.targets.indexOf(role.rid) == -1) {
                    skillType3.targets.push(role.rid);
                }

                this.fromRole.fightControl.recordSkillDps[this.skillconf.skillid] += Math.abs(info.dps);
            });

            this.fromRole.fightControl.event.emit('fightEvent_dodamage', {
                skillid: this.skillconf.skillid,
                fromRole: this.fromRole,
                toRole: this.toRole
            });
        });

        return skillType3;
    }
}