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 CycleActivityLogic = require("yunying.CycleActivity")

--[[
    absAct.xlxs-wishGift
    新英雄来袭-夙愿礼盒
        1.默认初始化礼盒数据
        2.礼盒分直购和钻石购买
    DB:
        human.db.absAct[id] = {
            premiumCnt = {
               xxx = xxx,          -- 配置的礼包id = 购买数量 
            }
        }
    public:
        initAfterHot()              -- 初始礼盒数据
        getAndSendMsg()             -- 发送活动数据
        isRed()                     -- 红点提醒
        isActive()                  -- 激活状态
        isOpen()                    -- 活动开启   、
        getLeftTime()               -- 得到活动剩余时间(YunYingLogic统一调用)
        wishGift()                  -- 直购礼盒(统一从YunYingLogic.onBuy中调用)
        giftBuy()                   -- 钻石购买礼盒
--]]

ABS_PREMIUM_GIFT_LIST_ACTID = { }   -- 所有礼盒数据配置id
ABS_PREMIUM_GIFT_LIST_BUYID = { }   -- 所有需要直购的礼盒数据 buyID => 配置id
function initAfterHot()
    ABS_PREMIUM_GIFT_LIST_ACTID = {}
    for k, v in pairs(AbsActExcel.wishGift) do
        ABS_PREMIUM_GIFT_LIST_ACTID[v.actId] = ABS_PREMIUM_GIFT_LIST_ACTID[v.actId] or { }
        ABS_PREMIUM_GIFT_LIST_ACTID[v.actId][#ABS_PREMIUM_GIFT_LIST_ACTID[v.actId] + 1] = k
        if v.buyID ~= 0 then
            ABS_PREMIUM_GIFT_LIST_BUYID[v.buyID] = k
        end
    end
end

function getAndSendMsg(human, id, actId)
    -- local startedFlag, endTime, startTime = AbsActLogic.isStarted(human, id)
    local startedFlag, endTime, startTime = CycleActivityLogic.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_ACT_WISH_GIFT_QUERY
    msgRet.templateId = absActConfig.panelID
    msgRet.ad = absActConfig.adIcon
    msgRet.absActId = id
    msgRet.status = absAct.status or 0

    local index = 0
    for k, giftId in pairs(ABS_PREMIUM_GIFT_LIST_ACTID[absActConfig.actId]) do
        local v = AbsActExcel.wishGift[giftId]
        if v then
            index = index + 1
            for j = 1, #v.reward do
                Grid.makeItem(msgRet.premiumGiftMsg[index].item[j], v.reward[j][1], v.reward[j][2])
            end
            msgRet.premiumGiftMsg[index].item[0] = #v.reward
            msgRet.premiumGiftMsg[index].cnt = absAct.premiumCnt and absAct.premiumCnt[giftId] or 0
            msgRet.premiumGiftMsg[index].id = giftId
            msgRet.premiumGiftMsg[index].maxCnt = v.cnt
            msgRet.premiumGiftMsg[index].icon = v.icon or 0
            msgRet.premiumGiftMsg[index].name = v.name or ""

            local buyID = v.buyID
            msgRet.premiumGiftMsg[index].buyMsg[0] = 0
            msgRet.premiumGiftMsg[index].needItem[0] = 0
            if v.buyID == 0 then
                local len = 0
                for _, item in ipairs(v.needItem) do
                    len = len + 1
                    Grid.makeItem(msgRet.premiumGiftMsg[index].needItem[len], item[1], item[2])
                end
                msgRet.premiumGiftMsg[index].needItem[0] = len
            else
                BuyLogic.fontBuyItem(human, msgRet.premiumGiftMsg[index].buyMsg[1], buyID)
                msgRet.premiumGiftMsg[index].buyMsg[0] = 1
            end
        end
    end
    msgRet.premiumGiftMsg[0] = index
    msgRet.startTime = startTime
    msgRet.endTime = endTime
    Msg.send(msgRet, human.fd)
end

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

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

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

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

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

    -- 存在多个特惠礼包 同时   开放
    local premiumID = ABS_PREMIUM_GIFT_LIST_BUYID[buyConf.id]
    if not premiumID then return end

    local config = AbsActExcel.wishGift[premiumID]
    if not config then return end

    if config.actId ~= absConfig.actId then return end

    -- AbsActLogic.checkAbsActClean(human, id)

    human.db.absAct[id].premiumCnt = human.db.absAct[id].premiumCnt or { }
    local nowBuyCnt = human.db.absAct[id].premiumCnt[premiumID] or 0

    -- 判断是否达到购买上限
    if nowBuyCnt >= config.cnt 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].premiumCnt[premiumID] = nowBuyCnt + 1
    -- 发放物品
    -- local items = { }
    BagLogic.addItemList(human, AbsActExcel.wishGift[premiumID].reward, "abs_wish_gift_buy")

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

function giftBuy(human, id, premiumID)
    -- local state = AbsActLogic.isStarted(human, id)
    local state = CycleActivityLogic.isStarted(human, id)
    if state ~= true then return Broadcast.sendErr(human, Lang.ACT_WAS_OVER) end

    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    -- AbsActLogic.checkAbsActClean(human, id)

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

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

    for _, item in ipairs(config.needItem) do
        local itemID = item[1]
        local itemCnt = item[2]
        if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
            Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
            return
        end
    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

    for _, item in ipairs(config.needItem) do
        local itemID = item[1]
        local itemCnt = item[2]
        BagLogic.delItem(human, itemID, itemCnt, "abs_wish_gift_reward")
    end



    -- 增加已购买次数
    human.db.absAct[id].premiumCnt[premiumID] = nowBuyCnt + 1
    -- 发放物品
    -- local items = { }
    BagLogic.addItemList(human, AbsActExcel.wishGift[premiumID].reward, "abs_wish_gift_reward")

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