local DungeonComp = class("DungeonComp", LuaComponent)

local CELL = "app/ui/dungeon/cell/dungeon_cell"

function DungeonComp:init()
	local uiMap = self:getUIMap()
	self.scrollrect = uiMap["dungeon_comp.content.scrollrect"]

	self:bind(DataManager.DungeonDailyData.GoldData, "isDirty", function()
		self:refresh()
	end)
	self:bind(DataManager.DungeonDailyData.MaterialData, "isDirty", function()
		self:refresh()
	end)
	self:bind(DataManager.DungeonRuneData, "isDirty", function()
		self:refresh()
	end)
end

function DungeonComp:refresh(moduleKey)
	if not self:getBaseObject():getActiveSelf() then
		return
	end
	self:checkRankData()

	self.showList = DataManager.DungeonData:getDungeonList()
	if self.scrollRect == nil then
		self.scrollRect = self.scrollrect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
		self.scrollRect:addInitCallback(function()
			return CELL
		end)
		self.scrollRect:addRefreshCallback(function(index, cell)
			if self.showList[index] then
				cell:refresh(self.showList[index])
			else
				cell:showComingSoon()
			end
		end)
	end
	local cellCount = #self.showList
	if not GFunc.isShenhe() then
		cellCount = cellCount + 1 -- 最后一个显示敬请期待
	end
	if self.scrollRect:getTotalCount() == nil or self.scrollRect:getTotalCount() <= 0 then
		self.scrollRect:refillCells(cellCount)
	elseif self.scrollRect:getTotalCount() ~= cellCount then
		self.scrollRect:clearCells()
		self.scrollRect:refillCells(cellCount)
	else
		self.scrollRect:updateAllCell()
	end

	local findIdx = moduleKey and table.indexof(self.showList, moduleKey) or 0
	if moduleKey then
		self.scrollRect:moveToIndex(findIdx)
	end
end

function DungeonComp:onCrossDay()
	self.checkedRank = false
end

function DungeonComp:checkRankData()
	if self.checkedRank then
		return
	end
	self.checkedRank = true
end

function DungeonComp:updateTime()
	local list = self.scrollRect:getListCell()
	if list then
		for k, v in ipairs(list) do
			v:updateTime()
		end
	end
end

return DungeonComp