local StarInfoComp = class("StarInfoComp", LuaComponent)

local STAR_CELL = "app/ui/hero/cell/star_info_cell"

function StarInfoComp:init()
	local uiMap = self:getUIMap()

	self.upBtn = uiMap["star_info.upgrade.up_btn"]
	self.upBtnTx = uiMap["star_info.upgrade.up_btn.text"]
	self.descTx = uiMap["star_info.upgrade.desc_tx"]
	self.upgrade = uiMap["star_info.upgrade"]
	self.bg = uiMap["star_info.bg"]
	self.vfx04 = uiMap["star_info.bg.scrollrect.viewport.content.vfx_c1_ui_up_b04"]
	self.vfx04:setActive(false)

	self.attrBg = uiMap["star_info.attr_bg"]
	self.attrNode = uiMap["star_info.attr_bg.attr_node"]
	local descTx = uiMap["star_info.attr_bg.attr_node.desc_tx_1"]
	self.attrTx = uiMap["star_info.attr_bg.attr_node.desc_tx_2"]

	uiMap["star_info.attr_bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_24))
	descTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_25))
	local meshProComp = descTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
	local contentWidth = meshProComp.preferredWidth
	descTx:setSizeDeltaX(contentWidth)

	self.costCell = uiMap["star_info.upgrade.reward_cell_1"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	self.upBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_16))
	self.upBtn:addClickListener(function()
		ModuleManager.HeroManager:upgradeHeroStar(self.heroEntity:getCfgId())
	end)
	self.attrBg:addClickListener(function()
		ModuleManager.HeroManager:showAttrAllUI()
	end)

	self.scrollrect = uiMap["star_info.bg.scrollrect"]
	self.scrollRectComp = uiMap["star_info.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRectComp:addInitCallback(function()
		return STAR_CELL
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.heroEntity, index, self.starUp)
	end)
    self.scrollRectComp:clearCells()

	local rect = self.baseObject:getRectSize()
	self.bg:setSizeDeltaY(rect.height - 620)
end

function StarInfoComp:setParentUI(parentUI)
    self.parentUI = parentUI
end

function StarInfoComp:setHeroData(heroEntity, onlyLook, formationType)
	self.heroEntity = heroEntity
	self.onlyLook = onlyLook
	self.formationType = formationType
	-- self:bind(self.heroEntity, "isDirty", function()
	-- 	self:refresh(true)
	-- end)
end

function StarInfoComp:refresh(starUp)
	self.starUp = starUp
	self:refreshStarBtn()
	self:refreshStarInfo()
	self:refreshAttrInfo()
end

function StarInfoComp:refreshStarBtn()
	if not self.heroEntity:isActived() or self.heroEntity:getIsStarMax() then
		self.upgrade:setActive(false)
		local rect = self.baseObject:getRectSize()
		self.bg:setAnchoredPositionY(150)
		self.bg:setSizeDeltaY(rect.height - 488)
	else
		self.upgrade:setActive(true)
		self.upBtn:setActive(true)
		self.descTx:setActive(false)
		local rect = self.baseObject:getRectSize()
		self.bg:setAnchoredPositionY(303)
		self.bg:setSizeDeltaY(rect.height - 640)

		local cost = {id = self.heroEntity:getStarUpCostId(), num = self.heroEntity:getStarUpMaterialNum() or 0, type = 1}
		local hadNum = DataManager.BagData.ItemData:getItemNumById(cost.id)
		self.costCell:setActive(true)
		self.costCell:refreshByConfig(cost)
		if hadNum >= cost.num then
			self.costCell:showRewardNum(hadNum .. "/" .. cost.num)
			self.upBtn:addRedPoint(100, 36, 1)
		else
			self.costCell:showRewardNum("<color=#D82F2D>" .. hadNum .. "</color>" .. "/" .. cost.num)
			self.upBtn:removeRedPoint()
		end
	end
end

function StarInfoComp:refreshStarInfo()
	local maxStarLv = self.heroEntity:getMaxStar()
	self.scrollRectComp:refillCells(maxStarLv)

	self.vfx04:setActive(false)
	if self.starUp then
		local star = self.heroEntity:getStar()
		self.scrollRectComp:moveToIndex(star)
		self.vfx04:setAnchoredPositionY(-34 - (star - 1) * 58)
		self.vfx04:setActive(true)
		self.vfx04:play()
	end
end

function StarInfoComp:refreshAttrInfo()
	-- self.attrTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_26))
	local attr = self.heroEntity:getStarAttr()
	self.attrTx:setText(GFunc.getFinalAttrValue(GConst.ATTR_ALL.ATTR_HPP_ALL, attr.attr_hpp_all or 0))
	local meshProComp = self.attrTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
	local contentWidth = meshProComp.preferredWidth
	self.attrTx:setSizeDeltaX(contentWidth)
	self.attrNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
end

return StarInfoComp