local Config = require("Config")
local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local AbsActDefine = require("absAct.AbsActDefine")
local PremiumGiftLogic = require("absAct.PremiumGiftLogic")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local MangHeLogic = require("absAct.MangHeLogic")
local MailExcel = require("excel.mail")
local MailManager = require("mail.MailManager")
local HeroUpLogic = require("absAct.HeroUpLogic")
local CommonDB = require("common.CommonDB")
local DropExchangeLogic = require("absAct.DropExchangeLogic")
local PerpleHeroLogic = require("absAct.PerpleHeroLogic")
local HeroLogLogic = require("absAct.HeroLogLogic")
local AbsJifenDrawLogic = require("absAct.AbsJifenDrawLogic")
local HeroLoginPickLogic = require("absAct.HeroLoginPickLogic")
local AbsZhanbuLunpanLogic = require("absAct.AbsZhanbuLunpanLogic")
local DrawCardLogic = require("drawCard.DrawCardLogic")
local LimitPerpleHeroLogic = require("present.LimitPerpleHeroLogic")
local RechargeBackLogic = require("absAct.RechargeBackLogic")
local Abs365CardLogic = require("absAct.Abs365CardLogic")
local AbsWeeklyCardLogic = require("absAct.AbsWeeklyCardLogic")
local AbsSurpriseChest = require("absAct.AbsSurpriseChest")
local SpecialCustomLogic = require("absAct.SpecialCustomLogic")
local SevenDayLogic = require("absAct.SevenDayLogic")
local DrumBlastingLogic = require("absAct.DrumBlastingLogic")
local HeroComeLogic = require("absAct.HeroComeLogic")
local WishGiftLogic = require("absAct.WishGiftLogic")
local EveryDayPrayLogic = require("absAct.EveryDayPrayLogic")
local ExclusiveTaskLogic = require("absAct.ExclusiveTaskLogic")
local AbsActBillboardMiddleLogic = require("absAct.AbsActBillboardMiddleLogic")
local AbsActBillboardLogic = require("absAct.AbsActBillboardLogic")
local SingleReachLogic = require("absAct.SingleReachLogic")
local TotalReachLogic = require("absAct.TotalReachLogic")
local UseDiamondLogic = require("absAct.UseDiamondLogic")
local WishCupLogic = require("absAct.WishCupLogic")
local SeckillGiftLogic = require("absAct.SeckillGiftLogic")
local AbsOpenServerTotalConsume = require("absAct.AbsOpenServerTotalConsume")
local AbsReachRankLogic = require("absAct.AbsReachRankLogic")
local AbsReachRankDB = require("absAct.AbsReachRankDB")
local FestivalSevenDayCardLogic = require("absAct.FestivalSevenDayCardLogic")
local DailyFixedTaskLogic = require("absAct.DailyFixedTaskLogic")
local AbsSignGiftLogic = require("absAct.AbsSignGiftLogic")

ABS_ACT_TYPE_LIST = nil
function initAfterHot()
    ABS_ACT_TYPE_LIST = {}
    local now = os.time()
    
    local len = 0
    for k, config in pairs(AbsActExcel.absActivity) do
        config.realStartTime = config.realStartTime or 0
        config.realEndTime = config.realEndTime or 0

        ABS_ACT_TYPE_LIST[config.type] = ABS_ACT_TYPE_LIST[config.type] or {}
        ABS_ACT_TYPE_LIST[config.type][#ABS_ACT_TYPE_LIST[config.type] + 1] = k
    end
end

function onLogin(human)
    local sendLisg = {}
    for k, config in pairs(AbsActExcel.absLogin) do
        local state,id = isStartedByType(human,  config.absType)
        if state then 
            checkAbsActClean(human, id)
            if config.type == AbsActDefine.ABS_LOGIN_REWARD_TYPE_1 and not human.db.absAct[id].openGet then
                human.db.absAct[id].openGet = 1
                sendLisg[#sendLisg + 1] = k
            elseif config.type == AbsActDefine.ABS_LOGIN_REWARD_TYPE_2 then
                local lastTime = human.db.absAct[id].loginDayGetTime or 0
                if not Util.isSameDay(lastTime) then
                    human.db.absAct[id].loginDayGetTime = os.time()
                    sendLisg[#sendLisg + 1] = k
                end
            end
        end
    end

    if #sendLisg > 0 then
        for _, id in ipairs(sendLisg) do
            local config = AbsActExcel.absLogin[id]
            if config.mailID > 0 then
               local mailConfig = MailExcel.mail[config.mailID]
               local title = mailConfig.title
               local senderName = mailConfig.senderName
               local content = mailConfig.content
               MailManager.add(MailManager.SYSTEM, human.db._id, title, content, config.item, senderName)	
            end
        end
    end
end

-- 判断活动是否开启
function isStarted(human, id)
	local config = AbsActExcel.absActivity[id]
	if not config then return end
   
	local now = os.time()
    if now < config.realStartTime or now >= config.realEndTime then
		return
    end
    return true, config.realEndTime, config.realStartTime
end

-- 判断活动是否开启
function isStartedByType(human, type)
    if not ABS_ACT_TYPE_LIST[type] then return end

    for k, v in ipairs(ABS_ACT_TYPE_LIST[type]) do
        local config = AbsActExcel.absActivity[v]
        if config and isStarted(human, v) == true then
            return true, v, config.realEndTime, config.realStartTime
        end
    end
end

-- 根据actID 获取 Id
function getIDByActID(human, actID)
    for id, v in pairs(AbsActExcel.absActivity) do
        if v.actId == actID and isStarted(human, id) then
            return id
        end
    end
end

function checkAbsActClean(human, id)
    local config = AbsActExcel.absActivity[id]	
    if human.db.absAct[id] and human.db.absAct[id].endTime == config.realEndTime then
    else
       -- 两个卡不清 领完才能清
       if config.type == AbsActDefine.ABS_ACT_TYPE_12 then     -- 铂金周卡
          if not human.db.absAct[id] then
              human.db.absAct[id] = { endTime = config.realEndTime }
          end

       elseif config.type == AbsActDefine.ABS_ACT_TYPE_13 then -- 365王卡
           if not human.db.absAct[id] then
              human.db.absAct[id] = { endTime = config.realEndTime }
           end

       else
	       human.db.absAct[id] = { endTime = config.realEndTime }
       end
    end
end

-- 活动详情查询
function actDetailQuery(human, id)
    -- 判断传入ID是否合法
    local config = AbsActExcel.absActivity[id]
    if config == nil then
        return
    end

    -- 判断活动是否开启
    local startFlag = isStarted(human, id)
    if startFlag == nil then
        return Broadcast.sendErr(human, Lang.ACT_NOT_START)
    end

    checkAbsActClean(human, id)

    if config.type == AbsActDefine.ABS_ACT_TYPE_1 then-- 超值礼包
        PremiumGiftLogic.getAndSendMsg(human, id, config.actId)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_2 then-- 盲盒
        MangHeLogic.query(human, id, config.actId)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_6 then
        DropExchangeLogic.query(human, id, true)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_4 then
        PerpleHeroLogic.getAndSendMsg(human,id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_5 then
        HeroLogLogic.getAndSendMsg(human,id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_7 then
        AbsJifenDrawLogic.getAndSendMsg(human, id, config)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_8 then
        HeroLoginPickLogic.query(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_9 then
        AbsZhanbuLunpanLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_10 then
        RechargeBackLogic.query(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_12 then
        AbsWeeklyCardLogic.getAndSendMsg(human, id, config)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_13 then
        Abs365CardLogic.getAndSendMsg(human, id, config)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_14 then
        AbsSurpriseChest.getAndSendMsg(human, id, config)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_18 then
        SevenDayLogic.getAndSendMsg(human, id, config.actID)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_16 then
        DrumBlastingLogic.getAndSendMsg(human, id, config.actID)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_17 then
        SpecialCustomLogic.getAndSendMsg(human, id, config.actID)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_19 then
        HeroComeLogic.getAndSendMsg(human, id, config.actId)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_20 then
        WishGiftLogic.getAndSendMsg(human, id, config.actId)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_21 then
        EveryDayPrayLogic.getAndSendMsg(human, id, config.actId)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_22 then
        ExclusiveTaskLogic.getAndSendMsg(human, id, config.actId)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_26 then
        AbsReachRankLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_46 then
        AbsOpenServerTotalConsume.getAndSendMsg(human, id, config.actId)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_72 then
        SingleReachLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_71 then
        TotalReachLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_73 then
        AbsActBillboardLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_23 then
        SeckillGiftLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_24 then
        UseDiamondLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_25 then
        WishCupLogic.getAndSendMsg(human, id)
	elseif config.type == AbsActDefine.ABS_ACT_TYPE_27 then
        DailyFixedTaskLogic.getAndSendMsg(human, id)
	elseif config.type == AbsActDefine.ABS_ACT_TYPE_28 then
        FestivalSevenDayCardLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_30 then
        AbsSignGiftLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_31 then
        DailyFixedTaskLogic.getAndSendMsg(human, id)
    elseif config.type == AbsActDefine.ABS_ACT_TYPE_33 then
        FestivalSevenDayCardLogic.getAndSendMsg(human, id)
    end
end

-- 活动抽卡
function getLuckDraw(human,luckDraw,randomIndex,isAct,op)
    if isAct == AbsActDefine.OA_ACT_TYPE_501 then
        return LimitPerpleHeroLogic.getLuckDraw(human, op)
    end

    if not randomIndex then
        return 
    end

    if isAct == AbsActDefine.ABS_ACT_TYPE_3 then
        -- 判断活动是否存在
        local state,actID = isStartedByType(human, AbsActDefine.ABS_ACT_TYPE_3)
        -- 根据单抽十连抽判断是否中签
        if state then
            checkAbsActClean(human, actID)
            local actConfig = HeroUpLogic.getConfigByActID(actID)
            local randomNum = math.random(1,10000)
            if op == DrawCardLogic.DRAWCARD_OP_1 then
                if randomNum <= actConfig.singlePer then
                    luckDraw = true
                end
            elseif op == DrawCardLogic.DRAWCARD_OP_2 then
                if randomNum <= actConfig.tenPer and randomIndex then
                    luckDraw = true
                end
            end
        end

        if luckDraw then
            local actConfig = HeroUpLogic.getConfigByActID(actID)
            return actConfig.heroId
        end

    elseif isAct == AbsActDefine.ABS_ACT_TYPE_4 then
        -- 判断活动是否存在
        local state,actID = isStartedByType(human, AbsActDefine.ABS_ACT_TYPE_4)

        if state then
            checkAbsActClean(human, actID)
           
            local actConfig = PerpleHeroLogic.getConfigByActID(actID)
            local randomNum = math.random(1,10000)
            if randomNum <= actConfig.rate then
                luckDraw = true
            end
            local noTargetHeroCnt = human.db.absAct[actID].noTargetHeroCnt or 0
            if op == DrawCardLogic.DRAWCARD_OP_2 then
                noTargetHeroCnt = noTargetHeroCnt + 10
            else
                noTargetHeroCnt = noTargetHeroCnt + 1
            end

            human.db.absAct[actID].noTargetHeroCnt = noTargetHeroCnt

            if not luckDraw and noTargetHeroCnt >= actConfig.cnt then
                luckDraw = true
            end
           
            if luckDraw then 
                if human.db.absAct[actID] then
                    human.db.absAct[actID].noTargetHeroCnt = 0
                end
                return human.db.absAct[actID].perpleHero 
            end           
        end
    elseif isAct == AbsActDefine.ABS_ACT_TYPE_19 then
        -- 判断活动是否存在
        local state,actID = isStartedByType(human, AbsActDefine.ABS_ACT_TYPE_19)

        if state then
            checkAbsActClean(human, actID)
           
            local actConfig = HeroComeLogic.getConfigByActID(actID)
            local randomNum = math.random(1,10000)
            if randomNum <= actConfig.rate then
                luckDraw = true
            end
            local noTargetHeroCnt = human.db.absAct[actID].noTargetHeroCnt or 0
            if op == DrawCardLogic.DRAWCARD_OP_2 then
                noTargetHeroCnt = noTargetHeroCnt + 10
            else
                noTargetHeroCnt = noTargetHeroCnt + 1
            end

            human.db.absAct[actID].noTargetHeroCnt = noTargetHeroCnt

            if not luckDraw and noTargetHeroCnt >= actConfig.cnt then
                luckDraw = true
            end
           
            if luckDraw then 
                if human.db.absAct[actID] then
                    human.db.absAct[actID].noTargetHeroCnt = 0
                end
                return human.db.absAct[actID].perpleHero 
            end           
        end
    end
end

function onHour(hour)
    if hour == 23 then
        AbsActBillboardLogic.sendBillboardReward()
    end
    if hour == 1 then
        AbsActBillboardLogic.cleanBillBoardDB()
    end
end

function updateBillBoard(humanDb)
   if not humanDb.absAct then return end
   
   local now = os.time()
   
   for k, data in pairs(humanDb.absAct) do
      local config = AbsActExcel.absActivity[k]
      
      if config and config.type == AbsActDefine.ABS_ACT_TYPE_26 then
          AbsReachRankDB.loadBoardData(data, k, humanDb._id, now)
      end	
   end
end


function updateBillBoardRank()
   AbsReachRankDB.loadBoardRank()
end