import { PublicShared } from "../public/public";
import { FightControl } from "./fightCntrol";
import { FightObj } from "./fightType";
import { Player } from "./Player";
import { Role } from "./Role";

function _parseSpecialAtkTarget(side: number) {
    let targetType = "";

    switch (side) {
        case -1: // 自己
            targetType = 'self';
            break;
        case 0: // 友方
            break;
        case 1: // 敌方
            break;
        case 2: // 最后攻击自己的英雄
            targetType = 'lastHitMe';
            break;
        case 3: // 选择对位, 和我位置相同的敌方
            targetType = 'pos';
            break;
        case 4: // 主技能选敌
            targetType = 'lastSkillTargets';
            break;
        case 5: //友方死亡队友
            targetType = '0Dead';
            break;
        case 6: // 主动技能通过doatk，可能触发N个随机的目标其中一个
            targetType = 'SkillTargetsByOne';
            break;
        default:
            if (side >= 100) {
                targetType = 'skillTargets';
            }
            break;
    }
    return targetType;
}


function _checknear(roles: Role[], fromRole: FightObj) {
    if (roles.length == 0) return roles;
    let firstrole = roles[0];
    let roleArr = [];
    fromRole.fightControl.eachLiveRoles((role) => {
        let pos = role.roleData.pos as number;
        if ((firstrole.roleData.pos == pos * 1 + 1 || firstrole.roleData.pos == pos * 1 - 1) && role.roleData.side == firstrole.roleData.side) {
            roleArr.push(role);
        }
    });
    roleArr.push(firstrole)
    return roleArr;
}

function _checkOrder(roles: Role[], order: string, fromRole: FightObj) {
    if (roles.length <= 1 || order == "") return roles;
    if (order == 'rand') return roles.shuffle();
    if (order == "near") return roles.shuffle();
    let _orderAdd = order.split(',');
    roles.sort(function (a, b) {
        let valA: number, valB: number;
        if (a.roleData.attr[_orderAdd[0]] != null) {
            valA = a.roleData.attr[_orderAdd[0]];
            valB = b.roleData.attr[_orderAdd[0]];
        } else {
            switch (_orderAdd[0]) {
                case 'hp':
                    valA = a.roleData.attr.hp / a.roleData.attr.maxhp;
                    valB = b.roleData.attr.hp / b.roleData.attr.maxhp;
                    break;
                default:
                    valA = 0;
                    valB = 0;
                    console.error('选敌参数错误', order);
                    break;
            }
        }

        if (_orderAdd[1] == 'asc') {
            return valA - valB > 0 ? 1 : -1;
        } else if (_orderAdd[1] == 'desc') {
            return valB - valA > 0 ? 1 : -1;
        }
    });
    return roles;
};

function _limit(roles: Role[], v: number) {
    if (v > 0 && v < roles.length) {
        return roles.splice(0, v);
    }
    return roles;
};

function checkisVaild(roles: Role[]) {
    roles.forEach(v => {
        if (!v) roles.remove(v);
    });
    return roles
}

export function getAtkTarget(fightCntrol: FightControl, fromRole: FightObj, skillconf: k_v<any>) {
    let roles = _getAtkTarget(fightCntrol, fromRole, skillconf);
    fromRole.skillTargets[skillconf.skillid] = roles;
    return roles;
}

function _getAtkTarget(fightCntrol: FightControl, fromRole: FightObj, skillconf: k_v<any>) {
    let skillside = skillconf.side;
    let skillid = skillconf.skillid;
    let order = skillconf.order;
    let limit = skillconf.limit;
    if (typeof limit == 'string') {
        limit = PublicShared.eval(limit, { slv: fromRole.skillLv });
    }
    // let meihuo = fromRole.hasbuff('meihuo');
    let targetType = _parseSpecialAtkTarget(skillside);

    let _r = [];
    //特俗判断
    if (targetType != '') {
        switch (targetType) {
            // 自己
            case 'self':
                _r = _r.concat(fromRole);
                break;
            // 主动技能选敌
            case 'lastSkillTargets':
                _r = _r.concat(fromRole.lastSkillTargets);
                break;
            // 攻击自己的敌方
            case 'lastHitMe':
                _r = _r.concat(fromRole.lastHitMe);
                break;
            // 根据技能ID
            case 'skillTargets':
                _r = _r.concat(fromRole.getLastTargetsBySkillID(skillid));
                break;
            // 对位
            case 'pos':
                fightCntrol.eachLiveRoles((role) => {
                    if (role.roleData.pos == fromRole.roleData.pos && role.roleData.side != fromRole.roleData.side) {
                        _r.push(role)
                        return;
                    }
                });
                break;
            // 已死的友方
            case '0Dead':
                fightCntrol.eachDeadRoles((role) => {
                    if (role.roleData.side == fromRole.roleData.side) {
                        _r.push(role)
                        return;
                    }
                });
                break;
            // 主动技能通过doatk，可能触发N个随机的目标其中一个
            case 'SkillTargetsByOne':
                // _r = _r.concat(toRole);
                break;
            default:
                break;
        }
        _r = checkisVaild(_r)
        //如果没有选中，则随机选择一个英雄
        if (_r.length == 0 && targetType != '0Dead') {
            fromRole.fightControl.eachLiveRoles((role) => {
                if (role.roleData.side != fromRole.roleData.side) {
                    _r.push(role);
                    return
                }
            });
        }
        return _limit(_r, limit);
    }

    //过滤side
    let _roles = [];
    let sideObj = { 0: 1, 1: 0 };
    let _checkSide = skillside == 0 ? fromRole.roleData.side : sideObj[fromRole.roleData.side];
    // let checkSide = sideObj[skillside];

    fromRole.fightControl.eachLiveRoles((role) => {
        if (_checkSide != role.roleData.side) return;
        _roles.push(role);
    });
    if (_roles.length == 0) return _roles;
    _roles = _checkOrder(_roles, order, fromRole);

    if (order == "near") _roles = _checknear(_roles, fromRole);
    return _limit(_roles, limit);
}