-- 月度活动   转盘高抽 猛将如虎 招贤纳士

-- human.db.actMonthTask.summonCnt 本轮完成次数
-- human.db.actMonthTask.sumWinScoCnt 总完成轮数
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local MailExcel = require("excel.mail")
local MonthactExcel = require("excel.monthAct")
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")
local BagLogic = require("bag.BagLogic")
local MailManager = require("mail.MailManager")
local MonthActLogic = require("monthAct.MonthActLogic")
local YunYingLogic = require("yunying.YunYingLogic")

local function getRoundCnt(human)
    if not human.db.actMonthTask then
        return 0
    end
    return human.db.actMonthTask.sumWinScoCnt or 0
end

local function setRoundCnt(human, value)
    human.db.actMonthTask = human.db.actMonthTask or {}
    human.db.actMonthTask.sumWinScoCnt = value
end

local function getSummonCnt(human)
    if not human.db.actMonthTask then
        return 0
    end
    return human.db.actMonthTask.summonCnt or 0
end

local function setSummonCnt(human, value)
    human.db.actMonthTask = human.db.actMonthTask or {}
    human.db.actMonthTask.summonCnt = value   
end

local MAX_SUMMON_CNT = nil
local function getMaxSummonCnt()
    if not MAX_SUMMON_CNT then
        MAX_SUMMON_CNT = 0
        for _, cf in ipairs(MonthactExcel.sumWinScore) do     
            MAX_SUMMON_CNT = math.max(MAX_SUMMON_CNT, cf.needCnt)
        end  
    end
    return MAX_SUMMON_CNT
end

-- 查询召唤礼包信息
function query(human)
    local msgRet = Msg.gc.GC_MONTHACT_SUMMON_WIN_SCORE_QUERY
    local roundCnt = getRoundCnt(human)
    local maxRoundCnt = MonthactExcel.sumWinScore[1].reCnt   
    msgRet.curCnt = roundCnt
    msgRet.cnt = maxRoundCnt
    msgRet.leftTime = MonthActLogic.getActLeftTime(human)   
    msgRet.sunWinScoreMsg[0] = #MonthactExcel.sumWinScore
    for id, cf in ipairs(MonthactExcel.sumWinScore) do
        local net = msgRet.sunWinScoreMsg[id]
        net.id = id         
        if roundCnt >= maxRoundCnt then
            net.cur = cf.needCnt
        else
            net.cur = math.min(getSummonCnt(human), cf.needCnt)
        end
        net.max = cf.needCnt
        net.reward[0] = #cf.reward
        net.desc = ""
        for i = 1, net.reward[0] do
            Grid.makeItem(net.reward[i], cf.reward[i][1], cf.reward[i][2])
        end         
    end
    --Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

-- 发送礼包物品
function onSummon(human, cnt)
    -- 检查活动是否结束
    MonthActLogic.checkActEndTime(human)

    -- 满6轮了
    local roundCnt = getRoundCnt(human)
    local maxRoundCnt = MonthactExcel.sumWinScore[1].reCnt
    if roundCnt >= maxRoundCnt then
        return
    end
    
    local oldCnt = getSummonCnt(human)
    local newCnt = oldCnt + cnt
    local maxSummonCnt = getMaxSummonCnt()
    local summonCnt = newCnt % maxSummonCnt

    -- 判断是否达到兑换次数上限
    for round = roundCnt + 1, maxRoundCnt do
        for id, cf in ipairs(MonthactExcel.sumWinScore) do
            if newCnt < cf.needCnt then
                break
            end
            if oldCnt < cf.needCnt then
                local title = MailExcel.mail[26].title
                local content = Util.format(MailExcel.mail[26].content, cf.needCnt)
                local senderName = MailExcel.mail[26].senderName
                MailManager.add(MailManager.SYSTEM, human.db._id, title, content, cf.reward, senderName)
            end     
        end
        if newCnt < maxSummonCnt then
            break
        end
        oldCnt = 0
        newCnt = newCnt - maxSummonCnt
        roundCnt = round
    end
    setRoundCnt(human, roundCnt)
    setSummonCnt(human, summonCnt)

    
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        break
    end
end

--[[
function isRed(human)
    local roundCnt = getRoundCnt(human)
    local maxRoundCnt = MonthactExcel.sumWinScore[1].reCnt
    if roundCnt < maxRoundCnt then
        return true
    end   
end
--]]