local SignMonthComp = class("SignMonthComp", LuaComponent)

function SignMonthComp:init()
	local uiMap = self:getBaseObject():genAllChildren()
	if not self.boxObjs then
		self.boxObjs = {}
		for i = 1, 4 do
			self.boxObjs[i] = {
				box = uiMap["sign_month_comp.top_node.bg_2.progress_bg.box_" .. i],
				btnGet = uiMap["sign_month_comp.top_node.bg_2.progress_bg.box_" .. i .. ".btn_get"],
				desc = uiMap["sign_month_comp.top_node.bg_2.progress_bg.box_" .. i .. ".bg.box_desc"],
				rewardCell = uiMap["sign_month_comp.top_node.bg_2.progress_bg.box_" .. i .. ".reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL),
			}
			self.boxObjs[i].btnGet:addClickListener(function()
				ModuleManager.SignManager:reqMonthAccumClaim(DataManager.SignMonthData:getAccumClaimID(i))
			end)
		end
	end
	self.boxObjs[1].desc:setText(7)	
	self.boxObjs[2].desc:setText(14)
	self.boxObjs[3].desc:setText(21)
	self.boxObjs[4].desc:setText(30)

	self.slider = uiMap["sign_month_comp.top_node.bg_2.progress_bg.slider"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
	self.scrollRectPrefab = uiMap["sign_month_comp.bg_2.scroll_rect"]
	self.txAccum = uiMap["sign_month_comp.top_node.bg_2.progress_bg.bg.tx_accum"]
	self.btnSign = uiMap["sign_month_comp.btn_sign"]
	self.btnAdSign = uiMap["sign_month_comp.btn_ad_sign"]
	self.btnNo = uiMap["sign_month_comp.btn_no"]
	self.imgAd = uiMap["sign_month_comp.bg_2.img_bg.btn_ad_sign.img_ad"]
	self.timeBg = uiMap["sign_month_comp.top_node.time_bg"]

	self.titleTx = uiMap["sign_month_comp.top_node.title_tx"]
	self.txTips = uiMap["sign_month_comp.top_node.time_bg.tx_tips"]
	self.txSign = uiMap["sign_month_comp.btn_sign.tx_sign"]
	self.txAdSign = uiMap["sign_month_comp.btn_ad_sign.tx_ad_sign"]
	self.txNo = uiMap["sign_month_comp.btn_no.tx_no"]
	self.txTips2 = uiMap["sign_month_comp.bg_2.scroll_rect.view_port.content.sign_month_cell.btn_ad.tx_tips"]

	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_30SIGNIN_1))
	self.txTips:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_30SIGNIN_3))
	self.txSign:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_8))
	self.txAdSign:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_30SIGNIN_5))
	self.txNo:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_10))
	self.txTips2:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_30SIGNIN_5))

	local meshProComp = self.txTips:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
	self.timeBg:setSizeDeltaX(meshProComp + 55)
	
	self.count = DataManager.SignMonthData:getConfigCount()

	self.btnAdSign:addClickListener(function()
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.AD_SIGN_MONTH_AGAIN, function ()
			ModuleManager.SignManager:reqMonthSignAgain()
		end)
	end)
	self.btnSign:addClickListener(function()
    	ModuleManager.SignManager:reqMonthSign(1)
	end)
	self:bind(DataManager.SignMonthData, "isDirty", function()
		self:refresh()
	end)
end

function SignMonthComp:refresh()
	self:refreshTop()
	self:refreshScrollRect()
	self:refreshBtn()

	BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME.SIGN_MONTH)
end

function SignMonthComp:refreshTop()
	for i = 1, 4 do
		local reward = DataManager.SignMonthData:getReward(GConst.SignConst.MONTH_REWARD_TYPE.BOX, DataManager.SignMonthData:getAccumClaimID(i))
		self.boxObjs[i].rewardCell:refreshByConfig(reward, false, false)
		local state = DataManager.SignMonthData:getBoxState(i)
		if state == GConst.SignConst.BOX_STATE.UNABLE_CLAIM then
			self.boxObjs[i].rewardCell:showMask(false, false)
			self.boxObjs[i].rewardCell:hideFrameAnimation()
			self.boxObjs[i].btnGet:setVisible(false)
		elseif state == GConst.SignConst.BOX_STATE.CAN_CLAIM then
			self.boxObjs[i].rewardCell:showMask(false, false)
			self.boxObjs[i].rewardCell:showFrameAnimation()
			self.boxObjs[i].btnGet:setVisible(true)
		elseif state == GConst.SignConst.BOX_STATE.CLAIMED then
			self.boxObjs[i].rewardCell:showMask(true, true)
			self.boxObjs[i].rewardCell:hideFrameAnimation()
			self.boxObjs[i].btnGet:setVisible(false)
		else
			self.boxObjs[i].btnGet:setVisible(false)
		end
	end
	self.curDay = DataManager.SignMonthData:getCurDay()
	self.txAccum:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_30SIGNIN_4, self.curDay))

	if self.curDay == 0 then
		self.slider.value = 0
	else
		self.slider.value = DataManager.SignMonthData:getSliderValue()
	end
end

function SignMonthComp:refreshBtn()
	if DataManager.PaymentData:getIsSkipAd() then
		self.imgAd:setSprite(GConst.ATLAS_PATH.COMMON,  "common_ad_2")
	else
		self.imgAd:setSprite(GConst.ATLAS_PATH.COMMON,  "common_ad_1")
	end
	if DataManager.SignMonthData:getCanSign() then
		self.btnSign:addRedPoint(89, 42, 1)
		self.btnSign:setActive(true)
		self.btnAdSign:setActive(false)
		self.btnNo:setActive(false)
	elseif not DataManager.SignMonthData:getCanSignAgain() then
		self.btnSign:setActive(false)
		self.btnAdSign:setActive(true)
		self.btnNo:setActive(false)
	else
		self.btnSign:setActive(false)
		self.btnAdSign:setActive(false)
		self.btnNo:setActive(true)
	end
end

function SignMonthComp:refreshScrollRect()
    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_month_cell"
		end)
		self.scrollRect:addRefreshCallback(function(index, cell)
			cell:refresh(index)
		end)
		self.scrollRect:refillCells(self.count)
    end
	if self.curDay > 0 then
		self.scrollRect:moveToIndex(self.curDay)
	end
end

return SignMonthComp