local BountyManager = class("BountyManager", BaseModule)

function BountyManager:showBountyMainUI()
	UIManager:showUI("app/ui/bounty/bounty_main_ui")
end

function BountyManager:showBountyBuyUI()
	DataManager.BountyData:markPopBought()
	UIManager:showUI("app/ui/bounty/bounty_buy_ui")
end

function BountyManager:claimReward(index, isPro)
	local args = {
		level = index,
		is_pro = isPro,
	}
	self:sendMessage(ProtoMsgType.FromMsgEnum.BountyRewardReq, args, {}, self.onClaimReward, BIReport.ITEM_GET_TYPE.BOUNTY_REWARD)
end

function BountyManager:onClaimReward(result)
	if result.err_code == GConst.ERROR_STR.SUCCESS then
		if result.rewards then
			-- 读表获取该层奖励 如果是箱子走单独奖励展示接口
			local isOneKeyGet = false
			local isSpecialBox = false
			local index = result.reqData.level
			local rewardId
			if index == 0 then -- 一键领取
				isOneKeyGet = true
			else
				local isPro = result.reqData.is_pro
				local info = DataManager.BountyData:getSeasonInfoByLevel(index)
				local rewardType
				if info then
					local reward = isPro and info.reward_pro or info.reward
					rewardType = reward and reward.type
					rewardId = reward and reward.id
					if rewardType == GConst.REWARD_TYPE.ITEM then
						local itemCfgInfo = ConfigManager:getConfig("item")[rewardId]
						if itemCfgInfo and itemCfgInfo.type == GConst.ItemConst.ITEM_TYPE.BOX then
							isSpecialBox = true
						end
					end
				end
			end

			if isSpecialBox and not isOneKeyGet then
				ModuleManager.ShopManager:showBoxOpenUI({type = GConst.ShopConst.BOX_REWARD_TYPE.BOUNTY, params = rewardId, rewards = result.rewards})
				local openType = BIReport.BOX_OPEN_OPEN_TYPE.BOUNTY
				BIReport:postBoxOpen(rewardId, BIReport.BOX_OPEN_BOX_TYPE.ACTIVITY, openType, 0, result.rewards)
			else
				GFunc.showRewardBox(result.rewards)
			end
		end
		if result.reqData.is_pro ~= nil then
			local BountyData = DataManager.BountyData
			local index = result.reqData.level
			if index ~= 0 then
				if result.reqData.is_pro then
					BountyData:onClaimProReward(result.reqData.level)
				else
					BountyData:onClaimReward(result.reqData.level)
				end
			else
				BountyData:onOneKeyClaimReward()
			end

			BIReport:postBountyReward(BountyData:getBountyReportType(), BountyData:getLevel(), BountyData:getExp(), BountyData:getSeason(), result.reqData.level, result.reqData.is_pro)
		end
    end
end

function BountyManager:buyBountyLevel()
	if not DataManager.BountyData:getIfCanBuyLevel() then
		return
	end
	local cost = DataManager.BountyData: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.BOUNTY_BUY_LEVEL,
		costId = costId,
		costNum = costNum,
		okFunc = function()
			self:sendMessage(ProtoMsgType.FromMsgEnum.BountyLevelUnlockReq, {}, {}, self.onBoughtBountyLevel, BIReport.ITEM_GET_TYPE.BOUNTY_UNLOCK_LEVEL)
		end,
	}
	GFunc.showMessageBox(params)
end

function BountyManager:onBoughtBountyLevel(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
		DataManager.BountyData:onBoughtLevel()
    end
end

function BountyManager:buyBounty(advanced)
    if not DataManager.BountyData:getIsOpen() then
        return
    end
	PayManager:purchasePackage(DataManager.BountyData:getGiftId(advanced), PayManager.PURCHARSE_TYPE.ACT_GIFT)
end

function BountyManager:onBoughtBountyFinish(result)
	if result.status == 0 then
    	DataManager.BountyData:setBought(result.season, result.level)
	end
end

return BountyManager