-- 新商业化活动2——英雄赠礼
-- 玩法：多条配置奖励, 有的免费领取，有的需要先充值，但是所有奖励需要逐条领取。
-- db
--[=[
        human.db.absAct[id] = {
            gridData = { -- 当前格子
                nowIdx = 1, -- 从第一个开始
                state = 0,  -- 0-不可领, 1-领可取
            }
        }
]=]--

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 CommonDefine = require("common.CommonDefine")
local CycleActivityLogic = require("yunying.CycleActivity")
local heroSendGiftCfg = require("excel.commercializationActivity").heroSendGift


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


local function initData(human, actId)
    human.db.absAct[actId] = human.db.absAct[actId] or {}
    human.db.absAct[actId].gridData = {
        nowIdx = 1,
        state = CommonDefine.COMMON_PRIZE_STATE_NOGET,
    }
end

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

local function updateActData(human, actId)
    local actData = getActData(human, actId)
    actData.gridData.state = CommonDefine.COMMON_PRIZE_STATE_CANGET
end

local function nextGrid(human, actId, nextIdx)
    if not nextIdx then
        return
    end
    local actData = getActData(human, actId)
    actData.gridData.nowIdx = nextIdx
    actData.gridData.state = CommonDefine.COMMON_PRIZE_STATE_NOGET
end



local function checkGridState(human, actId, nowBuyId)
    local actData = getActData(human, actId)
    if not actData then
        return
    end

    local gridData = actData.gridData
    local nowIdx = gridData and gridData.nowIdx or -1

    local gridCfg = heroSendGiftCfg[nowIdx]
    if not gridCfg then
        return
    end

    local bl = false
    if gridCfg.buyId == 0 then
        bl = true
    else
        if nowBuyId and gridCfg.buyId == nowBuyId then
            bl = true
        end
    end

    if bl then
        updateActData(human, actId)
    end

    return bl
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 or not actData.gridData then
        return false
    end

    if actData.gridData.state == CommonDefine.COMMON_PRIZE_STATE_CANGET then
        return true
    end

    return false
end

function onLogin(human, funcID)
    local state = CycleActivityLogic.isStarted(human, funcID)
    if not state then return end

    local actData = getActData(human, funcID)
    if not actData then
        return false
    end

    if not actData.gridData or not actData.gridData.nowIdx then
        initData(human, funcID)
        checkGridState(human, funcID)
    end
end

function onCharge(human, price, funcID, buyID)
    local state = CycleActivityLogic.isStarted(human, funcID)
    if not state then return end

    local res = checkGridState(human, funcID, buyID)
    if res then
        -- 推送数据更新
        Query(human, funcID)
        -- 红点
        updateRedDot(human, funcID)
    end
end



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

    if not actData.gridData or not actData.gridData.nowIdx then
        initData(human, actId)
        checkGridState(human, actId)
    end

    local gridData = actData.gridData
    local nowGridState = gridData.state
    local nowIdx = gridData.nowIdx
    if nowIdx > #heroSendGiftCfg then
        nowIdx = #heroSendGiftCfg
        nowGridState = CommonDefine.COMMON_PRIZE_STATE_GET
    end


    local msgRet = Msg.gc.GC_CYCLHEROSENDGIFT_QUERY
    local gridList = msgRet.gridList


    for k, v in ipairs(heroSendGiftCfg) do
        gridList[0] = k

        if k == nowIdx then
            gridList[k].state = nowGridState
        elseif k < nowIdx then
            gridList[k].state = CommonDefine.COMMON_PRIZE_STATE_GET
        else
            gridList[k].state = 3
        end

        BuyLogic.fontBuyItem(human, gridList[k].buyMsg, v.buyId)

        local itemList = gridList[k].itemList
        for len, itemCfg in ipairs(v.reward) do
            itemList[0] = len
            Grid.makeItem(itemList[len], itemCfg[1], itemCfg[2])
        end
    end

    Msg.send(msgRet, human.fd)
end

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

    local gridData = actData.gridData
    local nowGridState = gridData.state

    if nowGridState ~= CommonDefine.COMMON_PRIZE_STATE_CANGET then
        return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
    end

    local nowIdx = gridData.nowIdx
    local gridCfg = heroSendGiftCfg[nowIdx]
    if not gridCfg then
        return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
    end

    local itemVec = {}
    for l, itemCfg in ipairs(gridCfg.reward) do
        itemVec[l] = { itemCfg[1], itemCfg[2] }
    end

    nextGrid(human, actId, nowIdx+1)
    local bl = checkGridState(human, actId)
    if not bl then
        updateRedDot(human, actId)
    end

    BagLogic.addItemList(human, itemVec, LOGTAG)

    Query(human, actId)
end