local SignWeekCell = class("SignWeekCell", BaseCell)

function SignWeekCell:init()
	local uiMap = self:getUIMap()	
	self.upSlider = uiMap["bounty_cell.up_slider"]
	self.upIcon = uiMap["bounty_cell.up_slider.icon"]
	self.downSlider = uiMap["bounty_cell.down_slider"]
	self.downIcon = uiMap["bounty_cell.down_slider.icon"]
	self.downSliderCanvas = self.downSlider:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS)
	self.dayIconCanvas = uiMap["bounty_cell.day_icon"]:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS)
	self.dayTx = uiMap["bounty_cell.day_icon.day_tx"]
	self.freeRewardCell = uiMap["bounty_cell.reward_free.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	self.proRewardCells = {}
	for i = 1, 2 do
		self.proRewardCells[i] = uiMap["bounty_cell.reward_pro.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	end
	self.resignFlagTx = uiMap["bounty_cell.resign_tx"]
	self.funcBtn = uiMap["bounty_cell.func_btn"]
	self.funcBtnCostIcon = uiMap["bounty_cell.func_btn.icon"]
	self.funcBtnCostTx = uiMap["bounty_cell.func_btn.resign_tx"]
	self.funcBtnCenterTx = uiMap["bounty_cell.func_btn.center_tx"]
	self.rp = uiMap["bounty_cell.func_btn.rp"]
	self.signFlag = uiMap["bounty_cell.has_sign_flag"]
	self.signFlagTx = uiMap["bounty_cell.has_sign_flag.desc_tx"]
end

function SignWeekCell:refresh(day)
	local curDay = DataManager.SignWeekData:calculateDay()
	local freeGot = DataManager.SignWeekData:getHasGotReward(GConst.SignConst.REWARD_TYPE.FREE, day)
	local proGot = DataManager.SignWeekData:getHasGotReward(GConst.SignConst.REWARD_TYPE.PRO, day)
	local freeCanGet = DataManager.SignWeekData:getCanGetReward(GConst.SignConst.REWARD_TYPE.FREE, day)
	local rewardFree = DataManager.SignWeekData:getReward(GConst.SignConst.REWARD_TYPE.FREE, day)
	local rewardPro1, rewardPro2 = DataManager.SignWeekData:getReward(GConst.SignConst.REWARD_TYPE.PRO, day)
	local cost = DataManager.SignWeekData:getReSignCost()
	local isUnlockPro = DataManager.SignWeekData:isUnlockPro()

	self.dayTx:setText(day)
	self.resignFlagTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_9))
	self.signFlagTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_10))
	self.funcBtnCostTx:setText(cost.num)

	if curDay >= day then -- 处于可领取天数
		if freeGot then -- 已经领过了
			self.funcBtn:setActive(false)
			self.resignFlagTx:setVisible(false)
			self.signFlag:setVisible(true)
		elseif freeCanGet then -- 可领取
			self.funcBtn:setActive(true)
			self.funcBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_yellow_1")
			self.funcBtnCostIcon:setVisible(false)
			self.funcBtnCostTx:setVisible(false)
			self.funcBtnCenterTx:setVisible(true)
			self.funcBtnCenterTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOUNTY_SIGNIN_DESC_8))
			self.resignFlagTx:setVisible(false)
			self.signFlag:setVisible(false)
			self.rp:setVisible(true)
		else
			self.funcBtn:setActive(true)
			self.funcBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_1")
			self.funcBtnCostIcon:setVisible(true)
			self.funcBtnCostTx:setVisible(true)
			GFunc.centerImgAndTx(self.funcBtnCostIcon, self.funcBtnCostTx, 0, nil, -6)
			self.funcBtnCenterTx:setVisible(false)
			self.resignFlagTx:setVisible(true)
			self.signFlag:setVisible(false)
			self.rp:setVisible(false)
		end
	else -- 还未到达可领取天数
		self.funcBtn:setActive(true)
		self.funcBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
		self.funcBtnCostIcon:setVisible(false)
		self.funcBtnCostTx:setVisible(false)
		self.funcBtnCenterTx:setVisible(true)
		self.funcBtnCenterTx:setText(I18N:getGlobalText(I18N.GlobalConst.GIFT_ROUTINE_DESC_11))
		self.resignFlagTx:setVisible(false)
		self.signFlag:setVisible(false)
		self.rp:setVisible(false)
	end

	-- 刷新slider部分
	-- self.upSlider:setVisible(day ~= 1)
	-- self.downSlider:setVisible(day ~= 7)
	self.upIcon:setVisible(day <= curDay)
	self.downIcon:setVisible(day < curDay)

	-- 奖励
	self.freeRewardCell:refreshByConfig(rewardFree, freeGot, freeGot)
	self.proRewardCells[1]:refreshByConfig(rewardPro1, proGot, proGot)
	if rewardPro2 then
		self.proRewardCells[2]:setActive(true)
		self.proRewardCells[2]:refreshByConfig(rewardPro2, proGot, proGot)
	else
		self.proRewardCells[2]:setActive(false)
	end
	self.proRewardCells[1]:showLock(not isUnlockPro)
	self.proRewardCells[2]:showLock(not isUnlockPro)

	if freeGot and isUnlockPro and not proGot then
		self.proRewardCells[1]:showFrameAnimation()
		self.proRewardCells[2]:showFrameAnimation()
		self.proRewardCells[1]:addClickListener(function()
			ModuleManager.SignManager:reqSignClaimed(day, true)
		end)
		self.proRewardCells[2]:addClickListener(function()
			ModuleManager.SignManager:reqSignClaimed(day, true)
		end)
	else
		self.proRewardCells[1]:hideFrameAnimation()
		self.proRewardCells[2]:hideFrameAnimation()
		self.proRewardCells[1]:setClickShowTips()
		self.proRewardCells[2]:setClickShowTips()
	end

	self.funcBtn:addClickListener(function()
		local curDay = DataManager.SignWeekData:calculateDay()
		local freeGot = DataManager.SignWeekData:getHasGotReward(GConst.SignConst.REWARD_TYPE.FREE, day)
		local proGot = DataManager.SignWeekData:getHasGotReward(GConst.SignConst.REWARD_TYPE.PRO, day)
		local freeCanGet = DataManager.SignWeekData:getCanGetReward(GConst.SignConst.REWARD_TYPE.FREE, day)

		if curDay >= day then -- 处于可领取天数
			if freeGot then -- 已经领过了
				-- 不作处理
			elseif freeCanGet then -- 可领取
				ModuleManager.SignManager:reqSignClaimed(day, false)
			else -- 补签
				local cost = DataManager.SignWeekData:getReSignCost()
				if GFunc.checkCost(cost.id, cost.num, true) then
					ModuleManager.SignManager:reqSignReCheckin(day)
				end
			end
		else -- 还未到达可领取天数
			-- 不作处理
		end
	end)
end

return SignWeekCell