-- ABS 周卡

local Config = require("Config")
local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local AbsActDefine = require("absAct.AbsActDefine")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local MangHeLogic = require("absAct.MangHeLogic")
local MailExcel = require("excel.mail")
local MailManager = require("mail.MailManager")
local CommonDB = require("common.CommonDB")
local AbsActLogic = require("absAct.AbsActLogic")
local HeroGrid = require("hero.HeroGrid")
local DrawCardLogic = require("drawCard.DrawCardLogic")
local Log = require("common.Log")
local HeroBook = require("hero.HeroBook")
local HeroExcel = require("excel.hero")
local HeroLogic = require("hero.HeroLogic")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local Grid = require("bag.Grid")
local BuyLogic = require("topup.BuyLogic")
local BuyExcel = require("excel.buy")
local TopupLogic = require("topup.TopupLogic")
local MailExcel = require("excel.mail")
local MailManager = require("mail.MailManager")
local BagLogic = require("bag.BagLogic")

DAY_CNT = 7
local nABSWEEKLYCARDID = 12001

local function initDB(human, id, actId)
    if not human.db.absAct[id] then return end
    human.db.absAct[id][actId] = { dayCnt = DAY_CNT }
end

function isOpen(human, YYInfo, funcConfig)
    if not human.db.nFirstBuy or 1 ~= human.db.nFirstBuy then
        return false
    end
    
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end

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

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

function getAndSendMsg(human, id, config)
    local msgRet = Msg.gc.GC_ABS_WEEK_LYCARD_QUERY
    msgRet.leftTime = 0
    msgRet.actId = id

    local actId = config.actId
    local weeklyCardConfig = AbsActExcel.absWeeklyCard
    if not weeklyCardConfig then return end

    local buyItemConfig = BuyExcel.libao[TopupLogic.TOPUP_LIBAO_ID2]
    if not buyItemConfig then return end

    local absAct = human.db.absAct[id]
    msgRet.isActive = absAct and absAct[config.actId] and 1 or 0

    local len = 0
    for k, item in ipairs(weeklyCardConfig) do
        len = len + 1
        if len > #msgRet.weekLyCard then
            break
        end

        local net = msgRet.weekLyCard[len]

        net.id = k
        net.items[0] = #item[actId]
        for i = 1, #item[actId] do
            Grid.makeItem(net.items[i], item[actId][i][1], item[actId][i][2])
        end
    end
    msgRet.weekLyCard[0] = len

    msgRet.buyItem.item[0] = #buyItemConfig.reward
    for i = 1, #buyItemConfig.reward do
        Grid.makeItem(msgRet.buyItem.item[i], buyItemConfig.reward[i][1], buyItemConfig.reward[i][2])
    end

    msgRet.buyItem.cnt = isActive and 1 or 0
    msgRet.buyItem.id = TopupLogic.TOPUP_LIBAO_ID2
    msgRet.buyItem.maxCnt = buyItemConfig.cnt
    msgRet.buyItem.icon = buyItemConfig.icon or 0
    msgRet.buyItem.name = buyItemConfig.name or ""
    BuyLogic.fontBuyItem(human, msgRet.buyItem.buyMsg[1], buyItemConfig.buyID)
    msgRet.buyItem.buyMsg[0] = 1
    msgRet.buyItem.needItem[0] = 0
    
    Msg.send(msgRet, human.fd)
end

function updateDaily(human, id, findActId)
    local config = AbsActExcel.absActivity[id]
    if not config then return end

    -- 有无激活记录
    local absAct = human.db.absAct[id]
    if not absAct then return end

    -- 发放奖励
    local clean = false
    for actId, v in pairs(absAct) do
        -- 是否只发放当期奖励
        local bSend = false
        if findActId == nil or
            findActId == actId then
            bSend = true
        end

        if type(v) == "table" and v.dayCnt and v.dayCnt > 0 and bSend then

            v.dayCnt = v.dayCnt - 1

            local dayIndex = DAY_CNT - v.dayCnt
            local weeklyCardConfig = AbsActExcel.absWeeklyCard[dayIndex]
            if weeklyCardConfig then
                -- 减去一天
                local mailId = config.param[1]
                local mailConfig = MailExcel.mail[mailId]
                local title = mailConfig.title
                local senderName = mailConfig.senderName
                local content = Util.format(mailConfig.content, DAY_CNT, DAY_CNT - v.dayCnt) 
                MailManager.add(MailManager.SYSTEM, human.db._id, title, content, weeklyCardConfig[actId], senderName)
            end
            
            clean = true
            -- 有数据就 跳出重置流程
            if v.dayCnt > 0 then
                clean = false
            else
                -- 小于0的 清除掉
                human.db.absAct[id][actId] = nil
            end
        end
    end

    if clean and findActId == nil then
        human.db.absAct[id] = { endTime = config.realEndTime }
    end
end

-- 礼包激活
function absWeeklyCard(human, id, buyConf, isFirst, cnt, nBuyNum)
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

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

    local buyItemConfig = BuyExcel.libao[TopupLogic.TOPUP_LIBAO_ID2]
    if not buyItemConfig then return end

    AbsActLogic.checkAbsActClean(human, id)

    -- 是否已经激活
    local absAct = human.db.absAct[id][config.actId]
    if absAct ~= nil then return end

    -- 激活
    initDB(human, id, config.actId)

    -- 给礼包道具
    BagLogic.addItemList(human, buyItemConfig.reward, "abs_weeklyCard")

    -- 发送一天道具
    updateDaily(human, id, config.actId)

    Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
    getAndSendMsg(human, id, config)
end

-- function GetRemainNum(human, nBuyID)
--     local absAct = human.db.absAct[nABSWEEKLYCARDID]
--     if not absAct then
--         return 0
--     else 
--         return 1
--     end
-- end