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

local Util = require("common.Util")
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.commonact").store

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

local COMMONACTSHOPABSID = 7502        -- 对应ABS活动ID

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

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

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

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

function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID and funcConfig.funcID or COMMONACTSHOPABSID)
     if not state then
        print("[CommonActShop_isOpen] 当前活动未开启")
        return
    end
    
    print("[CommonActShop_isOpen] 进入判断 endTime = "..endTime.." startTime = "..startTime)
    return state, endTime, startTime
end

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

--查询
function CommonActShop_Query(human)
    CheckAndCreateDB(human)

    local state = AbsActLogic.isStarted(human, COMMONACTSHOPABSID)
    if not state then
        print("[CommonActShop_Query] 当前活动未开启")
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local actData = human.db.absAct[COMMONACTSHOPABSID]
    if not actData then
        print("[CommonActShop_Query] 不存在对应的活动数据")
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    print("[CommonActShop_Query] 正常进入")
    local buyRecord = actData.buyRecord
    local msgRet = Msg.gc.GC_ABS_FESTIVAL_SHOP_QUERY

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

    for k,v in ipairs(Config) do
        local singleItem = itemVec[k]
        local tempConfig = v
        Grid.makeItem(singleItem.itemData, tempConfig.item[1], tempConfig.item[2])
        singleItem.itemIndex = k
        Grid.makeItem(singleItem.needItem, tempConfig.currencyInfo[1], tempConfig.currencyInfo[2])
        singleItem.maxCanBuy = tempConfig.maxBuyCnt
        singleItem.nowBuy = buyRecord and buyRecord[k] or 0
        singleItem.zhekou  = 0
        singleItem.order  = 0
        singleItem.needVipLv = 0
        singleItem.rare = 0
        singleItem.limitType = 1
        singleItem.bCanChose = 0
        singleItem.bChose = 0
    end

    Msg.send(msgRet, human.fd)
end

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

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

    local itemCfg = Config[itemIdx]
    if not itemCfg or buyCnt <= 0 then
        print("[CommonActShop_Buy] 不正确的数据 itemIdx = "..itemIdx.." buyCnt = "..buyCnt)
        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)

    --下发数据
    CommonActShop_Query(human)
end