local TaskMainUI = class("TaskMainUI", BaseUI)

local PAGE_DAILY = 1
local DAILY_TASK_CELL = "app/ui/task/cell/daily_task_cell"
local ACHIEVEMENT_TASK_CELL = "app/ui/task/cell/achievement_task_cell"

function TaskMainUI:onPressBackspace()
	self:closeUI()
end

function TaskMainUI:isFullScreen()
	return false
end

function TaskMainUI:getPrefabPath()
	return "assets/prefabs/ui/task/task_main_ui.prefab"
end

function TaskMainUI:onClose()
	if self.menuOneRedPointTween then
		self.menuOneRedPointTween:Kill()
	end
	if self.menuTwoRedPointTween then
		self.menuTwoRedPointTween:Kill()
	end
end

function TaskMainUI:ctor(params)
	self.page = params and params.page or PAGE_DAILY
	self.dailyTaskList = {}
	self.achievementTaskList = {}
end

function TaskMainUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["task_main_ui.bg.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	self.titleTx = uiMap["task_main_ui.bg.title_tx"]

	self.btnTxs = {
		I18N:getGlobalText(I18N.GlobalConst.TASK_DAILY),
		I18N:getGlobalText(I18N.GlobalConst.TASK_CHALLENGE),
	}
	self.pageBtns = {}
	self.pageBtnTxs = {}
	for i = 1, 2 do
		self.pageBtns[i] = uiMap["task_main_ui.bg.page_btn_" .. i]
		self.pageBtnTxs[i] = uiMap["task_main_ui.bg.page_btn_" .. i .. ".text"]
		self.pageBtns[i] :addClickListener(function()
			-- if not self:getIsOpen(i) or self.page == i then
			-- 	return
			-- end
			self.page = i
			self:onRefresh()
		end)
	end
	self.menuOneRedPoint = uiMap["task_main_ui.bg.red_point_1"]
	self.menuTwoRedPoint = uiMap["task_main_ui.bg.red_point_2"]

	self.dailyNode = uiMap["task_main_ui.bg.daily"]
	self.dailyTaskFinalCellNameTx = uiMap["task_main_ui.bg.daily.task_final_cell.name_tx"]
	self.dailyTaskFinalCellProgress = uiMap["task_main_ui.bg.daily.task_final_cell.progress"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
	self.dailyTaskFinalCellProgressTx = uiMap["task_main_ui.bg.daily.task_final_cell.progress_tx"]
	self.boxBtns = {}
	self.boxBtnTxs = {}
	self.boxBtnSpines = {}
	local stageIds = DataManager.DailyTaskData:getStageIds()
	for i = 1, 5 do
		self.boxBtns[i] = uiMap["task_main_ui.bg.daily.task_final_cell.box_" .. i]
		self.boxBtnTxs[i] = uiMap["task_main_ui.bg.daily.task_final_cell.box_" .. i .. ".text"]
		self.boxBtnTxs[i]:setText(DataManager.DailyTaskData:getStageCondition(stageIds[i]))
		self.boxBtnSpines[i] = uiMap["task_main_ui.bg.daily.task_final_cell.box_" .. i .. ".ui_spine_obj"]
		-- self.boxBtns[i]:addClickListener(function()
		-- 	GFunc.showToast(i)
		-- 	-- local taskId = DataManager.DailyTaskData:getDailyTaskFinalId()
		-- 	-- if DataManager.DailyTaskData:getDailyTaskCanClaimTask(taskId) then
		-- 	-- 	ModuleManager.TaskManager:claimDailyTask(taskId)
		-- 	-- else
		-- 	-- 	local reward = DataManager.DailyTaskData:getDailyTaskFinalReward()
		-- 	-- 	if reward and reward[1] then
		-- 	-- 		local rewardId = GFunc.getRewardId(reward[1])
		-- 	-- 		local rewardType = GFunc.getRewardType(reward[1])
		-- 	-- 		ModuleManager.TipsManager:showRewardTips(rewardId, rewardType, self.dailyTaskFinalCellBox)
		-- 	-- 	end
		-- 	-- end
		-- end)
	end
	self.dailyTaskScrollRect = uiMap["task_main_ui.bg.daily.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.dailyTaskScrollRect:addInitCallback(function()
		return DAILY_TASK_CELL
	end)
	self.dailyTaskScrollRect:addRefreshCallback(function(index, cell)
		cell:refresh(self.dailyTaskList[index])
	end)
	self.dailyTaskScrollRect:clearCells()

	self.challengeNode = uiMap["task_main_ui.bg.challenge"]
	self.challengeTaskScrollRect = uiMap["task_main_ui.bg.challenge.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.challengeTaskScrollRect:addInitCallback(function()
		return ACHIEVEMENT_TASK_CELL
	end)
	self.challengeTaskScrollRect:addRefreshCallback(function(index, cell)
		cell:refresh(self.achievementTaskList[index])
	end)
	self.challengeTaskScrollRect:clearCells()
	self.challengeTaskScrollRect:refillCells(#self.achievementTaskList)

	self.timeTx = uiMap["task_main_ui.bg.daily.time_tx"]

	self:bindData()
	self:scheduleGlobal(function()
		self:updateTime()
	end, 1)
	self:updateTime()
end

function TaskMainUI:bindData()
	self:bind(DataManager.DailyTaskData, "isDirty", function()
		self:onRefresh()
	end)
end

function TaskMainUI:updateTime()
	if self.page ~= PAGE_DAILY then
		return
	end
	self.timeTx:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_22, Time:formatNumTimeStr(Time:getTodaySurplusTime())))
end

function TaskMainUI:onRefresh()
	self:refreshPageBtn()
	self:refreshPageInfo()
end

function TaskMainUI:refreshPageBtn()
	for i = 1, 2 do
		if self.page == i then
			self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.COMMON, "common_tab_1")
			self.pageBtnTxs[i]:setText(self.btnTxs[i])
		else
			self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.COMMON, "common_tab_2")
			self.pageBtnTxs[i]:setText("<color=#72778C>" .. self.btnTxs[i] .. "</color>")
		end
	end
end

function TaskMainUI:refreshPageInfo()
	self.dailyNode:setActive(self.page == PAGE_DAILY)
	self.challengeNode:setActive(self.page ~= PAGE_DAILY)
	if self.page == PAGE_DAILY then
		self:refreshDaily()
	else
		self:refreshChallenge()
	end
end

function TaskMainUI:refreshDaily()
	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_NAME))
	local stageIds = DataManager.DailyTaskData:getStageIds()
	local curScore = DataManager.DailyTaskData:getCurStageScore()
	local targetList = DataManager.DailyTaskData:getStageTargetList()
	self.dailyTaskFinalCellProgressTx:setText(curScore)
	self.dailyTaskFinalCellProgress.value = GFunc.getUnevenSliderValue(targetList, curScore)

	for i, item in ipairs(self.boxBtns) do
		local stageId = stageIds[i]
		if stageId then
			item:setActive(true)
			if DataManager.DailyTaskData:canClaimStage(stageId) then
				self.boxBtnSpines[i]:playAnim("ready", true, false)
				item:addClickListener(function()
					self.boxBtnSpines[i]:playAnimComplete("open", false, true, function()
						self:onClickBox()
					end)
				end)
			elseif DataManager.DailyTaskData:isStageReceived(stageId) then
				self.boxBtnSpines[i]:playAnim("idle02", true, false)
				item:addClickListener(function()
					ModuleManager.TipsManager:showRewardsTips(DataManager.DailyTaskData:getStageReward(stageId), nil, item)
				end)
			else
				self.boxBtnSpines[i]:playAnim("idle01", true, false)
				item:addClickListener(function()
					ModuleManager.TipsManager:showRewardsTips(DataManager.DailyTaskData:getStageReward(stageId), nil, item)
				end)
			end
		else
			item:setActive(false)
		end
	end

    self.dailyTaskList = DataManager.DailyTaskData:getTaskIdsSort()
	self.dailyTaskScrollRect:refillCells(#self.dailyTaskList)
	if DataManager.DailyTaskData:showAchievementRedPoint() then
		self.menuTwoRedPoint:setActive(true)
		if self.menuTwoRedPointTween == nil then
			self.menuTwoRedPointTween = self:getShakeTween(self.menuTwoRedPoint)
		else
			self.menuTwoRedPointTween:Restart()
		end
	else
		if self.menuTwoRedPointTween then
			self.menuTwoRedPointTween:Pause()
		end
		self.menuTwoRedPoint:setActive(false)
	end
	if self.menuOneRedPointTween then
		self.menuOneRedPointTween:Pause()
	end
	self.menuOneRedPoint:setActive(false)
end

function TaskMainUI:onClickBox()
	ModuleManager.TaskManager:onTaskDailyStageRewardReq()
	-- local isFirst = true
	-- local stageIds = DataManager.DailyTaskData:getStageIds()
	-- for i, item in ipairs(self.boxItems) do
	-- 	local stageId = stageIds[i]
	-- 	if stageId and DataManager.DailyTaskData:canClaimStage(stageId) then
	-- 		item:initPrefabHelper()
	-- 		local map = item:genAllChildren()
	-- 		local spineBox = map["box.spine_box"]

	-- 		if isFirst then
	-- 			isFirst = false
	-- 			spineBox:playAnimComplete("open", false, true, function()
	-- 				ModuleManager.TaskManager:reqTaskStageReward(stageId)
	-- 			end)
	-- 		else
	-- 			spineBox:playAnimComplete("open", false, true)
	-- 		end
	-- 	end
	-- end
end

function TaskMainUI:refreshChallenge()
	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_DESC_2))
	self.achievementTaskList = DataManager.DailyTaskData:getAchievementIdsSort()
	self.challengeTaskScrollRect:refillCells(#self.achievementTaskList)
	if DataManager.DailyTaskData:showDailyRedPoint() then
		self.menuOneRedPoint:setActive(true)
		if self.menuOneRedPointTween == nil then
			self.menuOneRedPointTween = self:getShakeTween(self.menuOneRedPoint)
		else
			self.menuOneRedPointTween:Restart()
		end
	else
		if self.menuOneRedPointTween then
			self.menuOneRedPointTween:Pause()
		end
		self.menuOneRedPoint:setActive(false)
	end
	if self.menuTwoRedPointTween then
		self.menuTwoRedPointTween:Pause()
	end
	self.menuTwoRedPoint:setActive(false)
end

function TaskMainUI:getShakeTween(obj)
	return GFunc.getShakeSeq(obj, false, 0.6, true)
end

return TaskMainUI