local ChapterBoxUI = class("ChapterBoxUI", BaseUI)

function ChapterBoxUI:isFullScreen()
	return false
end

function ChapterBoxUI:getPrefabPath()
	return "assets/prefabs/ui/main_city/chapter_box_ui.prefab"
end

function ChapterBoxUI:ctor(params)
	self.chapterId = params.chapterId
end

function ChapterBoxUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()

	uiMap["chapter_box_ui.bg.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	self.selImg = uiMap["chapter_box_ui.bg.bg1.sel_img"]
	self.maxWaveTx = uiMap["chapter_box_ui.bg.max_wave_tx"]

	uiMap["chapter_box_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CHAPTER_WAVE_REAWRD_2))

	self.chapterPage = DataManager.ChapterData:getChapterPage(self.chapterId)
	self.chapterStage = DataManager.ChapterData:getChapterStage(self.chapterId)
	local chapterList = DataManager.ChapterData:getChapterList(self.chapterPage)
	self.chapterTxs = {}
	for i = 1, 5 do
		self.chapterTxs[i] = uiMap["chapter_box_ui.bg.bg1.tx_node.chapter_tx_" .. i]
		if i <= #chapterList then
			self.chapterTxs[i]:setActive(true)
		else
			self.chapterTxs[i]:setActive(false)
		end
		self.chapterTxs[i]:addClickListener(function()
			if self.chapterStage == i then
				return
			end
			self.chapterStage = i
			self:onRefresh()
		end)
	end
	uiMap["chapter_box_ui.bg.bg1"]:setSizeDeltaX(#chapterList * 118)
	uiMap["chapter_box_ui.bg.bg1.tx_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()

	self.scrollRect = uiMap["chapter_box_ui.bg.scrollrect"]
	self.scrollRectComp = self.scrollRect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.scrollRectComp:addInitCallback(function()
		return "app/ui/main_city/cell/chapter_box_cell"
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.chapterId, index)
	end)
	self.scrollRectComp:setTotalCount(0)

	self:bind(DataManager.ChapterData, "isDirty", function()
		self:onRefresh()
	end)
end

function ChapterBoxUI:onRefresh()
	self.chapterId = DataManager.ChapterData:getChapterCfgByPageAndStage(self.chapterPage, self.chapterStage)
	for i = 1, 5 do
		self.chapterTxs[i]:setText(self.chapterPage .. "-" .. i)
	end
	self.selImg:setAnchoredPositionX((self.chapterStage - 0.5) * 118)
	local list = DataManager.ChapterData:getChapterBoxRewardList(self.chapterId)
	self.scrollRectComp:refillCells(#list)

	self.maxWaveTx:setText(I18N:getGlobalText(I18N.GlobalConst.CHAPTER_DESC_1, DataManager.ChapterData:getChapterMaxWave(self.chapterId)))
end

return ChapterBoxUI