-- 月度活动   合成与觉醒 百炼成神

-- human.db.actMonthTask.hecheng
local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local MonthactExcel = require("excel.monthAct")
local MailExcel = require("excel.mail")
local HeroExcel = require("excel.hero").hero
local Grid = require("bag.Grid")
local MonthActFill = require("monthAct.MonthDataFill")
local MonthActLogic = require("monthAct.MonthActLogic")
local MailManager = require("mail.MailManager")
local YunYingLogic = require("yunying.YunYingLogic")

MONTHACT_HCJX_TYPE_1  = 1        -- 合成任务
MONTHACT_HCJX_TYPE_2  = 2        -- 觉醒任务

local function getNowValue(human, id)
    if not human.db.actMonthTask or 
        not human.db.actMonthTask.hecheng then
        return 0
    end
    return human.db.actMonthTask.hecheng[id] or 0
end

local function setNowValue(human, id, value)
    human.db.actMonthTask = human.db.actMonthTask or {}
    human.db.actMonthTask.hecheng = human.db.actMonthTask.hecheng or {}
    human.db.actMonthTask.hecheng[id] = value
end

function query(human)    
    local msgRet = Msg.gc.GC_MONTHACT_HCJX_QUERY 
    msgRet.leftTime = MonthActLogic.getActLeftTime(human)
    msgRet.list[0] = #MonthactExcel.hecheng
    for id, cf in ipairs(MonthactExcel.hecheng) do 
        MonthActFill.makeMonthTaskData(msgRet.list[id], cf, id)  
        msgRet.list[id].cur = getNowValue(human, id)  
    end
    --Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

--合成觉醒活动  计数入口   Ex 之前
function addMonthHecheJuexCntEx(human, hcType, star, heroID)
    -- 检查任务类型是否非法
    if not hcType then return end

    MonthActLogic.checkActEndTime(human)
    star = star or 0
    for id, cf in ipairs(MonthactExcel.hecheng) do
        if cf.type == hcType and star == cf.special then
            addMonthHecheJuexCnt(human, id, heroID)
            break
        end
    end    
end

function addMonthHecheJuexCnt(human, id, heroID)
    --检查id是否非法
    local cf = MonthactExcel.hecheng[id]
    if not cf then return end

    local oldStep = getNowValue(human, id)

    -- 已经达到完成上限
    if oldStep >= cf.taskCnt then return end

    local newStep = oldStep + 1
    setNowValue(human, id, newStep)
    ObjHuman.save(human)
    --完成一次 发放奖励一次   
    local mailId = 0
    if cf.type == MONTHACT_HCJX_TYPE_1 then
        mailId = 13
    elseif cf.type == MONTHACT_HCJX_TYPE_2 then
        mailId = 14
    end
    if mailId == 0 then return end
    
    local heroConfig = HeroExcel[heroID]
    local heroName = heroConfig and heroConfig.name or ""
    local title = MailExcel.mail[mailId].title
    local content = Util.format(MailExcel.mail[mailId].content, cf.special, heroName)
    local senderName = MailExcel.mail[mailId].senderName    
    MailManager.add(MailManager.SYSTEM, human.db._id, title, content, cf.reward, senderName)
    
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        break
    end
end

-- GM
function addCntByGm(human,type,heroId, num)
    if not num or not type or not heroId then 
        return 
    end
    local heroConfig = HeroExcel[heroId]
    if not heroConfig then return end
    for i= 1,num do
        addMonthHecheJuexCntEx(human, type, heroConfig.star , heroId)
    end
end

--[[
function isRed(human)
    for id, cf in pairs(MonthactExcel.hecheng) do
        -- 已经达到完成上限
        if getNowValue(human, id) < cf.taskCnt then
            return true
        end
    end
end
--]]
