--新商业化活动3—天选兑换
--db
--[=[
    human.db.absAct[id] = {
        buyRecord = {
            [1] = 2, --key是道具在配置中的id, value是已经购买次数
            [2] = 1,
        }
    }
]=]--

local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local AbsActLogic = require("absAct.AbsActLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Config = require("excel.BusThreeActivity").DiscountStore

local LOGTAG = "BusThreeDiscountStore" --日志标识

local BUSTHREDISCOUNTSTOREABSID = 7403        -- 对应ABS活动ID

local function CreateDB(human)
    if not human.db.absAct[BUSTHREDISCOUNTSTOREABSID] then
        human.db.absAct[BUSTHREDISCOUNTSTOREABSID] = {}
    end

    if not human.db.absAct[BUSTHREDISCOUNTSTOREABSID].buyRecord then
        human.db.absAct[BUSTHREDISCOUNTSTOREABSID].buyRecord = {}
    end

    for index, v in ipairs(Config) do
        human.db.absAct[BUSTHREDISCOUNTSTOREABSID].buyRecord[index] = 0
    end
end

local function CheckAndCreateDB(human)
    if not human.db.absAct[BUSTHREDISCOUNTSTOREABSID] or not human.db.absAct[BUSTHREDISCOUNTSTOREABSID].buyRecord then
        CreateDB(human)
    end
end

function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID and funcConfig.funcID or BUSTHREDISCOUNTSTOREABSID)

    return state, endTime, startTime
end

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

-- function isRed(human, YYInfo, absActConfig)
--     local state = AbsActLogic.isStarted(human, BUSTHREDISCOUNTSTOREABSID)
--     if not state  then
--         return false
--     end

--     CheckAndCreateDB(human)

--     for nID, v in ipairs(Config) do
        
--         return true
--     end

--     return false
-- end


--查询
function Query(human, id, isBuy)
    CheckAndCreateDB(human)

    local state = AbsActLogic.isStarted(human, BUSTHREDISCOUNTSTOREABSID)
    if not state then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local actData = human.db.absAct[BUSTHREDISCOUNTSTOREABSID]
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local buyRecord = actData.buyRecord
    local msgRet = Msg.gc.GC_NEW_BUSTHREEACT_DISQUERY

    local itemVec = msgRet.titemData
    itemVec[0] = #Config

    for k,v in ipairs(Config) do
        local singleItem = itemVec[k]
        singleItem.idx = k
        singleItem.maxBuyCnt = v.maxBuyCnt
        singleItem.nowBuyCnt = buyRecord and buyRecord[k] or 0
        Grid.makeItem(singleItem.item, v.item[1], v.item[2])
        Grid.makeItem(singleItem.currencyInfo, v.currencyInfo[1], v.currencyInfo[2])
    end

    Msg.send(msgRet, human.fd)
end

--购买商品
function BuyItem(human, id, itemIdx, buyCnt)
    local state = AbsActLogic.isStarted(human, id)
    if not state then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local actData = human.db.absAct[BUSTHREDISCOUNTSTOREABSID]
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local itemCfg = Config[itemIdx]
    if not itemCfg or buyCnt <= 0 then
        return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
    end

    local buyRecord = actData.buyRecord
    if buyRecord and buyRecord[itemIdx] then
        if buyRecord[itemIdx] + buyCnt > itemCfg.maxBuyCnt then
            return Broadcast.sendErr(human, Lang.COMMON_BUY_NUM_NOT_ENOUGH)
        end
    end

    local currencyInfo = itemCfg.currencyInfo
    local cnt = currencyInfo[2] * buyCnt
    if BagLogic.getItemCnt(human, currencyInfo[1]) < cnt then
        return Broadcast.sendErr(human, Lang.COMMON_ITEM_NOT_ENOUGH)
    end
    BagLogic.delItem(human, currencyInfo[1], cnt, LOGTAG)

    --更新数据
    buyRecord = buyRecord or {}
    buyRecord[itemIdx] = (buyRecord[itemIdx] or 0) + buyCnt
    actData.buyRecord = buyRecord

    --发放奖励
    local itemInfo = itemCfg.item
    BagLogic.addItemList(human, {{itemInfo[1], itemInfo[2] * buyCnt}}, LOGTAG)

    --下发数据
    Query(human, id, true)
end