--[[
    absAct ={
        [id] = {
            time = os.time()
            day = 1
        }
    }
]]


local AbsActLogic = require("absAct.AbsActLogic")
local AbsActExcel = require("excel.absAct")
local BuyExcel = require("excel.buy")
local Util = require("common.Util")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local ObjHuman = require("core.ObjHuman")
local Lang  = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local MailManager = require("mail.MailManager")
local MailExcel = require("excel.mail")
local YunYingLogic = require("yunying.YunYingLogic")

function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end
    local absAct = human.db.absAct[funcConfig.funcID]
    if absAct and absAct.dayCnt and absAct.dayCnt <= 0 and #absAct.item <= 0 then
        return false
    end
    return true, endTime, startTime
end

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

function fontFestvialSevenDayCard(human,config,net,id,giftId)
    local len = #config.item
    for i = 1,len do
        local jLen = #config.item[i]
        for j = 1,jLen do
            Grid.makeItem(net.dayItem[i].item[j],config.item[i][j][1],config.item[i][j][2])
        end
        net.dayItem[i].item[0] = jLen
    end
    net.dayItem[0] = len
    BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
    net.state = human.db.absAct[id].card and (human.db.absAct[id].card[giftId] and 1 or 0) or 0
end


function getAndSendMsg(human,id)
    -- 判断活动是否开启
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    print("festvialCard",state, endTime, starTime)
    if not state then return end

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

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

    local msgRet = Msg.gc.GC_ABS_FESTVIAL_SEVENDAY_CARD_QUERY
    local config = AbsActExcel.festvialSevenDayCard
    local len = 0
    for infoId,info in pairs(config) do 
        if info.actId == absConfig.actId then
            len = len + 1
            fontFestvialSevenDayCard(human,info,msgRet.netList[len],id,infoId)
        end
    end
    msgRet.netList[0] = len
    Msg.send(msgRet,human.fd)
end

function festvialCard(human,id,conf)
    local giftId = conf.args[1]
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then return end

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

    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end
    absAct.card = absAct.card or {}
    if absAct.card and absAct.card[giftId] then
        return
    end

    absAct.card[giftId] = {}
    absAct.card[giftId].day = 1
    local dayIndex = absAct.card[giftId].day
    local cardConfig = AbsActExcel.festvialSevenDayCard[giftId]

    BagLogic.addItemList(human, cardConfig.zuanshi, "festival_seven_day_card")   

    if cardConfig then
        -- 减去一天
        local mailId = cardConfig.mailId
        local mailConfig = MailExcel.mail[mailId]
        local title = mailConfig.title
        local senderName = mailConfig.senderName
        local content = Util.format(mailConfig.content, 7, dayIndex) 
        MailManager.add(MailManager.SYSTEM, human.db._id, title, content, cardConfig.item[dayIndex], senderName)
    end
    
    getAndSendMsg(human, id)
    
    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end

function updateDaily(human,id)
    local config = AbsActExcel.absActivity[id]
    if not config then return end
    
    AbsActLogic.checkAbsActClean(human, id)
    -- 有无激活记录
    local absAct = human.db.absAct[id]
    if not absAct then return end
    
    if not absAct.card then
        return
    end
    -- 发放奖励
    for giftId, v in pairs(absAct.card) do
        if type(v) == "table" and v.day and v.day < 7 then
            v.day = v.day + 1
            local dayIndex = v.day
            local cardConfig = AbsActExcel.festvialSevenDayCard[giftId]
            if cardConfig then
                -- 减去一天
                local mailId = cardConfig.mailId
                local mailConfig = MailExcel.mail[mailId]
                local title = mailConfig.title
                local senderName = mailConfig.senderName
                local content = Util.format(mailConfig.content, 7, dayIndex) 
                MailManager.add(MailManager.SYSTEM, human.db._id, title, content, cardConfig.item[dayIndex], senderName)
            end
            
            clean = true
            -- 有数据就 跳出重置流程
            if v.day <= 7 then
                clean = false
            else
                -- 小于0的 清除掉
                absAct.card[giftId] = nil
            end
        end
    end

    if clean and not next(absAct.card) then
        human.db.absAct[id] = { endTime = config.realEndTime }
    end
end

function isRed(human, YYInfo, funcConfig)
    local state,endTime,starTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end
    local absConfig = AbsActExcel.absActivity[funcConfig.funcID]
    local absAct = human.db.absAct[funcConfig.funcID]
    if not absAct then return end

    
    if not absAct.card then
        return true
    end

    local config = AbsActExcel.festvialSevenDayCard
    local len = #config 
    for i = 1,len do
        if not absAct.card[i] then
            return true
        end
    end 
    return false
end