--[[
    absact = {
        task[id] = {
            state = 1
            cnt = 0
        }
    }
]]


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

function isOpen(human, YYInfo, funcConfig)
    -- local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    local state, endTime, startTime = CycleActivityLogic.isStarted(human, funcConfig.funcID)
    if not state then return end
 --[[
    local absAct = human.db.absAct[funcConfig.funcID]
    if not absAct then
        return 
    end
 ]]   
    return true, endTime, startTime
end

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

function fontDailyFixedTask(db,config,net,id)
    net.state = db.task and (db.task[id] and db.task[id].state or 0) or 0
    net.nowCnt = db.task and (db.task[id] and db.task[id].cnt or 0) or 0
    net.needCnt = config.needCnt
    net.desc = config.desc
    net.day = config.day
    net.giftId = id
    local len = #config.item
    for i = 1,len do
        Grid.makeItem(net.item[i],config.item[i][1],config.item[i][2])
    end
    net.item[0] = len
end


function getAndSendMsg(human,id)
    -- 判断活动是否开启
    -- local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    local state, endTime, starTime = CycleActivityLogic.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
    local config = AbsActExcel.dailyFixedTask
    local msgRet = Msg.gc.GC_ABS_DAILY_FIXED_TASK_QUERY
    local len = #config
    local count = 0
    for i = 1,len do
        if config[i].day == nowDay and config[i].actId == absConfig.actId then
            count = count + 1
            local net = msgRet.netList[count]
            fontDailyFixedTask(absAct,config[i],net,i)
        end
    end
    msgRet.netList[0] = count
    Msg.send(msgRet,human.fd)
end

function getItem(human,id,giftId)
    -- 判断活动是否开启
    -- local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    local state, endTime, starTime = CycleActivityLogic.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
    local config = AbsActExcel.dailyFixedTask[giftId]
    if not config or config.day ~= nowDay or config.actId ~= absConfig.actId then
        return
    end

    if absAct.task and absAct.task[giftId] and absAct.task[giftId].state == 2 then
        return
    end
    absAct.task = absAct.task or {}
    absAct.task[giftId] = absAct.task[giftId] or {}
    absAct.task[giftId].state = 2
    BagLogic.addItemList(human, config.item, "dailyFixedTask")   
    getAndSendMsg(human,id)

    
    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end


function onLogin(human,funcID)
    taskActive(human, 1, funcID,1)

    --特殊处理下活动没开前充值、完成了活跃度任务，任务开启后没玩法完成
    local nowHuoYue =  human.db.dailyTask.nowHuoYue or 0
    if nowHuoYue > 0 then
        specailFunc(human, 2, funcID, nowHuoYue)
    end

    local topupAcountDaily = human.db.topupAcountDaily
    if topupAcountDaily and topupAcountDaily > 0 then
        specailFunc(human, 3, funcID, topupAcountDaily)
    end
end

function onCharge(human,parameter,funcID)
    taskActive(human, 3, funcID,parameter)
end

function onDailyTask(human,funcID, parameter)
    taskActive(human, 2, funcID,parameter)
end

function onZhanBuBuyCard(human, funcID, parameter)
    taskActive(human, 4, funcID, parameter)
end

--特殊处理下活动没开前充值、完成了活跃度任务，任务开启后没玩法完成
function specailFunc(human, type, id, cnt)
    -- local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    local state, endTime, starTime = CycleActivityLogic.isStarted(human, id)
    if not state then return end

    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end
    
    -- AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end
    absAct.task = absAct.task or {}
    local nowDay = Util.diffDay(starTime) + 1
    local config = AbsActExcel.dailyFixedTask

    local len = #config

    for i = 1,len do
        if config[i].day == nowDay and config[i].actId == absConfig.actId and type == config[i].type then
            absAct.task[i] = absAct.task[i] or {} 
            absAct.task[i].cnt = absAct.task[i].cnt or 0
            if absAct.task[i].state and absAct.task[i].cnt > 0 then --只处理一次
                break
            end
            absAct.task[i].cnt = cnt
            --absAct.task[i].cnt = absAct.task[i].cnt + cnt
            if absAct.task[i].cnt >= config[i].needCnt then
                absAct.task[i].state = 1
                break
            end
        end
    end
    
    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end


-- 每日任务激活
function taskActive(human, type, id, cnt)
    -- local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    local state, endTime, starTime = CycleActivityLogic.isStarted(human, id)
    if not state then return end

    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end
    
    -- AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end
    absAct.task = absAct.task or {}
    local nowDay = Util.diffDay(starTime) + 1
    local config = AbsActExcel.dailyFixedTask

    local len = #config

    for i = 1,len do
        if config[i].day == nowDay and config[i].actId == absConfig.actId and type == config[i].type then
            absAct.task[i] = absAct.task[i] or {} 
            absAct.task[i].cnt = absAct.task[i].cnt or 0
            if absAct.task[i].state then
                break
            end
            absAct.task[i].cnt = absAct.task[i].cnt + cnt
            if absAct.task[i].cnt >= config[i].needCnt then
                absAct.task[i].state = 1
                break
            end
        end
    end
    
    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end

function isRed(human, YYInfo, funcConfig)
    -- local state,endTime,starTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    local state,endTime,starTime = CycleActivityLogic.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.task then
        return
    end
    local config = AbsActExcel.dailyFixedTask
    local nowDay = Util.diffDay(starTime) + 1
    for k,v in pairs(absAct.task) do
        if v.state == 1 and config[k].actId == absConfig.actId and config[k].day == nowDay then
            return true
        end
    end

    return false
end