-- 宝石相关逻辑
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local BagLogic = require("bag.BagLogic")
local LogDefine = require("common.LogDefine")
local HeroLogic = require("hero.HeroLogic")
local ObjHuman = require("core.ObjHuman")
local HeroExcel = require("excel.hero")
local Grid = require("bag.Grid")
local Util = require("common.Util")
local HeroDefine = require("hero.HeroDefine")

--宝石最大等级
local MAXGEMLEVEL = 10


--一级宝石对普通装备基础属性的加成
local BASEBONUS = 0.05

-- 一级宝石对戒指、护符基础属性的加成
local SPE_EQUIP_BASEBONUS = 0.05

-- 一级宝石对戒指、护符套装属性的加成
local SPE_EQUIP_SUITBONUS = 0.001

--一级宝石对装备套装效果的加成，key为套装，value为加成比例
local GEMTOEQUIPATTR = {
    [2] = 0.0025,
    [3] = 0.0025,
    [4] = 0.0025
}

--local inlayLog = "inlayGem"
local upgradeLog = "upgradeGem"

--组装下发给客户端的宝石数据
function assembleData(sourceTb, targetTb, job, pos)
    --只生成单个数据
    local jobToGem = HeroDefine.HEROJOBTOGEM
    if pos then
        if sourceTb[pos] then
            targetTb.id = job and jobToGem[job] or 0
            targetTb.pos = pos
            targetTb.lv = sourceTb[pos].lv
            targetTb.maxLv = MAXGEMLEVEL
            if targetTb.lv < MAXGEMLEVEL then
                targetTb.upcost = math.ceil((targetTb.lv + 1) / 3 )
            else
                targetTb.upcost = 0 --宝石达到最高级时，消耗数量为0
            end
        else
            targetTb.id = job and jobToGem[job] or 0
            targetTb.pos = pos
            targetTb.lv = 0
            targetTb.maxLv = MAXGEMLEVEL
            targetTb.upcost = math.ceil(1 / 3 )
        end
        return
    end

    --所有数据
    for i = 1, HeroDefine.HEROGEMMAX do
        if sourceTb and sourceTb[i] then
            local lv = sourceTb[i].lv
            targetTb[i] = {
                id = job and jobToGem[job] or 0,
                pos = sourceTb[i].pos,
                lv = lv,
                maxLv = MAXGEMLEVEL,
            }
            if lv < MAXGEMLEVEL then
                targetTb[i].upcost = math.ceil((lv + 1) / 3 )
            else
                targetTb[i].upcost = 0 --宝石达到最高级时，消耗数量为0
            end

        else
            targetTb[i] = {
                id = job and jobToGem[job] or 0,
                pos = i,
                lv = 0,
                maxLv = MAXGEMLEVEL,
            }
            targetTb[i].upcost = math.ceil( 1/ 3 )
        end
    end
end


--宝石对装备加成
--规则：只要有镶嵌宝石，那么就能对1-4孔位上装备的基础属性，装备套装效果有加成。
function suitAttrBonus(heroGrid)
    local gemData = heroGrid.gem
    if not gemData then
        return
    end

    local sumLevel = 0
    for _, v in pairs(gemData) do
        sumLevel = sumLevel + v.lv
    end
    
    if sumLevel > 0 then
        local t = {}
        for k, v in pairs(GEMTOEQUIPATTR)  do
            t[k] = v * sumLevel
        end
        --基础属性
        t.base = BASEBONUS * sumLevel

        -- 戒指, 护符加成
        t.sp_base = SPE_EQUIP_BASEBONUS * sumLevel
        t.sp_suit = SPE_EQUIP_SUITBONUS * sumLevel

        return t
    end
end


--获取宝石数据，以ItemData格式下发
function getGemData(human, heroID, heroIndex, pos)
    --位置是否超出装备位置
    if not pos or pos > HeroDefine.HEROGEMMAX then
        return 
    end

    local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
    if not heroGrid  then
        return
    end
    
    local heroCfg = HeroExcel.hero[heroID]
    if not heroCfg or not heroCfg.job then
        return
    end

    local itemId = HeroDefine.HEROJOBTOGEM[heroCfg.job]
    local itemCnt = 1
    
    --下发客户端
    local msgRet = Msg.gc.GC_HERO_GEM_LEVELUP_QUERY
    Grid.makeItem(msgRet.list, itemId, itemCnt)
    Msg.send(msgRet, human.fd)
end


--升级宝石
function upgradeGem(human, heroID, heroIndex, pos)
    --位置是否超出装备位置
    if not pos or pos > HeroDefine.HEROGEMMAX then
        return 
    end

    local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
	if not heroGrid  then
		return
	end

    --有没有穿装备
    if not heroGrid.equip or not heroGrid.equip[pos] then
        return
    end


    local heroCfg = HeroExcel.hero[heroID]
    if not heroCfg or not heroCfg.job then
        return
    end
    
    local itemId = HeroDefine.HEROJOBTOGEM[heroCfg.job]
    if not itemId then
        return
    end

    --英雄数据中没有宝石那就是镶嵌操作
    local isGen = true
    local lv = 0
    local gemData = heroGrid.gem or {}
    if gemData[pos] then
        lv = gemData[pos].lv
        isGen = false
    end


    --已经最高级
    if lv == MAXGEMLEVEL then
        return
    end
    

    lv = lv + 1
    local itemCnt = math.ceil(lv / 3)

    --宝石数量是否足够
    if not BagLogic.checkItemCnt(human, itemId, itemCnt) then
        return
    end

    --扣除道具
    BagLogic.delItem(human, itemId, itemCnt, upgradeLog)

    --更新数据
    if isGen then
        gemData[pos] = {
            pos = pos,
            lv = lv,
        }
        heroGrid.gem = gemData
    else
        gemData[pos].lv = lv
    end
    
    ObjHuman.doCalcHero(human, heroIndex)
    HeroLogic.sendHeroBagDynamic(human, heroID, heroIndex)
    HeroLogic.refreshDot(human, heroGrid.uuid)

    --通知客户端
    local msgRet = Msg.gc.GC_HERO_GEM_UPGRADEGEM
    -- msgRet.gemData[0] = HeroDefine.HEROGEMMAX
    assembleData(gemData, msgRet.gemData, heroCfg.job, pos)
    Msg.send(msgRet, human.fd)

    --更新宝石加成
    local queryFunc = require("hero.HeroEquip").query
    queryFunc(human, heroID, heroIndex)
end