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

local SEVENDAYCARD_STAUS_NOBUY  =   0       -- 未购买
local SEVENDAYCARD_STAUS_BUY    =   1       -- 已购买
local SEVENDAYCARD_STAUS_FINISH =   2       -- 已购买奖励发送完成

local SEVENDAYCARD_TOTALDAY = 7             -- 持续时间
local SEVENDAYCARD_GIFTGET = 1              -- 礼包已领取

local SEVENDAYCARD_ID = 742                 -- 活动ID

local function SEVENDAYCARD_WRITELOG(human, szText)
    Log.write(Log.LOGID_OSS_COMMON, "[限时周卡]".." id = "..human.db._id.." name = "..human.db.name..szText)
end

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 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] and (human.db.absAct[id].nStatus == SEVENDAYCARD_STAUS_BUY and 1 or 0) or 0
end


-- 发放邮件奖励
local function SevenDayCard_SendMail(human, cardConfig, dayIndex)
    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)
    print("[SevenDayCard_SendMail] 发送周卡邮件奖励 name = "..human.db.name.." dayIndex = "..dayIndex)
end

function getAndSendMsg(human,id)
    -- 判断活动是否开启
    -- local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    local state, endTime, starTime = CycleActivityLogic.isStarted(human, id)
    print("getAndSendMsg ",state, endTime, starTime, 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 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 szLogText = " [festvialCard] "
    local giftId = conf.args[1]
    -- local state, endTime, starTime = AbsActLogic.isStarted(human, id)
    local state, endTime, starTime = CycleActivityLogic.isStarted(human, id)
    if not state then
        SEVENDAYCARD_WRITELOG(human,szLogText.."活动未开启")
        return 
    end

    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then 
        SEVENDAYCARD_WRITELOG(human,szLogText.."不存在对应配置 id = "..id)
        return
    end

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

    -- 状态设置为已购买
    absAct.nStatus = SEVENDAYCARD_STAUS_BUY
    -- 记录购买时间
    absAct.nBuyTime = os.time()

    local nDay = 1
    absAct.giftId = giftId
    absAct.day = Util.setBit(0, nDay)

    -- absAct.card = absAct.card or {}
    -- if absAct.card and absAct.card[giftId] then
    --     SEVENDAYCARD_WRITELOG(human,szLogText.."存在旧的数据 id = "..id.." giftId = "..giftId)
    --     return
    -- end

    -- absAct.card[giftId] = {}
    -- absAct.card[giftId].day = Util.setBit(0, nDay)

    local cardConfig = AbsActExcel.festvialSevenDayCard[giftId]

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

    if cardConfig then
        -- 减去一天
        SevenDayCard_SendMail(human, cardConfig, nDay)
    end

    print("[festvialCard] id = "..id.." giftId = "..giftId.." day = "..absAct.day)

    SEVENDAYCARD_WRITELOG(human,szLogText.."玩家购买了限时周卡, 发送了第一天奖励 id = "..id.." giftId = "..giftId.." day = "..absAct.day)
    
    getAndSendMsg(human, id)
    
    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, absConfig.panelID)
end

function updateDaily(human,id)
    local szLogText = " [updateDaily] "
    local config = AbsActExcel.absActivity[id]
    if not config then
        SEVENDAYCARD_WRITELOG(human, szLogText.." 不存在对应配置 id = "..id)
        return
    end

    -- AbsActLogic.checkAbsActClean(human, id)
     -- 有无激活记录
    local absAct = human.db.absAct[id]
    if not absAct then
        SEVENDAYCARD_WRITELOG(human, szLogText.." 不存在对应数据 id = "..id)
        return
    end

    if not absAct.nStatus or SEVENDAYCARD_STAUS_BUY ~= absAct.nStatus then
        return
    end

    -- 发放奖励
    local bClear = false
    local nBuyTime = absAct.nBuyTime
    local giftId = absAct.giftId
    
    local nDiffDay = -1
    nDiffDay = Util.diffDay(nBuyTime)
    if nDiffDay ~= -1 then
        if nDiffDay >= SEVENDAYCARD_TOTALDAY then
            bClear = true
        end
    end
    
    table.print_lua_table(absAct)
    local bRet = true == bClear and 1 or 0
    print("[updateDaily] nDiffDay = "..nDiffDay.." bRet = \n"..bRet)

    if false == bClear then
        local nNowDay = nDiffDay + 1
        local nFlag = Util.getBit(absAct.day, nNowDay)
        print("[updateDaily] nDiffDay = "..nDiffDay.." nNowDay = "..nNowDay.." day = "..absAct.day.." nFlag = ".. nFlag)
        if nFlag == 0 then
            absAct.day = Util.setBit(absAct.day, nNowDay)

            local dayIndex = nNowDay
            local cardConfig = AbsActExcel.festvialSevenDayCard[giftId]
            if cardConfig then
                -- 减去一天
                SevenDayCard_SendMail(human, cardConfig, dayIndex)
            end

            SEVENDAYCARD_WRITELOG(human, szLogText.." 正常发放了限时周卡奖励 id = "..id.." nNowDay = "..nNowDay)
            if nNowDay == SEVENDAYCARD_TOTALDAY then
                absAct.nStatus = SEVENDAYCARD_STAUS_FINISH
            end
        else
            print("[updateDaily] 当天重复登录 = "..nDiffDay.." day = "..absAct.day.." nNowDay = "..nNowDay.." nFlag = "..nFlag)
        end
    else
        print("[updateDaily] 已过期 nDiffDay = "..nDiffDay.." absAct.day = "..absAct.day)
        -- 检测一遍，如果还有就直接发
        for i = 1, SEVENDAYCARD_TOTALDAY, 1 do
            local nFlag = Util.getBit(absAct.day, i)
            print("[updateDaily] 已过期 nDiffDay = "..nDiffDay.." absAct.day = "..absAct.day.." i = "..i .." nFlag = "..nFlag)
            if 0 == nFlag then
                absAct.day = Util.setBit(absAct.day, i)

                local dayIndex = i
                local cardConfig = AbsActExcel.festvialSevenDayCard[giftId]
                if cardConfig then
                    -- 减去一天
                    SevenDayCard_SendMail(human, cardConfig, dayIndex)
                else
                    print("[updateDaily] 过期但是不存在对应的配置")
                end

                SEVENDAYCARD_WRITELOG(human, szLogText.." 判断玩家已经过了7天但是有奖励没有领取 发放了限时周卡奖励 id = "..id.." nNowDay = "..i)
            end
        end
        absAct.nStatus = SEVENDAYCARD_STAUS_FINISH
    end
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

    return false
end

function SEVENDAYCARD_GMCLEAR(human, id)
    human.db.absAct[id] = {}
    human.db.absAct[id].card = nil
    print("[SEVENDAYCARD_GMCLEAR] 重置成功 id = "..id)
end

function onLogin(human, funcID)
    local tAbsAct = human.db.absAct[SEVENDAYCARD_ID]
    if tAbsAct and SEVENDAYCARD_STAUS_BUY == tAbsAct.nStatus then
        local nDiffDay = Util.diffDay(tAbsAct.nBuyTime)
        local nCheckDay = nDiffDay + 1 >= SEVENDAYCARD_TOTALDAY and SEVENDAYCARD_TOTALDAY or nDiffDay + 1
        local giftId = tAbsAct.giftId

        for i = 1, nCheckDay, 1 do
            local nFlag = Util.getBit(tAbsAct.day, i)

            if 0 == nFlag then
                tAbsAct.day = Util.setBit(tAbsAct.day, i)
                local dayIndex = i
                local cardConfig = AbsActExcel.festvialSevenDayCard[giftId]
                if cardConfig then
                    -- 减去一天
                    SevenDayCard_SendMail(human, cardConfig, dayIndex)
                end

                SEVENDAYCARD_WRITELOG(human, "[onLogin] 玩家登录检测 ".." 发放了限时周卡奖励 nNowDay = "..i)

                if SEVENDAYCARD_TOTALDAY == i then
                    tAbsAct.nStatus = SEVENDAYCARD_STAUS_FINISH
                    break
                end              
            end
        end
    end
end

-- function GetRemainNum(human, nBuyID)
--     local tConfig = AbsActExcel.festvialSevenDayCard
--     local absAct = human.db.absAct[SEVENDAYCARD_ID]
--     if not absAct then
--         return 0
--     end

--     return SEVENDAYCARD_STAUS_NOBUY == absAct.nStatus and 1 or 0
-- end