local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local BuyLogic = require("topup.BuyLogic")
local AbsActDefine = require("absAct.AbsActDefine")
local AbsActLogic = require("absAct.AbsActLogic")
local Log = require("common.Log")
local YunYingLogic = require("yunying.YunYingLogic")

ABS_ACT_ID = 9001
--
function initAfterHot()
    
end

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

--
function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end

--
function isRed(human, YYInfo, funcConfig)
    return false
end

--
function getLeftTime(human, YInfo, funcConfig)
    local ret, endTime, startTime = AbsActLogic.isStarted(human, ABS_ACT_ID)
    if ret == true then
        return endTime - os.time()
    else
        return 0
    end
end

function getNewHeroID(human)
    local startedFlag, endTime, startTime = AbsActLogic.isStarted(human, ABS_ACT_ID)
    if not startedFlag then return 0 end
    local absActConfig = AbsActExcel.absActivity[ABS_ACT_ID]
    if not absActConfig then return 0 end

    return absActConfig.icon
end

function getAndSendMsg(human, id, actId)
    local now = os.time()
    local startedFlag, endTime, startTime = AbsActLogic.isStarted(human, id)
    if not startedFlag then return end
    local absActConfig = AbsActExcel.absActivity[id]
    if not absActConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then return end

    local msgRet = Msg.gc.GC_ABS_NEWHERO
    msgRet.id = absActConfig.icon
    msgRet.startTime = startTime
    msgRet.endTime = endTime

    local index = 0
    for giftId, v in ipairs(AbsActExcel.absEva) do
        index = index + 1
        for j = 1, #v.reward do
            Grid.makeItem(msgRet.giftlist[index].item[j], v.reward[j][1], v.reward[j][2])
        end
        msgRet.giftlist[index].item[0] = #v.reward
        msgRet.giftlist[index].cnt = absAct.newheroCnt and absAct.newheroCnt[giftId] or 0
        msgRet.giftlist[index].id = giftId
        msgRet.giftlist[index].maxCnt = v.limit

        local buyID = v.buyID
        msgRet.giftlist[index].buyMsg[0] = 1
        if v.buyID == 0 then
            msgRet.giftlist[index].buyMsg[1] = {
                buyID = buyID,
                region = "zuanshi",
                cost = v.price,
                icon = 0,
                name = "",
                isFirst = 0,
                desc = "",
                doubleCnt = 0,
                actDoubleCnt = 0,
                useDoubleCnt = 0,
                buyCnt = 0,
                vipExp = 0,
                yuanjia = 0,
                zhekou = 0,
                voucher = 0,
                productId = "",
                nCanBuyNum = 0,
            }
        else
            BuyLogic.fontBuyItem(human, msgRet.giftlist[index].buyMsg[1], buyID)
        end
    end
    msgRet.giftlist[0] = index

    Msg.send(msgRet, human.fd)
end

function getgiftbybuyid(buyid)
    for giftId, v in ipairs(AbsActExcel.absEva) do
        if v.buyID == buyid then
            return giftId
        end
    end
end

function newheroGift(human, id, buyConf, isFirst, cnt, buyNum)
    local state = AbsActLogic.isStarted(human, id)
    local absConfig = AbsActExcel.absActivity[id]
    if absConfig == nil or not state then return end

    -- 存在多个特惠礼包 同时   开放
    local giftid = getgiftbybuyid(buyConf.id)
    if not giftid then return end

    local config = AbsActExcel.absEva[giftid]
    if not config then return end

    AbsActLogic.checkAbsActClean(human, id)

    human.db.absAct[id].newheroCnt = human.db.absAct[id].newheroCnt or { }
    local nowBuyCnt = human.db.absAct[id].newheroCnt[giftid] or 0

    -- 判断是否达到购买上限
    if nowBuyCnt >= config.limit then
        Broadcast.sendErr(human, Lang.HERO_BAG_BUY_CAP_NO_CNT)
        return
    end

    -- if nowBuyCnt + buyNum >= config.limit then
    --     Broadcast.sendErr(human, Lang.HERO_BAG_BUY_CAP_NO_CNT)
    --     return
    -- end

    -- 当金币购买之后超过最大金币时，不允许购买
    for j = 1, #config.reward do
        local itemID = config.reward[j][1]
        local itemCnt = config.reward[j][2]
        if itemID == ItemDefine.ITEM_JINBI_ID then
            if not ObjHuman.canAddJinbi(human, itemCnt) then
                return
            end
        end
    end

    -- 增加已购买次数
    -- human.db.absAct[id].newheroCnt[giftid] = nowBuyCnt + buyNum
    -- -- 发放物品
    -- local items = { }
    -- for _, v in ipairs(config.reward) do
    --     table.insert(items, {v[1], v[2] * buyNum})
    -- end
    
    -- BagLogic.addItemList(human, config.reward, "newhero_reward")
    human.db.absAct[id].newheroCnt[giftid] = nowBuyCnt + 1
    -- 发放物品
    -- local items = { }
    BagLogic.addItemList(human, config.reward, "newhero_reward")

    Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
    AbsActLogic.actDetailQuery(human, id)
end


function giftBuy(human, giftid)
    local state = AbsActLogic.isStarted(human, ABS_ACT_ID)
    if state ~= true then return end
    local absConfig = AbsActExcel.absActivity[ABS_ACT_ID]
    if not absConfig then return end

    AbsActLogic.checkAbsActClean(human, ABS_ACT_ID)

    -- 存在多个特惠礼包 同时   开放
    local config = AbsActExcel.absEva[giftid]
    if config.buyID ~= 0 then return end

    -- 初始化已购买次数
    human.db.absAct[ABS_ACT_ID].newheroCnt = human.db.absAct[ABS_ACT_ID].newheroCnt or { }
    local nowBuyCnt = human.db.absAct[ABS_ACT_ID].newheroCnt[giftid] or 0
    -- 判断是否达到购买上限
    if nowBuyCnt >= config.limit then
        Broadcast.sendErr(human, Lang.HERO_BAG_BUY_CAP_NO_CNT)
        return
    end

    local itemID = ItemDefine.ITEM_ZUANSHI_ID
    local itemCnt = config.price
    if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
        Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
        return
    end

    -- 当金币购买之后超过最大金币时，不允许购买
    for j = 1, #config.reward do
        local itemID = config.reward[j][1]
        local itemCnt = config.reward[j][2]
        if itemID == ItemDefine.ITEM_JINBI_ID then
            if not ObjHuman.canAddJinbi(human, itemCnt) then
                return
            end
        end
    end

    BagLogic.delItem(human, itemID, itemCnt, "newhero_reward")

    -- 增加已购买次数
    human.db.absAct[ABS_ACT_ID].newheroCnt[giftid] = nowBuyCnt + 1
    -- 发放物品
    -- local items = { }
    BagLogic.addItemList(human, AbsActExcel.absEva[giftid].reward, "newhero_reward")

    Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)

    AbsActLogic.actDetailQuery(human, ABS_ACT_ID)
end

function updateDaily(human, id)
    if human.db.absAct[ABS_ACT_ID] then
        human.db.absAct[ABS_ACT_ID] = {}
    end
    local state = AbsActLogic.isStarted(human, ABS_ACT_ID)
    if state ~= true then
        human.db.drawCard.list[8] = {}
    end
    AbsActLogic.actDetailQuery(human, ABS_ACT_ID)
 end

-- function GetRemainNum(human, nBuyID)
--     local config = AbsActExcel.absEva[nBuyID]
--     if not config then
--         return 0
--     end

--     local giftid = getgiftbybuyid(nBuyID)
--     if not giftid then return 0 end

--     if not human.db.absAct[ABS_ACT_ID].newheroCnt then
--         return config.limit
--     end

--     if not human.db.absAct[ABS_ACT_ID].newheroCnt[giftid] then
--         return config.limit
--     else 
--         return config.limit - human.db.absAct[ABS_ACT_ID].newheroCnt[giftid]
--     end
-- end