--境界系统
--[=[
    数据库存储数据： human.db.realmLv
    只存境界等级，境界的属性加成等则是实时计算
]=]--

local Msg = require("core.Msg")
local BagLogic = require("bag.BagLogic")
local realmConfig = require("excel.realm").Sheet1
local Grid = require("bag.Grid")
local RoleAttr = require("role.RoleAttr")
local RoleDefine = require("role.RoleDefine")
local ObjHuman = require("core.ObjHuman")
local Util = require("common.Util")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local TriggerLogic = require("trigger.TriggerLogic")
local TriggerDefine = require("trigger.TriggerDefine")
local TalismanLogic = require("talisman.TalismanLogic")

local GiftLogic

local REALM_UPGRADE_LOG = "realmUpgrade"

--加万分比的属性
local sp_attr = {
    [201] = 1,
    [202] = 1,
    [203] = 1,
    [204] = 1
}

-- 获取来自秘宝的属性倍数加成
local function getAttrMulFromTalisman(human)
    local attrMul = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.RoleReal_Attr_Mul)
    attrMul = attrMul / 100
    return attrMul
end

local function calAttrSum(net, lv, attrMul)
    attrMul = 1 + attrMul

    for i = 1, lv do
        local cfg = realmConfig[i]
        if cfg and cfg.attrs then
            for k,v in ipairs(cfg.attrs) do
                local id = v[1]
                local value = v[2]

                net[k] = net[k] or {}
                net[k].key = id

                if sp_attr[id] then
                    net[k].value = (net[k].value or 0) + (value / 100) * attrMul
                else
                    net[k].value = (net[k].value or 0) + value * attrMul
                end
            end
        end
    end
end

local function sendToclient(human)
    local realmLv = human.db and human.db.realmLv or 0
    local msgRet = Util.copyTable(Msg.gc.GC_ROLE_REALM_QUERY)

    local bl = false
    if realmLv <= 0 then
        bl = true
        msgRet.nowRealmName = ""
    end


    local nowRealmCfg = bl and realmConfig[1] or realmConfig[realmLv]
    if not nowRealmCfg then
        return
    end
    if not bl then
        msgRet.nowRealmName = nowRealmCfg.name
    end

    local attrMul = getAttrMulFromTalisman(human)
    local attrsCfg = nowRealmCfg.attrs
    msgRet.nowAttrs[0] = #attrsCfg

    if bl then
        for k,v in ipairs(attrsCfg) do
            msgRet.nowAttrs[k] = msgRet.nowAttrs[k] or {}
            msgRet.nowAttrs[k].key = v[1] or 0
            msgRet.nowAttrs[k].value = 0
        end
    else
        calAttrSum(msgRet.nowAttrs, realmLv, attrMul)
    end

    local itemId, itemCnt, nextRealmCfg
    if realmLv < #realmConfig then
        nextRealmCfg = realmConfig[realmLv + 1]
        if not nextRealmCfg then
            return
        end
        itemCnt = nextRealmCfg.itemCnt
        calAttrSum(msgRet.nextAttrs, realmLv+1, attrMul)
    else
        nextRealmCfg = realmConfig[realmLv]
        itemCnt = 0
        calAttrSum(msgRet.nextAttrs, realmLv, attrMul)
    end
    itemId = nextRealmCfg.itemId
    Grid.makeItem(msgRet.itemData, itemId, itemCnt)

    msgRet.nextRealmName = nextRealmCfg.name

    msgRet.nextAttrs[0] = #nextRealmCfg.attrs

    Msg.send(msgRet, human.fd)

    if BagLogic.getItemCnt(human, itemId) < itemCnt then
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_303)
    end
end



--境界对属性加成
function doCalcHero(human, attrs)
    if not human then
        return
    end

    local realmLv = human.db and human.db.realmLv or 0
    if realmLv <= 0 then
        return
    end

    local attrMul = getAttrMulFromTalisman(human)
    attrMul = 1 + attrMul

    for i=1, realmLv do
        local singleCfg = realmConfig[i]
        if singleCfg and singleCfg.attrs then
            for _, v in ipairs(singleCfg.attrs) do
                if v[2] > 0 then
                    RoleAttr.updateValue(v[1], v[2] * attrMul, attrs)
                end
            end
        end
    end
end

--获取境界名字
function getRealmName(human)
    local name = ""
    if  human.db and human.db.realmLv then
        local singleCfg = realmConfig[human.db.realmLv]
        if singleCfg and singleCfg.name then
            name = singleCfg.name
        end
    end
    return name
end

--获取当前境界
function GetNowRealmLv(human)
    local realmLv = human.db.realmLv or 0
    return realmLv
end

--红点判断
function isDot(human)
    local realmLv = human.db.realmLv or 0
    if realmLv >= #realmConfig then
        return false
    end

    local singleCfg = realmConfig[1]
    if realmLv > 0 then
        singleCfg = realmConfig[realmLv + 1]
    end
    if not singleCfg then
        return false
    end
    local itemId = singleCfg.itemId
    local itemCnt = singleCfg.itemCnt
    if BagLogic.getItemCnt(human, itemId) < itemCnt then
        return false
    end
    return true
end



--查询
function query(human)
    sendToclient(human)
end

--提升境界
function realmUpgrade(human)
    local realmLv = human.db.realmLv or 0

    if realmLv >= #realmConfig then
        return
    end

    realmLv = realmLv + 1
    local singleCfg = realmConfig[realmLv]
    if not singleCfg then
        return
    end

    local itemId = singleCfg.itemId
    local itemCnt = singleCfg.itemCnt
    if not BagLogic.checkItemCnt(human, itemId, itemCnt) then
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_303)
        return
    end

    BagLogic.delItem(human, itemId, itemCnt, REALM_UPGRADE_LOG)
    human.db.realmLv = realmLv
    sendToclient(human)
    RoleAttr.cleanHeroAttrCache(human)
    RoleAttr.doCalc(human)
    ObjHuman.sendAttr(human, RoleDefine.ZHANDOULI)
    ObjHuman.doCalc(human)

    TriggerLogic.PublishEvent(TriggerDefine.REALM_UPGRADE, human.db._id, 1, realmLv)

    -- 弹窗礼包
    GiftLogic = GiftLogic or require("topup.GiftLogic")
    GiftLogic.trigger(human, GiftLogic.GIFT_REALM_UPGRADE_STAR, {currentVal = realmLv}, GiftLogic.GIFT_SEC_TYPE3)
end