local SignWeekComp = class("SignWeekComp", LuaComponent)

function SignWeekComp:init()
	local uiMap = self:getBaseObject():genAllChildren()
	self.timeTx = uiMap["sign_bounty_node.top_node.time_bg.text"]
	self.titleTx = uiMap["sign_bounty_node.top_node.title_tx"]
	self.desc1Tx = uiMap["sign_bounty_node.top_node.desc_bg.tx_1"]
	self.desc2Tx = uiMap["sign_bounty_node.top_node.desc_bg.tx_2"]
	self.lockFlag = uiMap["sign_bounty_node.top_node.bg_2.lock_flag"]
	self.gotFlag = uiMap["sign_bounty_node.top_node.bg_2.got_flag"]
	self.signTx = uiMap["sign_bounty_node.top_node.bg_2.sign_day_tx"]
	self.cardDescTx = uiMap["sign_bounty_node.top_node.bg_2.desc_tx"]
	self.buyBtn = uiMap["sign_bounty_node.top_node.bg_2.buy_btn"]
    self.buyTx = uiMap["sign_bounty_node.top_node.bg_2.buy_btn.text"]

	self.bg = uiMap["sign_bounty_node.bg"]
	self.scrollRectPrefab = uiMap["sign_bounty_node.bg.scroll_rect"]

	self.buyBtn:addClickListener(function()
		self:buyCard()	
	end)
	self:bind(DataManager.SignWeekData, "isDirty", function()
		self:refresh()
	end)
end

function SignWeekComp:refresh()
	local giftCfg = ConfigManager:getConfig("act_gift")[DataManager.SignWeekData:getProRewardGiftId()]
	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_1))
	self.desc1Tx:setText(I18N:getGlobalText(I18N.GlobalConst.FUND_DESC_3, giftCfg.value))
	self.desc2Tx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_2))
	self.buyTx:setText(GFunc.getFormatPrice(giftCfg.recharge_id))


	self:refreshTop()
	self:refreshScrollRect()

	-- 尝试请求一次未领取奖励
	ModuleManager.SignManager:reqSignAutoRewardsClaimed()

	BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.SING_WEEK)
end

function SignWeekComp:refreshTop()
	self.signTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_4, DataManager.SignWeekData:getSignDay()))
	self.lockFlag:setActive(not DataManager.SignWeekData:isUnlockPro())
	self.buyBtn:setActive(not DataManager.SignWeekData:isUnlockPro())
	-- self.gotFlag:setActive(DataManager.SignWeekData:isUnlockPro())
	self.cardDescTx:setText(DataManager.SignWeekData:isUnlockPro() and I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_6) or I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_5))
end

function SignWeekComp:refreshScrollRect()
	self.list = DataManager.SignWeekData:getConfigList()

    if self.scrollRect then
	    self.scrollRect:updateAllCell()
	else
		self.scrollRect = self.scrollRectPrefab:getLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
		self.scrollRect:addInitCallback(function()
			return "app/ui/sign/cell/sign_week_cell"
		end)
		self.scrollRect:addRefreshCallback(function(index, cell)
			cell:refresh(index)
		end)
		self.scrollRect:refillCells(#self.list)
    end
	local curDay = DataManager.SignWeekData:getMinCanGetRewardDay()
    self.scrollRect:moveToIndex(curDay)
end

function SignWeekComp:updateTime()
	local remainTime = DataManager.SignWeekData:getRemainTime()
	if remainTime < 0 then
		return
	end
	self.timeTx:setText(I18N:getGlobalText(I18N.GlobalConst.NEXT_REFRESH_DESC, Time:formatNumTimeStr(remainTime)))
end

function SignWeekComp:buyCard()
	local id = DataManager.SignWeekData:getProRewardGiftId()
	PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.ACT_GIFT)
end

return SignWeekComp