-- 新商业化活动2——6元闯关
-- 玩法: 充值指定购买项后, 获得一次参与机会，通过权重随机出前进步数，获得1~最终位置的所有奖励
-- db
--[=[
        human.db.absAct[id] = {
            times = nil, --参与次数
            dailyBuyCnt = nil, --每日购买次数
        }
]=]--

local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local Lang = require("common.Lang")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local AbsActExcel = require("excel.absAct")
local Broadcast = require("broadcast.Broadcast")
local YunYingLogic = require("yunying.YunYingLogic")
local CycleActivityLogic = require("yunying.CycleActivity")
local actVariableCfg = require("excel.commercializationActivity").variable[2]
local breakThroughCfg = require("excel.commercializationActivity").breakThrough

-- 本活动日志标识
local LOGTAG = "cycleAct_breakThrough"

local DAILY_MAX_BUY_CNT = 50

local function getActData(human, actId)
    return human.db.absAct[actId]
end

local function updateActTimes(human, actId, value)
    local actData = getActData(human, actId)
    actData.times = (actData.times or 0) + value
end

local function updateActDailyBuyCnt(human, actId, value)
    local actData = getActData(human, actId)
    actData.dailyBuyCnt = (actData.dailyBuyCnt or 0) + value
    actData.dailyBuyCnt = math.max(actData.dailyBuyCnt, 0)
end


local function calcWeightSum()
    local weight = 0
    for _, v in ipairs(breakThroughCfg) do
        weight = weight + v.weight
    end

    return weight
end
-- 更新红点
local function updateRedDot(human, actId)
    YunYingLogic.sendBanner(human)
    local otherConfig = AbsActExcel.absActivity[actId]
    YunYingLogic.sendGroupUpdate(YYInfo[actId], human, otherConfig.panelID)
end



function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = CycleActivityLogic.isStarted(human, funcConfig.funcID)
    if not state then return end

    return true, endTime, startTime
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isRed(human, YYInfo, funcConfig)
    local actData = getActData(human, funcConfig.funcID)
    if not actData then
        return false
    end

    if not actData.times or actData.times <= 0 then
        return false
    end

    return true
end

function onCharge(human, price, funcID, buyID)
    local actData = getActData(human, funcID)
    if not actData then
        return
    end

    if buyID == actVariableCfg.va1[1] and (not actData.dailyBuyCnt or actData.dailyBuyCnt < DAILY_MAX_BUY_CNT) then
        local oldTimes = actData.times or 0

        updateActTimes(human, funcID, 1)
        updateActDailyBuyCnt(human, funcID, 1)
        Query(human, funcID)

        if oldTimes <= 0 then
            updateRedDot(human, funcID)
        end
    end
end

function updateDaily(human, funcID)
    local actData = getActData(human, funcID)
    if not actData then
        return
    end
    updateActDailyBuyCnt(human, funcID, -DAILY_MAX_BUY_CNT)
end



function Query(human, actId)
    local actData = getActData(human, actId)
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local msgRet = Msg.gc.GC_CYCLEBREAKTHROUGH_QUERY
    msgRet.state = 0
    if actData.times and actData.times > 0 then
        msgRet.state = 1
    end

    msgRet.maxDailyBuyCnt = DAILY_MAX_BUY_CNT
    msgRet.nowDailyBuyCnt = actData.dailyBuyCnt or 0
    BuyLogic.fontBuyItem(human, msgRet.buyMsg, actVariableCfg.va1[1])


    local gridList = msgRet.gridList
    for idx, gridCfg in ipairs(breakThroughCfg) do
        gridList[0] = idx
        local rewardList = gridList[idx].rewardList

        for k, itemCfg in ipairs(gridCfg.reward) do
            rewardList[0] = k
            Grid.makeItem(rewardList[k], itemCfg[1], itemCfg[2])
        end
    end

    Msg.send(msgRet, human.fd)
end

function RandPos(human, actId)
    local actData = getActData(human, actId)
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    if not actData.times or actData.times <= 0 then
        return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
    end

    local weightSum = calcWeightSum()
    local randWeight = math.random(1, weightSum)

    local itemVec = {}
    local len, randIdx, weight = 0, 0, 0

    for idx, gridCfg in ipairs(breakThroughCfg) do
        for _, itemCfg in ipairs(gridCfg.reward) do
            len = len + 1
            itemVec[len] = {itemCfg[1], itemCfg[2]}
        end

        weight = weight + gridCfg.weight
        if  weight >= randWeight then
            randIdx = idx
            break
        end
    end

    -- 扣除次数
    updateActTimes(human, actId, -1)

    local msgRet = Msg.gc.GC_CYCLEBREAKTHROUGH_RAND
    msgRet.randVal = randIdx
    msgRet.state = 1
    local itemArray = msgRet.itemArray

    for idx, itemInfo in ipairs(itemVec) do
        itemArray[0] = idx
        Grid.makeItem(itemArray[idx], itemInfo[1], itemInfo[2])

        BagLogic.addItem(human, itemInfo[1], itemInfo[2], LOGTAG)
    end


    if actData.times <= 0 then
        msgRet.state = 0

        updateRedDot(human, actId)
    end

    Msg.send(msgRet, human.fd)

    -- BagLogic.addItemList(human, itemVec, LOGTAG)
end