-- 异界寻宝

local Msg = require("core.Msg")
local Lang = require("common.Lang")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local AnotherWorldBattleConfig = require("excel.anotherWorldBattle")

local TREASURELOG = "anotherWorldBattleTreasure"

local ITEMID_2_TIP = {
    [185] = Lang.AB_MIYAO_NOT_ENOUG
}


local showItemTArr = { {1044, 10}, {1045,1}, {1046,1}, {1047,1}, {1048,1}  }


local function initData(human)
    human.db.anotherWordTreasureTimes = 0
end

local function getData(human)
    if not human.db.anotherWordTreasureTimes then
        initData(human)
    end
    return human.db.anotherWordTreasureTimes
end

local function updateData(human, lotteryTimes)
    human.db.anotherWordTreasureTimes = (human.db.anotherWordTreasureTimes or 0) + lotteryTimes
end


-- 根据当前寻宝次数计算出寻宝等级
local function getTreasureLv(human)
    local lotteryTimes = getData(human)

    local treasureLv = 0
    if not lotteryTimes or lotteryTimes <= 0 then
        return treasureLv
    end

    for _, treasureCfg in pairs(AnotherWorldBattleConfig.treasureLv) do
        if lotteryTimes >= treasureCfg.condLotteryNum and treasureCfg.treasureLv > treasureLv then
            treasureLv = treasureCfg.treasureLv
        end
    end

    return treasureLv
end

-- 获取当前寻宝等级的展示道具
-- local function getShowItemByLv(targetTreasureLv)
--     local tb = {}
--     for _, treasureCfg in pairs(AnotherWorldBattleConfig.treasureAwardPool) do
--         if targetTreasureLv == treasureCfg.treasureLv and treasureCfg.isShow == 1 then
--             tb[#tb+1] = treasureCfg.item
--         end
--     end

--     return tb
-- end

-- 填充协议结构
local function populateMsg(net, itemArr)
    net[0] = 0

    for i, itemInfo in ipairs(itemArr) do
        net[0] = i
        Grid.makeItem(net[i], itemInfo[1], itemInfo[2])
    end
end

-- 填充协议结构2
local function populateMsg2(net, weightArr, isMaxLv)
    local len = 0

    if isMaxLv == 1 then
        for _, weightTb in ipairs(weightArr) do
            for i=1, 2 do
                len = len + 1
                net[len] = weightTb[2]
            end
        end
    else
        for _, weightTb in ipairs(weightArr) do
            for _, weight in ipairs(weightTb) do
                len = len + 1
                net[len] = weight
            end
        end
    end

    net[0] = len
end

-- 生成当前等级的抽奖数据
local function genLotteryData(targetTreasureLv)
    local totalWeight = 0
    local itemPool = {}

    for _, treasureCfg in pairs(AnotherWorldBattleConfig.treasureAwardPool) do
        if targetTreasureLv == treasureCfg.treasureLv and treasureCfg.weight > 0 then
            totalWeight = totalWeight + treasureCfg.weight
            itemPool[#itemPool+1] = { treasureCfg.weight, treasureCfg.item[1], treasureCfg.item[2] }
        end
    end

    return totalWeight, itemPool
end


-- 查询
function AB_Treasure_Query(human)
    local nowLv = getTreasureLv(human)
    local msgRet = Msg.gc.GC_AB_TREASURE_QUERY
    msgRet.treasureLv = nowLv
    msgRet.lotteryTimes = getData(human)
    msgRet.nextLvCondTimes = 0

    local maxLv = #AnotherWorldBattleConfig.treasureLv
    local nextLv = nowLv + 1
    if nextLv >= maxLv then
        nextLv = maxLv
    end

    msgRet.isMaxLv = nowLv == maxLv and 1 or 0
    local nextLvCfg = AnotherWorldBattleConfig.treasureLv[nextLv]
    msgRet.nextLvCondTimes = nextLvCfg.condLotteryNum


    local awardPoolMsg = msgRet.awardPool
    -- local showItemArr = getShowItemByLv(nowLv)
    awardPoolMsg[0] = #showItemTArr
    for i, itemInfo in ipairs(showItemTArr) do
        Grid.makeItem(awardPoolMsg[i], itemInfo[1], itemInfo[2])
    end

    local costCfg  = AnotherWorldBattleConfig.var[1]
    Grid.makeItem(msgRet.lottery1Cost, costCfg.lottery1Cost[1][1], costCfg.lottery1Cost[1][2])
    Grid.makeItem(msgRet.lottery10Cost, costCfg.lottery10Cost[1][1], costCfg.lottery10Cost[1][2])

    populateMsg(msgRet.lottery2Cost, costCfg.lottery2Cost)
    populateMsg(msgRet.lottery20Cost, costCfg.lottery20Cost)
    populateMsg2(msgRet.nextLvProArr, nextLvCfg.probabilityArr, msgRet.isMaxLv)

    Msg.send(msgRet, human.fd)
end

-- 抽奖
function AB_Treasure_Lottery(human, lotteryNum)
    local costItem
    local costCfg = AnotherWorldBattleConfig.var[1]

    if lotteryNum == 1 then
        costItem = costCfg.lottery1Cost
    elseif lotteryNum == 2 then
        costItem = costCfg.lottery2Cost
    elseif lotteryNum == 10 then
        costItem = costCfg.lottery10Cost
    elseif lotteryNum == 20 then
        costItem = costCfg.lottery20Cost
    end

    if not costItem then
        return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
    end

    -- 道具判断
    for _, itemTb in ipairs(costItem) do
        local itemId = itemTb[1]
        local itemNum = itemTb[2]
        if BagLogic.getItemCnt(human, itemId) < itemNum then
            local tips = ITEMID_2_TIP[itemId] or Lang.COMMON_NO_ZUANSHI
            return Broadcast.sendErr(human, tips)
        end
        BagLogic.delItem(human, itemId, itemNum, TREASURELOG)
    end

    -- 开始抽奖
    local weight = 0
    local itemArr = {}
    local nowLv = getTreasureLv(human)
    local totalWeight, itemPool = genLotteryData(nowLv)
    for i=1, lotteryNum do
        weight = 0
        local randWeight = math.random(0, totalWeight)
        for _, v in ipairs(itemPool) do
            weight = weight + v[1]
            if randWeight <= weight then
                itemArr[#itemArr+1] = {v[2], v[3]}
                break
            end
        end
    end

    -- 更新抽奖次数
    updateData(human, lotteryNum)
    -- 发抽奖所得奖励
    BagLogic.addItemList(human, itemArr, TREASURELOG)

    -- 推送最新数据到客户端
    local newLv = getTreasureLv(human)
    if newLv == nowLv then
        local msgRet = Msg.gc.GC_AB_TREASURE_LOTTERY
        msgRet.lotteryTimes = getData(human)
        return Msg.send(msgRet, human.fd)
    end

    AB_Treasure_Query(human)
end