local BattleConst = require "app/module/battle/battle_const"

local BattleSkillConditionHandle = {}

local SKILL_CONDITION_REL_TYPE = BattleConst.SKILL_CONDITION_REL_TYPE
local SKILL_CONDITION_TYPE = BattleConst.SKILL_CONDITION_TYPE
local DEFAULT_FACTOR = BattleConst.DEFAULT_FACTOR

local function _judgeTargetState(buffCondition, conditionRel, target, battleController)
    local num = target:getBuffCountByName(buffCondition.attr)
    if num > 0 then -- 拥有这个buff
        if buffCondition.op == "<" then
            return false
        else
            return true
        end
    else
        if buffCondition.op == "<" then
            return true
        else
            return false
        end
    end
    return false
end

local function _judgeTargetAttr(buffCondition, conditionRel, target, battleController)
    local attrNum = 0
    if buffCondition.attr == "hpp" then
        attrNum = target.unitEntity:getHpPercent() * DEFAULT_FACTOR
    else
        attrNum = target.unitEntity:getTotalAttrValue(buffCondition.attr)
    end
    return BattleSkillConditionHandle._strOperatorOverloading(buffCondition.op, attrNum, buffCondition.v)
end

BattleSkillConditionHandle._judgeSkillEffectCondition = {
    [SKILL_CONDITION_TYPE.STATE] = _judgeTargetState,
    [SKILL_CONDITION_TYPE.ATTR] = _judgeTargetAttr,
}

BattleSkillConditionHandle._strOperatorOverloading = function(opStr, value1, value2)
    if opStr == "<" then
        return value1 < value2
    elseif opStr == "<=" then
        return value1 <= value2
    elseif opStr == "=" then
        return value1 == value2
    elseif opStr == ">" then
        return value1 > value2
    elseif opStr == ">=" then
        return value1 >= value2
    elseif opStr == "!=" then
        return value1 ~= value2
    end

    return false
end

function BattleSkillConditionHandle.judgeSkillEffectCondition(buffConditions, conditionRel, battleController, side)
    if not buffConditions or not conditionRel then
        return BattleConst.SKILL_CONDITION_RESULT.NO_CONDITION
    end

    local passConditionCount = 0
    for _, condition in ipairs(buffConditions) do
        local func = BattleSkillConditionHandle._judgeSkillEffectCondition[condition.type]
        if func then
            local target
            if condition.side == 1 then
                target = battleController:getCurActionTeam(side):getMainUnit()
            else
                target = battleController:getOtherSideTeam(side):getMainUnit()
            end

            if func(condition, conditionRel, target, battleController) then
                passConditionCount = passConditionCount + 1
            end
        end
    end

    if conditionRel == SKILL_CONDITION_REL_TYPE.AND then
        if passConditionCount >= #buffConditions then
            return BattleConst.SKILL_CONDITION_RESULT.CONDITION_PASS
        end
    elseif conditionRel == SKILL_CONDITION_REL_TYPE.OR then
        if passConditionCount > 0 then
            return BattleConst.SKILL_CONDITION_RESULT.CONDITION_PASS
        end
    end

    return BattleConst.SKILL_CONDITION_RESULT.NOT_PASS
end

return BattleSkillConditionHandle