


local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Util = require("common.Util")
local Grid = require("bag.Grid")
local Timer = require("core.Timer")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local BRoleLogic = require("billboard.BRoleLogic")
local BillboardDB = require("billboard.BillboardDB")
local BillboardDefine = require("billboard.BillboardDefine")
local RoleLogic = require("role.RoleLogic")
local CommonDB = require("common.CommonDB")
local YunYingLogic = require("yunying.YunYingLogic")
local AbsActExcel = require("excel.absAct")
local MailExcel = require("excel.mail")
local MailManager = require("mail.MailManager")
local MailDefine = require("mail.MailIdDefine")
local RoleDBLogic = require("role.RoleDBLogic")
local AbsActLogic = require("absAct.AbsActLogic")

function getEndTime()
    local now = os.time()
    local endTime = Util.getDayStartTime(now + 24 * 60 * 60)
    return endTime - now
end

function query(human)
    local msgRet = Msg.gc.GC_ABS_ICE_DRAGON_RANK_QUERY
    local board = BillboardDB.getBoard(BillboardDefine.TYPE_DRAGON)
    msgRet.list[0] = 1
    local msgNet =  msgRet.list[1]
    msgNet.funcRankType = 3
    msgNet.leftTime = getEndTime()
    msgNet.list[0] = 10
    
    for i = 1, 10 do            
        local net = msgNet.list[i]
        net.rank = i
        net.rankNeedValue= 0
        if board and board.rank2data[i]  then         
            local rankData = board.rank2data[i]      
           
            net.uid = rankData.roleBase and rankData.roleBase.uuid or rankData.uuid 
            net.rankValue = rankData and rankData.value1 or 0
            
            if rankData and rankData.roleBase then
                net.uid = net.uid or ""
                net.name = rankData.roleBase.name
                net.head =  rankData.roleBase.head
                net.headFrame =  rankData.roleBase.headFrame
                net.lv = rankData.roleBase.lv
            else
                local userDB = RoleDBLogic.getDb(rankData.uuid)
                net.uid = rankData.uuid
                net.name = userDB.name
                net.head = userDB.head
                net.rankValue = rankData.value1
                net.headFrame = userDB.headFrame
                net.lv = userDB.lv
            end
        else
            net.uid = "-1"
            net.name = ""
            net.head = -1
            net.rankValue = 0
            net.headFrame = -1
            net.lv = -1
        end           

        local id = getRewardByRank(i)
        local conf = AbsActExcel.IceRank[id]
        for j, h in ipairs(conf.totalRewards) do
            Grid.makeItem(net.items[j], h[1], h[2])
        end
        net.items[0] = #conf.totalRewards
    end       
       

    -- 获取排行区间 奖励  给予前端处理 放到各个名词后面  显示排名奖励
 --[[
for k, v in ipairs(AbsActExcel.IceRank) do
        local net = msgRet.rankItem[k]
        net.minRank = v.minRank
        net.maxRank = v.maxRank
        net.rankNeedValue = 0
        for j, h in ipairs(v.reward) do
            Grid.makeItem(net.items[j], h[1], h[2])
        end
        net.items[0] = #v.reward
    end
    msgRet.rankItem[0] = #AbsActExcel.IceRank
 ]]   

    local myRank = BillboardDB.getRank(BillboardDefine.TYPE_DRAGON, human.db._id)
    if myRank then
        local rankData = BillboardDB.getRankData(BillboardDefine.TYPE_DRAGON, myRank)
        if rankData then           
            local net =   msgNet.onwerData
            net.rank = myRank 
	        net.rankValue = rankData and rankData.value1 or 0
            net.items[0] = 0
        end 
    else
        local net =   msgNet.onwerData
        net.rank = -1 
        net.rankValue = rankData and rankData.value1 or 0
        net.items[0] = 0
    end
   -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

function queryReward(human)
    local msgRet = Msg.gc.GC_ABS_REACH_RANK_REWARD_QUERY
    msgRet.actId = 0
    msgRet.funcRankType = 3
    for k , conf in ipairs(AbsActExcel.IceRank) do
        local net = msgRet.list[k]
        net.minRank  =  conf.roleMin
        net.maxRank = conf.roleMax
        net.rankNeedValue = 0
        
        for j, h in ipairs(conf.totalRewards) do
            Grid.makeItem(net.items[j], h[1], h[2])
        end
        net.items[0] = #conf.totalRewards
    end
    msgRet.list[0] = #AbsActExcel.IceRank
    Msg.send(msgRet, human.fd)
end

function getRewardByRank(rank)
    for k , conf in ipairs(AbsActExcel.IceRank) do
        if conf.roleMin <= rank and  conf.roleMax >= rank then
            return k  
        end
    end
end

function onZero(funcID)
    local absConfig = AbsActExcel.absActivity[funcID]
    if not absConfig then return end
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
	if not funcConfig then return end

    if not AbsActLogic.isStarted(human, funcID) then
        local diffDay = Util.diffDay(absConfig.realEndTime)
        if diffDay ~= 1 then     
            return
        end        
    end
    local board = BillboardDB.getBoard(BillboardDefine.TYPE_DRAGON)
	if board then 
        local mailConfig = MailExcel.mail[MailDefine.MAIL_ID_HEFU_ICE_RANK]
        local title = mailConfig.title
        local senderName = mailConfig.senderName	

        local sendList = {}
        local len = 0
        for rank = 1, #board.rank2data do
            local rankData = board.rank2data[rank]
            if rankData then
                local uuid
                if rankData and rankData.roleBase then
                    uuid = rankData.roleBase.uuid
                end 

                if not uuid  then
                    uuid = rankData and rankData.uuid or ""
                end
                if uuid then
                    len = len + 1
                    sendList[len] = {}
                    sendList[len].uuid = uuid
                    sendList[len].rank = rank
                end
            end
        end

        if len > 0 then
            Timer.addLater(25, iceDragonRankSend, sendList, 0 )
        end
    end
end



function iceDragonRankSend(sendList, len)
    if len >= #sendList then return end
    local mailConfig = MailExcel.mail[MailDefine.MAIL_ID_HEFU_ICE_RANK]
    local title = mailConfig.title
    local senderName = mailConfig.senderName	
    
    local thisLen = 0 
    for i = len + 1 , #sendList do
        
        local uuid = sendList[i].uuid
        local rank = sendList[i].rank
    
        local id = getRewardByRank(rank)
        local conf = AbsActExcel.IceRank[id]
        local content = Util.format(mailConfig.content, rank)
        MailManager.add(MailManager.SYSTEM, uuid, title, content, conf.totalRewards, senderName)
        thisLen = thisLen + 1
        if thisLen >= 10 then
            break
        end
    end
  

    local nextLen = len + thisLen
    if nextLen < #sendList and #sendList > 0 then
        Timer.addLater(2, iceDragonRankSend, sendList, nextLen)      
    end  
        
end









