--[[
    absAct[id] = {
        
    
    
    }
]]

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 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 getAndSendMsg(human,id,actID)
    -- 判断活动是否开起
    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
    -- 构造数据
    local nowDay = Util.diffDay(starTime) + 1
    absAct.get = absAct.get or {}
    config = AbsActExcel.sevenDay
    local len = #config
    local count = 0 
    local msgRet = Msg.gc.GC_ABS_ND_SEVEN_DAY_QUERY
    msgRet.day = nowDay
    for i = 1,len do
        local v = config[i]
        if v.actId == absConfig.actId then
            count = count + 1
            local net = msgRet.sevenDayList[count]
            net.day = v.day
            local itemLen = #v.item
            for j = 1,itemLen do
                Grid.makeItem(net.item[j],v.item[j][1],v.item[j][2])
            end
            net.item[0] = itemLen
            net.state = absAct.get[v.day] and absAct.get[v.day] or 0
            if nowDay >= v.day and net.state == 0 then
                net.state = 1
            end
        end
    end
    msgRet.sevenDayList[0] = count    
    Msg.send(msgRet,human.fd)
end

-- 获取对应天数奖励
function getItem(human,day,id)
    -- 判断活动是否开启
    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.get = absAct.get or {}
    local nowDay = Util.diffDay(starTime) + 1
    local config = AbsActExcel.sevenDay
    -- 指定天已被领取
    if absAct.get[day] == 2 then
        return
    end

    -- 指定天未到
    if day > nowDay then
        return
    end

    -- 改变状态
    absAct.get[day] = 2
    
    -- 发放道具
    local len = #config
    for i = 1,len do
        local v = config[i]
        if v.actId == absConfig.actId and v.day == day then
            BagLogic.addItemList(human, v.item, "abs_seven_day")
        end
    end
    getAndSendMsg(human,id,absConfig.actID)
    
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendBanner(human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
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 true end

    local nowDay = Util.diffDay(starTime) + 1
    absAct.get = absAct.get or {}
    for i = 1,nowDay do
        local v = absAct.get[i]
        if v == 1 or v == nil then
            return true
        end
    end

    return false
end

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