local TournWaveManager = class("TournWaveManager", BaseModule)

function TournWaveManager:showActMainUI()
    UIManager:showUI("app/ui/activity/tourn_wave/tourn_wave_ui")
end

-- 初始化计时器
function TournWaveManager:initTimer()
    self:unscheduleGlobal(self.actSid)

    if DataManager.TournWaveData:isActiveTime() then
        Logger.logHighlight("波次锦标赛活动结束倒计时："..DataManager.TournWaveData:getEndRemainTime())
        self.actSid = self:performWithDelayGlobal(function()
            Logger.logHighlight("波次锦标赛活动结束")
            EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.ACTIVITY_TOURN_WAVE_END)
        end, DataManager.TournWaveData:getEndRemainTime())
    elseif DataManager.TournWaveData:getStartRemainTime() > 0 then
        Logger.logHighlight("波次锦标赛活动开始倒计时："..DataManager.TournWaveData:getStartRemainTime())
        self.actSid = self:performWithDelayGlobal(function()
            Logger.logHighlight("波次锦标赛活动开始")
            DataManager.ShopData:resetTournWaveGift()
            self:reqActData()
        end, DataManager.TournWaveData:getStartRemainTime())
    end
end

-- 购买活动战令
function TournWaveManager:buyBounty(giftId)
    PayManager:purchasePackage(giftId, PayManager.PURCHARSE_TYPE.ACT_GIFT)
end

-- 请求活动数据
function TournWaveManager:reqActData()
    if not DataManager.TournWaveData:isOpen() then
        return
    end
    if not DataManager.TournWaveData:isActiveTime() then
        return
    end
    self:sendMessage(ProtoMsgType.FromMsgEnum.TournWaveDataReq, {}, {}, self.rspActData, nil)
end

function TournWaveManager:rspActData(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.TournWaveData:onGetActData(result.tourn_wave)
    end
end

-- 请求战令奖励
function TournWaveManager:reqBountyReward(id, grade)
    id = 0-- 默认领所有可领奖励
    self:sendMessage(ProtoMsgType.FromMsgEnum.TournWaveBountyClaimReq, {id = id, grade = grade}, {}, self.rspBountyReward, BIReport.ITEM_GET_TYPE.TOURN_WAVE_BOUNTY_REWARD)
end

function TournWaveManager:rspBountyReward(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.TournWaveData:onReceivedBountyReward(result.success_id_grade)

        GFunc.showRewardBox(result.rewards)
    end
end

-- 购买战令等级
function TournWaveManager:reqBuyBountyLevel()
    local cost = DataManager.TournWaveData:getBuyBountyLevelCost()
	local costId = GFunc.getRewardId(cost)
	local costNum = GFunc.getRewardNum(cost)
	if not GFunc.checkCost(costId, costNum, true) then
		return
	end
	local params ={
		titleTx = I18N:getGlobalText(I18N.GlobalConst.BOUNTY_BUY_LEVEL_TITLE),
		content = I18N:getGlobalText(I18N.GlobalConst.BOUNTY_BUY_LEVEL_COUNTENT),
		boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
		showToday = GConst.MESSAGE_BOX_SHOW_TODAY.ACTIVITY_BUY_LEVEL,
		costId = costId,
		costNum = costNum,
		okFunc = function()
            local id = DataManager.TournWaveData:getBountyIdList()[DataManager.TournWaveData:getBountyLevel() + 1]
            -- Logger.logHighlight("购买战令等级:"..id)
			self:sendMessage(ProtoMsgType.FromMsgEnum.TournWaveBountyClaimByDiamondReq, {id = id}, {}, self.rspBuyBountyLevel, BIReport.ITEM_GET_TYPE.TOURN_WAVE_BOUNTY_LEVEL)
		end,
	}
	GFunc.showMessageBox(params)
end

function TournWaveManager:rspBuyBountyLevel(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.TournWaveData:onBoughtBountyLevel()
    end
end

-- 获取排行榜数据
function TournWaveManager:reqRankList(turn)
    self:sendMessage(ProtoMsgType.FromMsgEnum.TournWaveRankReq, {turn = turn}, {}, self.rspRankList, nil)
end

function TournWaveManager:rspRankList(result)
    DataManager.TournWaveData:setInReset(false)
    if result.err_code == GConst.ERROR_STR.SUCCESS and result.reqData then
        DataManager.TournWaveData:onGetRankData(result.reqData.turn, result.ranks, result.score, result.own_rank)
    end
end

-- 领取排行榜奖励
function TournWaveManager:reqRankReward()
    self:sendMessage(ProtoMsgType.FromMsgEnum.TournWaveRankClaimRewardReq, {}, {}, self.rspRankReward, BIReport.ITEM_GET_TYPE.TOURN_WAVE_RANK_REWARD)
end

function TournWaveManager:rspRankReward(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.TournWaveData:onReceivedRankReward()
        GFunc.showRewardBox(result.rewards)
    end
end

return TournWaveManager