local PrivilegeCardManager = class("MonthlyCardManager", BaseModule)

function PrivilegeCardManager:showPrivilegeUI(showPage)
	local params = {
		showPage = showPage,
	}
	UIManager:showUI("app/ui/privilege/privilege_main_ui", params)
end

function PrivilegeCardManager:getFreeDailyRewards()
	if DataManager.PrivilegeCardData:getIsClaimedFreeDailyReward() then
		return
	end
	self:sendMessage(ProtoMsgType.FromMsgEnum.CardDailyRewardReq, {}, self.onGetFreeDailyRewards, BIReport.ITEM_GET_TYPE.PRIVILEGE_FREE_DAILY_REWARD)
end

function PrivilegeCardManager:onGetFreeDailyRewards(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	DataManager.PrivilegeCardData:onClaimedFreeDailyReward()
	if result.rewards then
		GFunc.showRewardBox(result.rewards)
	end
	-- BI
	BIReport:postPrivilegeCardGetDailyReward(result.rewards)
end

function PrivilegeCardManager:buyMonthlyCard()
	local canBuyCard = DataManager.PrivilegeCardData:checkCanBuyMonthlyCard()
	if canBuyCard then
		local giftId = GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD
		PayManager:purchasePackage(giftId, PayManager.PURCHARSE_TYPE.ACT_GIFT, true)
	else
		GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_17))
	end
end

function PrivilegeCardManager:buyChallengeCard()
	-- local canBuyCard = DataManager.PrivilegeCardData:checkCanBuyChallengeCard()
	-- if canBuyCard then
	-- 	local giftId = GConst.ShopConst.PRIVILEGE_CARD_ID.CHALLEGE_CARD
	-- 	PayManager:purchasePackage(giftId, PayManager.PURCHARSE_TYPE.ACT_GIFT, true)
	-- else
	-- 	GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_17))
	-- end
end

function PrivilegeCardManager:buyAdCard()
	local canBuyCard = DataManager.PrivilegeCardData:checkCanBuyAdCard()
	if canBuyCard then
		local giftId = GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD
		PayManager:purchasePackage(giftId, PayManager.PURCHARSE_TYPE.ACT_GIFT, true)
	else
		GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_17))
	end
end

function PrivilegeCardManager:buyLifetimeCard()
	-- local canBuyCard = DataManager.PrivilegeCardData:checkCanBuyLifetimeCard()
	-- if canBuyCard then
	--     local giftId = GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD
	--     PayManager:purchasePackage(giftId, PayManager.PURCHARSE_TYPE.ACT_GIFT, true)
	-- else
	--     GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_17))
	-- end
end

function PrivilegeCardManager:getPurchaseCardDailyReward(id)
	local itemGetType = BIReport.ITEM_GET_TYPE.PRIVILEGE_ALL_CARD_DAILY_REWARD
	if id == GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD then
		itemGetType = BIReport.ITEM_GET_TYPE.PRIVILEGE_MONTHLY_CARD_DAILY_REWARD
	-- elseif id == GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD then
	--     itemGetType = BIReport.ITEM_GET_TYPE.PRIVILEGE_LIFETIME_CARD_DAILY_REWARD
	elseif id == 0 then
		itemGetType = BIReport.ITEM_GET_TYPE.PRIVILEGE_ALL_CARD_DAILY_REWARD
	else
		return
	end
	self:sendMessage(ProtoMsgType.FromMsgEnum.CardRewardReq, {id = id}, self.onGetPurchaseCardDailyReward, itemGetType)
end

function PrivilegeCardManager:onGetPurchaseCardDailyReward(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	DataManager.PrivilegeCardData:onClaimedPurchaseCardDailyReward(result.reqData.id)
	if result.reqData.id == 0 then
		DataManager.PrivilegeCardData:onClaimedFreeDailyReward()
	end
	if result.rewards then
		GFunc.showRewardBox(result.rewards)
	end
	-- BI
	BIReport:postPrivilegeCardGetPurchaseDailyReward(result.rewards, result.reqData.id)
end

--region 上报
-- 特权卡
BIReport.ITEM_GET_TYPE.PRIVILEGE_CARD = "privilege_card"
BIReport.ITEM_GET_TYPE.PRIVILEGE_FREE_DAILY_REWARD = "privilege_free_daily_reward"
BIReport.ITEM_GET_TYPE.PRIVILEGE_CHALLENGE_CARD = "privilege_challenge_card"
BIReport.ITEM_GET_TYPE.PRIVILEGE_MONTHLY_CARD_DAILY_REWARD = "privilege_monthly_card_daily_reward"
BIReport.ITEM_GET_TYPE.PRIVILEGE_LIFETIME_CARD_DAILY_REWARD = "privilege_lifetime_card_daily_reward"
BIReport.ITEM_GET_TYPE.PRIVILEGE_ALL_CARD_DAILY_REWARD = "privilege_all_card_daily_reward"

BIReport.PRIVILEGE_CARD_OPT_TYPE = {
	FREE_DAILY_REWARD = "FreeDailyReward",
	PURCHASE_DAILY_REWARD = "PurchaseDailyReward",
}

local EVENT_NAME_PRIVILEGE_CARD = "event_privilege_card" -- 特权卡

function BIReport:postPrivilegeCardGetDailyReward(rewards)
	local args = {
		opt_type = BIReport.PRIVILEGE_CARD_OPT_TYPE.FREE_DAILY_REWARD,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil
	}
	self:report(EVENT_NAME_PRIVILEGE_CARD, args)
end

function BIReport:postPrivilegeCardGetPurchaseDailyReward(rewards, cardType)
	local args = {
		opt_type = BIReport.PRIVILEGE_CARD_OPT_TYPE.PURCHASE_DAILY_REWARD,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
		cardType = cardType
	}
	self:report(EVENT_NAME_PRIVILEGE_CARD, args)
end
--endregion

return PrivilegeCardManager