local TournArenaManager = class("TournArenaManager", BaseModule)

function TournArenaManager:init()
    self:addEventListener(EventManager.CUSTOM_EVENT.ARENA_BATTLE_SETTLEMENT, function(isWin)
        self:reqActData()
    end)
end

function TournArenaManager:showActMainUI()
    UIManager:showUI("app/ui/activity/tourn_arena/tourn_arena_ui")
end

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

    if DataManager.TournArenaData:isActiveTime() then
        Logger.logHighlight("竞技场锦标赛活动结束倒计时："..DataManager.TournArenaData:getEndRemainTime())
        self.actSid = self:performWithDelayGlobal(function()
            Logger.logHighlight("竞技场锦标赛活动结束")
            EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.ACTIVITY_TOURN_ARENA_END)
        end, DataManager.TournArenaData:getEndRemainTime())
    elseif DataManager.TournArenaData:getStartRemainTime() > 0 then
        Logger.logHighlight("竞技场锦标赛活动开始倒计时："..DataManager.TournArenaData:getStartRemainTime())
        self.actSid = self:performWithDelayGlobal(function()
            Logger.logHighlight("竞技场锦标赛活动开始")
            DataManager.ShopData:resetTournArenaGift()
            self:reqActData()
        end, DataManager.TournArenaData:getStartRemainTime())
    end
end

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

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

function TournArenaManager:rspActData(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.TournArenaData:onGetActData(result.tourn_arena)
    end
end

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

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

        GFunc.showRewardBox(result.rewards)
    end
end

-- 购买战令等级
function TournArenaManager:reqBuyBountyLevel()
    local cost = DataManager.TournArenaData: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.TournArenaData:getBountyIdList()[DataManager.TournArenaData:getBountyLevel() + 1]
            -- Logger.logHighlight("购买战令等级:"..id)
			self:sendMessage(ProtoMsgType.FromMsgEnum.TournArenaBountyClaimByDiamondReq, {id = id}, {}, self.rspBuyBountyLevel, BIReport.ITEM_GET_TYPE.TOURN_ARENA_BOUNTY_LEVEL)
		end,
	}
	GFunc.showMessageBox(params)
end

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

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

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

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

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

return TournArenaManager