local MonthlyCardComp = class("MonthlyCardComp", LuaComponent)

function MonthlyCardComp:init()
	local uiMap = self:getUIMap()
	self.titleTx = uiMap["monthly_card.title_tx"]
	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.PRIVILEGE_CARD_NAME_3))

	self.descTx1 = uiMap["monthly_card.desc_bg_1.text"]
	self.descTx1:setText(I18N:getGlobalText(I18N.GlobalConst.PRIVILEGE_CARD_DESC_4))
	self.descTx2 = uiMap["monthly_card.desc_bg_2.text"]
	self.descTx2:setText(I18N:getGlobalText(I18N.GlobalConst.PRIVILEGE_CARD_DESC_5))
	self.descTx3 = uiMap["monthly_card.desc_bg_3.text"]
	self.descTx3:setText(I18N:getGlobalText(I18N.GlobalConst.PRIVILEGE_CARD_DESC_6))
	self.descTx4 = uiMap["monthly_card.desc_bg_4.text"]
	self.descTx4:setText(I18N:getGlobalText(I18N.GlobalConst.PRIVILEGE_CARD_DESC_7))
	self.descTx5 = uiMap["monthly_card.desc_tx_1"]
	self.descTx6 = uiMap["monthly_card.desc_tx_2"]
	self.descTx7 = uiMap["monthly_card.desc_tx_3"]

	self.rewadCell1 = uiMap["monthly_card.reward_bg_1.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	self.rewadCell12 = uiMap["monthly_card.reward_bg_1.reward_cell_1"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	self.rewardDesc1 = uiMap["monthly_card.reward_bg_1.text"]
	self.rewardDesc1:setText(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_7))
	self.rewadCell2 = uiMap["monthly_card.reward_bg_2.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	self.rewardDesc2 = uiMap["monthly_card.reward_bg_2.text"]
	self.rewardDesc2:setText(I18N:getGlobalText(I18N.GlobalConst.PRIVILEGE_CARD_DESC_3))

	self.payBtn = uiMap["monthly_card.btn_pay"]
	self.payBtn:addClickListener(function()
		local count = DataManager.PrivilegeCardData:getUnclaimedMonthlyCardDailyRewardCount()
		if count > 0 then -- 还有奖励没领取完
			ModuleManager.PrivilegeCardManager:getPurchaseCardDailyReward(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
		else
			local remainTime = DataManager.PrivilegeCardData:getCardRemainTime(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
			if remainTime > 0 then
				local isClaimed = DataManager.PrivilegeCardData:getIsClaimedMonthlyCardDailyReward()
				if not isClaimed then
					ModuleManager.PrivilegeCardManager:getPurchaseCardDailyReward(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
				end
			else
				ModuleManager.PrivilegeCardManager:buyMonthlyCard()
			end
		end
	end)
	self.payBtnTx = uiMap["monthly_card.btn_pay.text"]
	self.payBtnGrey = uiMap["monthly_card.btn_pay_grey"]
	self.payBtnGreyTx = uiMap["monthly_card.btn_pay_grey.text"]

	self.offImg = uiMap["monthly_card.off_img"]
	self.offText1 = uiMap["monthly_card.off_img.text"]
	self.offText2 = uiMap["monthly_card.off_img.text_2"]
	self.offText2:setText(I18N:getGlobalText(I18N.GlobalConst.VALUE_DESC))

	uiMap["monthly_card.ui_spine_obj"]:playAnim("idle", true, true)
end

function MonthlyCardComp:refresh()
	local cfg = DataManager.ShopData:getActGiftConfig(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
	if cfg == nil then
		return
	end
	local remainTime = DataManager.PrivilegeCardData:getCardRemainTime(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
    local isActive = remainTime > 0
	local rewards = DataManager.PrivilegeCardData:getMonthlyCardDailyRewards()
	if isActive then
		self.descTx5:setText("<color=#4BFF53>" .. I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_12) .. "</color>")
		self.descTx7:setText(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_14, Time:formatNumTimeStr(remainTime)))
		local isClaimed = DataManager.PrivilegeCardData:getIsClaimedMonthlyCardDailyReward()
		if rewards and rewards[1] then
			self.rewadCell1:refreshByConfig(rewards[1], isClaimed, isClaimed)
		end
		if rewards and rewards[2] then
			self.rewadCell12:refreshByConfig(rewards[2], isClaimed, isClaimed)
		end
		if isClaimed then
			self.payBtnGrey:setActive(true)
			self.payBtnGreyTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_DONE))
			self.payBtn:setActive(false)
			self.rewadCell1:hideFrameAnimation()
            self.rewadCell1:setClickShowTips()
			self.rewadCell12:hideFrameAnimation()
            self.rewadCell12:setClickShowTips()
			self.descTx6:setText(GConst.EMPTY_STRING)
			self.payBtn:removeRedPoint()
		else
			self.payBtnGrey:setActive(false)
			self.payBtn:setActive(true)
			self.payBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
			local count = DataManager.PrivilegeCardData:getUnclaimedMonthlyCardDailyRewardCount()
			if count > 1 then
				self.descTx6:setText("<color=#4BFF53>" .. I18N:getGlobalText(I18N.GlobalConst.PRIVILEGE_CARD_DESC_1, count) .. "</color>")
			else
				self.descTx6:setText(GConst.EMPTY_STRING)
			end
			if rewards and rewards[1] then
				local num = count*rewards[1].num
				self.rewadCell1:showRewardNum(GFunc.num2Str(num))
			end
			if rewards and rewards[2] then
				local num = count*rewards[2].num
				self.rewadCell12:showRewardNum(GFunc.num2Str(num))
			end
			self.rewadCell1:showFrameAnimation()
			self.rewadCell1:addClickListener(function()
				ModuleManager.PrivilegeCardManager:getPurchaseCardDailyReward(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
			end)
			self.rewadCell12:showFrameAnimation()
			self.rewadCell12:addClickListener(function()
				ModuleManager.PrivilegeCardManager:getPurchaseCardDailyReward(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
			end)
			self.payBtn:addRedPoint(70, 30, 1)
		end
    else
		self.descTx5:setText(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_13))
		self.descTx7:setText(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_15, 30))
		local count = DataManager.PrivilegeCardData:getUnclaimedMonthlyCardDailyRewardCount()
		if count > 0 then -- 还有奖励没领
			if count > 1 then
				self.descTx6:setText("<color=#4BFF53>" .. I18N:getGlobalText(I18N.GlobalConst.PRIVILEGE_CARD_DESC_1, count) .. "</color>")
			else
				self.descTx6:setText(GConst.EMPTY_STRING)
			end
			self.payBtnGrey:setActive(false)
			self.payBtn:setActive(true)
			self.payBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
			if rewards and rewards[1] then
				local num = count*rewards[1].num
				self.rewadCell1:refreshByConfig(rewards[1], false, false)
				self.rewadCell1:showRewardNum(GFunc.num2Str(num))
			end
			if rewards and rewards[2] then
				local num = count*rewards[2].num
				self.rewadCell12:refreshByConfig(rewards[2], false, false)
				self.rewadCell12:showRewardNum(GFunc.num2Str(num))
			end
			self.rewadCell1:showFrameAnimation()
			self.rewadCell1:addClickListener(function()
				ModuleManager.PrivilegeCardManager:getPurchaseCardDailyReward(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
			end)
			self.rewadCell12:showFrameAnimation()
			self.rewadCell12:addClickListener(function()
				ModuleManager.PrivilegeCardManager:getPurchaseCardDailyReward(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
			end)
		else
			self.descTx6:setText(GConst.EMPTY_STRING)
			self.payBtnGrey:setActive(false)
			self.payBtn:setActive(true)
        	self.payBtnTx:setText(GFunc.getFormatPrice(cfg.recharge_id))
			if rewards and rewards[1] then
				self.rewadCell1:refreshByConfig(rewards[1], false, false)
			end
			self.rewadCell1:hideFrameAnimation()
			self.rewadCell1:setClickShowTips()
			if rewards and rewards[2] then
				self.rewadCell12:refreshByConfig(rewards[2], false, false)
			end
			self.rewadCell12:hideFrameAnimation()
			self.rewadCell12:setClickShowTips()
			self.payBtn:removeRedPoint()
		end
    end
	if cfg.reward and cfg.reward[1] then
		self.rewadCell2:refreshByConfig(cfg.reward[1], isActive, isActive)
	end
	if cfg.value then
		self.offImg:setVisible(true)
		self.offText1:setText(cfg.value .. "%")
	else
		self.offImg:setVisible(false)
	end
end

function MonthlyCardComp:updateTime()
	if DataManager.PrivilegeCardData:getIsMonthlyCardActive() then
		local remainTime = DataManager.PrivilegeCardData:getCardRemainTime(GConst.ShopConst.PRIVILEGE_CARD_ID.MONTHLY_CARD)
		self.descTx7:setText(I18N:getGlobalText(I18N.GlobalConst.MONTHLY_CARD_14, Time:formatNumTimeStr(remainTime)))
	end
end

return MonthlyCardComp