local PrivilegeCardData = class("PrivilegeCardData", BaseData)

function PrivilegeCardData:ctor()
	self.data.isDirty = false
end

function PrivilegeCardData:clear()
	DataManager:unregisterCrossDayFunc("PrivilegeCardData")
end

function PrivilegeCardData:setDirty()
	self.data.isDirty = not self.data.isDirty
end

function PrivilegeCardData:init(data)
	if EDITOR_MODE then
		Logger.logHighlight("初始化特权卡数据")
		Logger.printTable(data)
	end
	data = data or GConst.EMPTY_TABLE
	self.cardData = data.cards or {}
	self.isClaimedFreeDailyReward = data.claimed
	DataManager:registerCrossDayFunc("PrivilegeCardData", function()
		self.isClaimedFreeDailyReward = false
		if self:getIsCardActive(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD) then
			self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD].reward = (self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD].reward or 0) + 1
		end
		-- if self:getIsCardActive(GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD) then
		-- 	self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD].reward = (self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD].reward or 0) + 1
		-- end
		self:setDirty()
	end)
end

function PrivilegeCardData:getIsClaimedFreeDailyReward()
	return self.isClaimedFreeDailyReward
end

function PrivilegeCardData:getIsOpen(cardId)
	if GFunc.isShenhe() then
		return false
	end
	if cardId then
		if cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD then
			if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.CARD_MONTHLY, true) then
				return false
			end
		elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD then
			if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.CARD_AD, true) then
				return false
			end
		-- elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD then
		-- 	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.LIFETIME_CARD, true) then
		-- 		return false
		-- 	end
		-- elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.CHALLEGE_CARD then
		-- 	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.CHALLEGE_CARD, true) then
		-- 		return false
		-- 	end
		end
	else
		local isOpen = false
		for k, v in pairs(GConst.ShopConst.PRIVILEGE_CARD_ID) do
			if self:getIsOpen(v) then
				isOpen = true
				break
			end
		end
		return isOpen
	end
	return true
end

function PrivilegeCardData:onClaimedFreeDailyReward()
	self.isClaimedFreeDailyReward = true
	self:setDirty()
end

function PrivilegeCardData:onClaimedPurchaseCardDailyReward(id)
	if id == 0 then -- 一键全领
		if self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD] then
			self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD].reward = 0
		end
		-- if self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD] then
		-- 	self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD].reward = 0
		-- end
	elseif id == GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD then
		if self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD] then
			self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD].reward = 0
		end
	-- elseif id == GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD then
	-- 	if self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD] then
	-- 		self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD].reward = 0
	-- 	end
	end
	self:setDirty()
end

function PrivilegeCardData:getIsMonthlyCardOpen()
	return self:getIsOpen(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
end

function PrivilegeCardData:checkCanBuyMonthlyCard()
	if self:getIsOpen(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD) then
		local remainTime = self:getCardRemainTime(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
		return remainTime <= 0
	end
	return false
end

function PrivilegeCardData:getIsChallengeCardOpen()
	-- return self:getIsOpen(GConst.ShopConst.PRIVILEGE_CARD_ID.CHALLEGE_CARD)
end

function PrivilegeCardData:checkCanBuyChallengeCard()
	-- if self:getIsOpen(GConst.ShopConst.PRIVILEGE_CARD_ID.CHALLEGE_CARD) then
	-- 	local remainTime = self:getCardRemainTime(GConst.ShopConst.PRIVILEGE_CARD_ID.CHALLEGE_CARD)
	-- 	return remainTime <= 0
	-- end
	return false
end

function PrivilegeCardData:getIsAdCardOpen()
	return self:getIsOpen(GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD)
end

function PrivilegeCardData:checkCanBuyAdCard()
	if self:getIsOpen(GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD) then
		local data = self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD]
		if data == nil then
			return true
		end
		if data.expire_at == nil then
			return true
		end
		if data.expire_at >= 0 then
			local remainTime = Time:getDayBeginTimeStamp(data.expire_at) - Time:getServerTime()
			if remainTime <= 0 then
				return true
			end
		end
	end
	return false
end

function PrivilegeCardData:getIsLifetimeCardOpen()
	-- return self:getIsOpen(GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD)
end

function PrivilegeCardData:checkCanBuyLifetimeCard()
	-- if self:getIsOpen(GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD) then
	-- 	local data = self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD]
	-- 	if data == nil then
	-- 		return true
	-- 	end
	-- 	if data.expire_at == nil then
	-- 		return true
	-- 	end
	-- 	if data.expire_at >= 0 then
	-- 		local remainTime = Time:getDayBeginTimeStamp(data.expire_at) - Time:getServerTime()
	-- 		if remainTime <= 0 then
	-- 			return true
	-- 		end
	-- 	end
	-- end
	return false
end

function PrivilegeCardData:getIsLifetimeCardActive()
	-- return self:getIsCardActive(GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD)
end

function PrivilegeCardData:getIsSkipAd()
	if self:getIsCardActive(GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD) then
		return true
	end
	return false
end

function PrivilegeCardData:getIsChallegeCardActive()
	-- return self:getIsCardActive(GConst.ShopConst.PRIVILEGE_CARD_ID.CHALLEGE_CARD)
end

function PrivilegeCardData:getIsMonthlyCardActive()
	return self:getIsCardActive(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
end

function PrivilegeCardData:getIsCardActive(cardId)
	if not self.cardData then
		return false
	end
	if cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD then
		local remainTime = self:getCardRemainTime(cardId)
		return remainTime > 0
	elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD then
		local data = self.cardData[cardId]
		if data and data.expire_at < 0 then
			return true
		end
	-- elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD then
	-- 	local data = self.cardData[cardId]
	-- 	if data and data.expire_at < 0 then
	-- 		return true
	-- 	end
	-- elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.CHALLEGE_CARD then
	-- 	local remainTime = self:getCardRemainTime(cardId)
	-- 	return remainTime > 0
	end
	return false
end

function PrivilegeCardData:getCardRemainTime(cardId)
	local data = self.cardData[cardId]
	if data == nil or data.expire_at <= 0 then
		return 0
	end
	local remainTime = Time:getDayBeginTimeStamp(data.expire_at) - Time:getServerTime()
	if remainTime > 0 then
		return remainTime
	else
		return 0
	end
end

-- 月卡购买一次增加的累计天数
function PrivilegeCardData:getMonthlyCardOneTurnDays()
	return GFunc.getConstIntValue("card_monthly_days")
end

-- 购买成功
function PrivilegeCardData:onGiftBuySuccess(cardId)
	if cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD then
		if self:getIsCardActive(cardId) then
			self.cardData[cardId].expire_at = self.cardData[cardId].expire_at + self:getMonthlyCardOneTurnDays() * 86400
		else
			self.cardData[cardId] = self.cardData[cardId] or {}
			self.cardData[cardId].expire_at = Time:getServerTime() + self:getMonthlyCardOneTurnDays() * 86400
			local rewardCount = self.cardData[cardId].reward or 0
			if rewardCount <= 0 then
				rewardCount = 1
			else
				rewardCount = rewardCount + 1
			end
			self.cardData[cardId].reward = rewardCount
		end
	elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.CARD_AD then
		if not self:getIsCardActive(cardId) then
			self.cardData[cardId] = self.cardData[cardId] or {}
			self.cardData[cardId].expire_at = -1
		end
		-- 局内事件
		EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.NO_AD_ACTIVE)
	-- elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD then
	-- 	if not self:getIsCardActive(cardId) then
	-- 		self.cardData[cardId] = self.cardData[cardId] or {}
	-- 		self.cardData[cardId].expire_at = -1
	-- 		local rewardCount = self.cardData[cardId].reward or 0
	-- 		 if rewardCount <= 0 then
	-- 			rewardCount = 1
	-- 		else
	-- 			rewardCount = rewardCount + 1
	-- 		end
	-- 		self.cardData[cardId].reward = rewardCount
	-- 	end
	-- elseif cardId == GConst.ShopConst.PRIVILEGE_CARD_ID.CHALLEGE_CARD then
	-- 	if self:getIsCardActive(cardId) then
	-- 		self.cardData[cardId].expire_at = self.cardData[cardId].expire_at + self:getMonthlyCardOneTurnDays() * 86400
	-- 	else
	-- 		self.cardData[cardId] = self.cardData[cardId] or {}
	-- 		self.cardData[cardId].expire_at = Time:getServerTime() + self:getMonthlyCardOneTurnDays() * 86400
	-- 	end
	-- 	-- 购买成功添加体力
	-- 	local recoveryCfg = ConfigManager:getConfig("recovery")[GConst.ItemConst.ITEM_ID_VIT]
	-- 	local addVit = recoveryCfg and recoveryCfg.monthly_card or 0
	-- 	DataManager.BagData.ItemData:addItemReward({type = GConst.REWARD_TYPE.ITEM, id = GConst.ItemConst.ITEM_ID_VIT, num = addVit}, BIReport.ITEM_GET_TYPE.PRIVILEGE_CHALLENGE_CARD)
	end

	self:setDirty()
end

function PrivilegeCardData:getRedPoint()
	local haveUnclaimedRewards = false
	if not self:getIsClaimedFreeDailyReward() then
		haveUnclaimedRewards = true
	end
	if self:getIsMonthlyCardOpen() then
		local remainTime = self:getCardRemainTime(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
		if remainTime > 0 then
			local isClaimed = self:getIsClaimedMonthlyCardDailyReward()
			if not isClaimed then
				haveUnclaimedRewards = true
			end
		end
	end
	if self:getIsLifetimeCardOpen() then
		local isActive = self:getIsCardActive(GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD)
		if isActive then
			local isClaimed = self:getIsClaimedLifetimeCardDailyReward()
			if not isClaimed then
				haveUnclaimedRewards = true
			end
		end
	end
	return haveUnclaimedRewards
end

-- 月卡
function PrivilegeCardData:getIsClaimedMonthlyCardDailyReward()
	if self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD] then
		local count = self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD].reward or 0
		return count <= 0
	end
	return false
end

function PrivilegeCardData:getUnclaimedMonthlyCardDailyRewardCount()
	if self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD] then
		return self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD].reward or 0
	end
	return 0
end

function PrivilegeCardData:getMonthlyCardDailyRewards()
	if self._monthlyCardDailyRewards == nil then
		self._monthlyCardDailyRewards = GFunc.getConstCost("card_monthly_dailyreward", true)
	end
	return self._monthlyCardDailyRewards
end

-- 终身卡
function PrivilegeCardData:getIsClaimedLifetimeCardDailyReward()
	-- if self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD] then
	-- 	local count = self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD].reward or 0
	-- 	return count <= 0
	-- end
	return false
end

function PrivilegeCardData:getUnclaimedLifetimeCardDailyRewardCount()
	-- if self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD] then
	-- 	return self.cardData[GConst.ShopConst.PRIVILEGE_CARD_ID.LIFETIME_CARD].reward or 0
	-- end
	return 0
end

function PrivilegeCardData:getLifetimeCardDailyRewards()
	if self._lifetimeCardDailyRewards == nil then
		self._lifetimeCardDailyRewards = GFunc.getConstCost("card_forever_dailyreward", true)
	end
	return self._lifetimeCardDailyRewards
end

return PrivilegeCardData