-- 月度活动   好战者活动 龙争虎斗  竞技高手
-- 增加个人竞技场和君王试炼得参与

-- human.db.actMonthTask.jifen
-- human.db.actMonthTask.trial

local MailExcel = require("excel.mail")
local MonthActExcel = require("excel.monthAct")
local HzzExcel = MonthActExcel.hzz
local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Grid = require("bag.Grid")
local CombatDefine = require("combat.CombatDefine")
local MonthActFill = require("monthAct.MonthDataFill")
local MonthActLogic = require("monthAct.MonthActLogic")
local MailManager = require("mail.MailManager")
local YunYingLogic = require("yunying.YunYingLogic")

MONTHACT_HZZ_TYPE_1  = 1        -- 个人竞技场
MONTHACT_HZZ_TYPE_2  = 2        -- 冠军试炼
MONTHACT_HZZ_FINISH_TYPE = 3    --  好战者活动完成所有其他任务的任务type

-- 个人竞技场 积分
local function getJifen(human)
    if not human.db.actMonthTask then
        return 0
    end
    return human.db.actMonthTask.jifen or 0
end

function setJifen(human, value)
    human.db.actMonthTask = human.db.actMonthTask or {}
    human.db.actMonthTask.jifen = value
end

-- 冠军试炼 积分
local function getTrial(human)
   if not human.db.actMonthTask then
        return 0
    end
    return human.db.actMonthTask.trial or 0
end

function setTrial(human, value)
    human.db.actMonthTask = human.db.actMonthTask or {}
    human.db.actMonthTask.trial = value
end

local TASKTYPE_2_MAXVALUE = nil
local function getMaxValue(taskType)
    if not TASKTYPE_2_MAXVALUE then
        TASKTYPE_2_MAXVALUE = {}
        for _, cf in ipairs(HzzExcel) do
            local oldvalue = TASKTYPE_2_MAXVALUE[cf.type] or 0 
            TASKTYPE_2_MAXVALUE[cf.type] = math.max(oldvalue, cf.taskCnt)
        end
    end
    return TASKTYPE_2_MAXVALUE[taskType] or 0
end

local function getCurValue(human, taskType)
    if taskType == MONTHACT_HZZ_TYPE_1 then
        return getJifen(human)
    end
    if taskType == MONTHACT_HZZ_TYPE_2 then
        return getTrial(human)
    end
    return isMax(human) and 1 or 0
end

--检查好战者活动是否全部完成
function isMax(human)
    if getJifen(human) >= getMaxValue(MONTHACT_HZZ_TYPE_1) and 
        getTrial(human) >= getMaxValue(MONTHACT_HZZ_TYPE_2) then
        return true
    end
end

local function checkHzzActAllFinish(human)
    if isMax(human) then
        -- 满足条件 立马把 jifen 和 trial 存入db防止多次触发
        ObjHuman.save(human)
        -- 发送奖励邮件
        local items = {}        
        for i = 1, #HzzExcel do
            local config = HzzExcel[i]
            if config.type == MONTHACT_HZZ_FINISH_TYPE then
                items = config.reward
                break
            end
        end
        local title = MailExcel.mail[10].title
        local content = MailExcel.mail[10].content
		local senderName = MailExcel.mail[10].senderName
        MailManager.add(MailManager.SYSTEM, human.db._id, title, content, items, senderName)
    end
end

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

-- 个人竞技场 计数
function addJjcFight(human, state, jifen)
    -- 检查活动是否结束
    MonthActLogic.checkActEndTime(human)

    local oldJifen = getJifen(human)
    local maxCnt = getMaxValue(MONTHACT_HZZ_TYPE_1)
    if oldJifen >= maxCnt then
        return
    end

    jifen = jifen or ((state == CombatDefine.RESULT_WIN) and 2 or 1)
    local newJifen = math.min(oldJifen + jifen, maxCnt)

    -- 存db
    setJifen(human, newJifen)
    if newJifen >= maxCnt then        
        --活动积分已达最大  检查活动任务是否都完成了
        checkHzzActAllFinish(human)
    end

    -- 有任务完成 就发放奖励
    for _, cf in ipairs(HzzExcel) do
        if cf.type == MONTHACT_HZZ_TYPE_1 and oldJifen < cf.taskCnt and newJifen >= cf.taskCnt then
            ObjHuman.save(human)
            --发放奖励 todo 邮件id未确定
            --local title = MailExcel.mail[8].title
            --local content = Util.format(MailExcel.mail[8].content, cf.taskCnt)
            --local senderName = MailExcel.mail[8].senderName            
            --MailManager.add(MailManager.SYSTEM, human.db._id, title, content, cf.reward, senderName)            
        end
    end
end

-- 冠军试炼 计数
function addTrialCnt(human, state, jifen)
    -- 检查活动是否结束
    MonthActLogic.checkActEndTime(human)

    local oldJifen = getTrial(human)
    local maxCnt = getMaxValue(MONTHACT_HZZ_TYPE_2)
    if oldJifen >= maxCnt then
        return
    end

    jifen = jifen or ((state == CombatDefine.RESULT_WIN) and 2 or 1)
    local newJifen = math.min(oldJifen + jifen, maxCnt)

    -- 存db
    setTrial(human, newJifen)
    if newJifen >= maxCnt then        
        --活动积分已达最大  检查活动任务是否都完成了
        checkHzzActAllFinish(human)
    end

    -- 有任务完成 就发放奖励
    for _, cf in ipairs(HzzExcel) do
        if cf.type == MONTHACT_HZZ_TYPE_2 and oldJifen < cf.taskCnt and newJifen >= cf.taskCnt then
            ObjHuman.save(human)
            --发放奖励 todo
            local title = MailExcel.mail[9].title
            local content = Util.format(MailExcel.mail[9].content, cf.taskCnt)
            local senderName = MailExcel.mail[9].senderName            
            MailManager.add(MailManager.SYSTEM, human.db._id, title, content, cf.reward, senderName)            
        end
    end
end





