local HeroAttrUI = class("HeroAttrUI", BaseUI)
local ATTR_NODE_CELL = "app/ui/hero/cell/attr_node_cell"
local ATTR_NODE_CELL_PATH = "assets/prefabs/ui/hero/cell/attr_node_cell.prefab"

local ATTR_CELLS_PADDING = 60
local ATTR_CELL_HEIGHT = 80
local ATTR_CELL_SPACING_Y = 8

function HeroAttrUI:isFullScreen()
	return false
end

function HeroAttrUI:getPrefabPath()
	return "assets/prefabs/ui/hero/hero_attr_ui.prefab"
end

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

function HeroAttrUI:onClose()
	self.rootNodes:removeAllChildren()
end

function HeroAttrUI:ctor(parmas)
	self.heroEntity = parmas.heroEntity
end

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

	self.txTitle = uiMap["hero_attr_ui.content.tx_title"]
	self.btnClose = uiMap["hero_attr_ui.content.btn_close"]
	self.rootNodes = uiMap["hero_attr_ui.content.ScrollView.Viewport.Content"]

	self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_18))

	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
end

function HeroAttrUI:onRefresh()
	local totalHeight = 0
	for index, node in ipairs(GConst.HeroConst.SHOW_NODE) do
		-- 有皮肤属性时才显示皮肤
		if self:isShowAttr(node) then
			CellManager:loadCellAsync(ATTR_NODE_CELL_PATH, ATTR_NODE_CELL, self.rootNodes, function(cell)
				cell:refresh(self.heroEntity, node)

				local attrCount = cell:getShowAttrCount()
				local nodeHeight = math.ceil(attrCount / 2) * ATTR_CELL_HEIGHT + (math.ceil(attrCount / 2) - 1) * ATTR_CELL_SPACING_Y + ATTR_CELLS_PADDING
				cell.baseObject:setLocalPositionY(-totalHeight)
				cell.baseObject:setSizeDeltaY(nodeHeight)
				totalHeight = totalHeight + nodeHeight
				self.rootNodes:setSizeDeltaY(totalHeight)
			end)
		end
	end
	self.rootNodes:setAnchoredPositionY(0)
end

-- 是否显示属性块
function HeroAttrUI:isShowAttr(node)
	if node == GConst.HeroConst.ATTR_SHOW_SKIN then
		return #DataManager.SkinData:getOwnAllAttr(self.heroEntity) > 0
	end
	if node == GConst.HeroConst.ATTR_SHOW_STAR then
		return table.nums(self.heroEntity:getStarAttr()) > 0
	end
	return true
end

return HeroAttrUI