--[[
    absAct[id] = {
        cup[1] = 1
        chose = {1,2,3}
        state = 0
        huoyue = 0
        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 getAndSendMsg(human,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

    local nowDay = Util.diffDay(starTime) + 1

    absAct.cup = absAct.cup or {1,1,1,1}
    absAct.chose = absAct.chose or {}
    absAct.state = absAct.state or 0
    local config = AbsActExcel.wishCup
    local msgRet = Msg.gc.GC_ABS_HF_WISH_CUP_QUERY
    local len = #config 
    local count = 0
    for i = 1,len do
        v = config[i]
        if v.actId == absConfig.actId and nowDay == v.day then
            count = count + 1
            local cupId = i%6
            cupId = cupId == 0 and 6 or cupId
            msgRet.cupList[count].id = cupId
            msgRet.cupList[count].desc = v.desc
            if v.lock == 0 then
                msgRet.cupList[count].state = 0
                if absAct.chose[cupId] then
                    msgRet.cupList[count].state = 1
                end
            else
                msgRet.cupList[count].state = 2
                if absAct.cup[cupId] then
                    msgRet.cupList[count].state = 0
                end
                if absAct.chose[cupId] then
                    msgRet.cupList[count].state = 1
                end
            end
            local rewardLen = #v.item
            for j = 1,rewardLen do
                local itemID = v.item[j][1]
                local itemCnt = v.item[j][2]
                Grid.makeItem(msgRet.cupList[count].item[j],itemID,itemCnt)
            end
            msgRet.cupList[count].item[0] = rewardLen
        end
    end
    msgRet.cupList[0] = count
    msgRet.state = absAct.state
    Msg.send(msgRet,human.fd)
end

-- 选择圣杯
function selectCup(human,id,one,two,three)
    -- 判断活动是否开启
    local msgRet = Msg.gc.GC_ABS_HF_WISH_CUP_SELECT
    msgRet.ret = 0
    local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    if not state then 
        Msg.send(msgRet,human.fd)
        return 
    end

    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then 
        Msg.send(msgRet,human.fd)
        return 
    end


    local absAct = human.db.absAct[id]
    if not absAct then
        Msg.send(msgRet,human.fd)
        return
    end
    local nowDay = Util.diffDay(starTime) + 1
    absAct.cup = absAct.cup or {1,1,1,1}
    absAct.chose = absAct.chose or {}
    absAct.state = absAct.state or 0
    absAct.day = nowDay
    absAct.actId = absConfig.actId

    -- 已许愿，不可更改
    if absAct.state ~= 0 then
        Msg.send(msgRet,human.fd)
        return
    end

    -- 杯子未解锁
    if not absAct.cup[one] or not absAct.cup[two] or not absAct.cup[three] then
        Msg.send(msgRet,human.fd)
        return
    end
       
    -- 保存选择
    absAct.chose[one] = 1
    absAct.chose[two] = 1
    absAct.chose[three] = 1
    
    -- 设置状态
    absAct.state = 1

    msgRet.ret = 1
    Msg.send(msgRet,human.fd)
    getAndSendMsg(human,id)
    
    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end


-- 每日任务活跃度
function onDailyTask(human,funcID, parameter)
    AbsActLogic.checkAbsActClean(human, funcID)
    local absAct = human.db.absAct[funcID]
    if not absAct then
        return
    end
    absAct.huoyue = absAct.huoyue or 0
    absAct.huoyue = absAct.huoyue + parameter
    if absAct.huoyue >= 100 then
        activeCup(human,funcID,5)
    end
end

-- 充值
function onCharge(human, parameter,funcID)
    AbsActLogic.checkAbsActClean(human, funcID)
    local absAct = human.db.absAct[funcID]
    if not absAct then
        return
    end
    activeCup(human,funcID,6)
end

-- 完成任务回调
function activeCup(human,id,cupId)
    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end
    absAct.cup = absAct.cup or {1,1,1,1}
    absAct.chose = absAct.chose or {}
    absAct.state = absAct.state or 0

    absAct.cup[cupId] = 1
end

-- 每日发放礼包
function updateDaily(human,id)
    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then
        return
    end
    
    absAct.huoyue = 0
    absAct.cup = absAct.cup or {1,1,1,1}
    absAct.chose = absAct.chose or {}
    absAct.state = absAct.state or 0
    local config = AbsActExcel.wishCup
    local len = #config
    local item = {}

    absAct.cup = {1,1,1,1}
    if absAct.state ~= 1 then 
        absAct.chose = {}
        return
    end

    for i = 1,len do
        if config[i].actId == absAct.actId and absAct.day == config[i].day then
            local cupId = i%6
            cupId = cupId == 0 and 6 or cupId
            if absAct.chose[cupId] then
                for j = 1,#config[i].item do
                    item[#item + 1] = {config[i].item[j][1],config[i].item[j][2]}
                end
            end
        end
    end
    
    absAct.chose = {}
    absAct.state = 0
    absAct.day = nil

    local mailConfig = MailExcel.mail[2007]
    local title = mailConfig.title
    local senderName = mailConfig.senderName
    local content = mailConfig.content
    MailManager.add(MailManager.SYSTEM, human.db._id, title, content, item, senderName)

    
    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)
    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
    
    absAct.cup = absAct.cup or {1,1,1,1}
    absAct.chose = absAct.chose or {}
    absAct.state = absAct.state or 0

    if absAct.state ~= 1 then
        return true
    end

    return false
end