local HeroSkillInfoUI = class("HeroSkillInfoUI", BaseUI)

local HERO_CELL = "app/ui/hero/cell/hero_cell"

function HeroSkillInfoUI:isFullScreen()
	return false
end

function HeroSkillInfoUI:getPrefabPath()
	return "assets/prefabs/ui/hero/hero_skill_info_ui.prefab"
end

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

function HeroSkillInfoUI:ctor(parmas)
	self.heroEntity = parmas.heroEntity
	self.idx = parmas.idx or 1
	self.buffId = parmas.buffId
	self.isPop = parmas.isPop
end

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

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

	uiMap["hero_skill_info_ui.content.btn_close"]:addClickListener(function()
		self:closeUI()
	end)

	self.titleTx = uiMap["hero_skill_info_ui.content.tx_title"]
	self.descTx = uiMap["hero_skill_info_ui.content.desc_tx"]
	self.skillIcon = uiMap["hero_skill_info_ui.content.skill_icon"]
	self.icon = uiMap["hero_skill_info_ui.content.skill_icon.icon"]
	self.skillDescTx = uiMap["hero_skill_info_ui.content.skill_icon.desc_tx"]
	self.heroNode = uiMap["hero_skill_info_ui.content.hero_node"]
	-- self.descTx1 = uiMap["hero_skill_info_ui.content.hero_node.desc_tx_1"]
	self.unlockBg = uiMap["hero_skill_info_ui.content.unlock_bg"]
	self.unlockTx = uiMap["hero_skill_info_ui.content.unlock_bg.unlock_tx"]
	self.buffNode = uiMap["hero_skill_info_ui.content.buff_node"]
	self.content = uiMap["hero_skill_info_ui.content"]
	self.line2 = uiMap["hero_skill_info_ui.content.line_2"]
	self.vfx02 = uiMap["hero_skill_info_ui.content.vfx_c1_ui_up_b02"]
	self.vfx02:setActive(false)

	self.heroCells = {}
	self.buffImgs = {}
	self.buffTxs = {}
	self.skillDescTxs = {}
	self.skillVfxs = {}
	for i = 1, 2 do
		self.heroCells[i] = uiMap["hero_skill_info_ui.content.hero_node.hero_cell_" .. i]:addLuaComponent(HERO_CELL)
		self.buffImgs[i] = uiMap["hero_skill_info_ui.content.buff_node.buff_img_" .. i]
		self.buffTxs[i] = uiMap["hero_skill_info_ui.content.buff_node.buff_tx_" .. i]
		self.skillDescTxs[i] = uiMap["hero_skill_info_ui.content.skill_desc_tx_" .. i]
		self.skillVfxs[i] = uiMap["hero_skill_info_ui.content.skill_desc_tx_" .. i .. ".vfx_c1_ui_up_b03"]
		self.skillVfxs[i]:setActive(false)
	end

	uiMap["hero_skill_info_ui.content.hero_node.desc_tx_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_13))
end

function HeroSkillInfoUI:onRefresh()
	self.height = 0
	self:refreshSkillIcon()
	self:refreshSkillDesc()
	self:refreshSkillBuff()
	self:refreshSkillHero()
	self.height = self.height + 36
	self.content:setSizeDeltaY(self.height)
end

function HeroSkillInfoUI:refreshSkillIcon()
	local skillInfo, currIdx = self.heroEntity:getRogueSkillListByIdx(self.idx)

	local lv = self.heroEntity:getLv()
	local skillUnlcokLv = skillInfo[1][1]
	local skillId = skillInfo[1][2]
	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_23, currIdx .. "/" .. #skillInfo))
	
	self.icon:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueIcon(skillId))
	if skillUnlcokLv > lv then
		self.skillIcon:setSprite(GConst.ATLAS_PATH.ICON_ITEM, "frame_1")

		self.unlockTx:setText(I18N:getGlobalText(I18N.GlobalConst.LV_UNLOCK, skillUnlcokLv))
		local meshProComp = self.unlockTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
		local contentWidth = meshProComp.preferredWidth
		self.unlockBg:setSizeDeltaX(contentWidth + 22)
		self.unlockBg:setActive(true)
	else
		self.unlockBg:setActive(false)
		self.skillIcon:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueBg(skillId, true))
	end

	self.descTx:setText(ModuleManager.HeroManager:getSkillRogueDesc(skillInfo[1][2]))
	self.vfx02:setActive(self.isPop)
end

function HeroSkillInfoUI:refreshSkillDesc()
	local skillInfo, currIdx = self.heroEntity:getRogueSkillListByIdx(self.idx)
	local posY = -324
	for i = 1, 2 do
		local skillUnlcokLv = skillInfo[i + 1][1]
		local skillId = skillInfo[i + 1][2]
		local lvStr = I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_14, skillUnlcokLv)
		local skillStr = ModuleManager.HeroManager:getSkillRogueDescEntry(skillId, i)
		if currIdx - 1 >= i then
			self.skillDescTxs[i]:setText("<color=#FFFFFF>" .. lvStr .. "</color><color=#FFA44B>     " .. skillStr .. "</color>")
		else
			self.skillDescTxs[i]:setText("<color=#B4B4B4>" .. lvStr .. "</color><color=#B4B4B4>     " .. skillStr .. "</color>")
		end
		if self.isPop and currIdx - 1 == i then
			self.skillVfxs[i]:setActive(true)
		end
		self.skillDescTxs[i]:setAnchoredPositionY(posY)
		local meshProComp = self.skillDescTxs[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
		local contentHeight = meshProComp.preferredHeight
		posY = posY - contentHeight - 18
	end

	self.line2:setAnchoredPositionY(posY + 4)
	self.height = -posY
end

function HeroSkillInfoUI:refreshSkillBuff()
	if not self.buffId then
		self.buffNode:setActive(false)
		return
	else
		local posY = 0
		self.buffNode:setActive(true)
		self.buffNode:setAnchoredPositionY(-self.height)
		for i = 1, 2 do
			if self.buffId[i] then
				self.buffImgs[i]:setActive(true)
				self.buffTxs[i]:setActive(true)
				local buffInfo = ConfigManager:getConfigWithOtherKey("buff", "name")[self.buffId[i]]
				self.buffTxs[i]:setText(I18N:getTextWithOtherKey("buff", "name", buffInfo.name, "tips_desc"))
				self.buffImgs[i]:setSprite(GConst.ATLAS_PATH.ICON_BUFF, buffInfo.icon)
				self.buffImgs[i]:setAnchoredPositionY(posY - 20)
				self.buffTxs[i]:setAnchoredPositionY(posY - 8)

				local meshProComp = self.buffTxs[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
				local contentHeight = meshProComp.preferredHeight
				posY = posY - contentHeight - 10
			else
				self.buffImgs[i]:setActive(false)
				self.buffTxs[i]:setActive(false)
			end
		end
		self.height = self.height - posY
	end
end

function HeroSkillInfoUI:refreshSkillHero()
	local skillInfo = self.heroEntity:getRogueSkillList()[self.idx]
	local skillId = skillInfo[2]
	local combaHero = ModuleManager.HeroManager:getSkillRogueComboHero(skillId)
	if combaHero then
		self.heroNode:setActive(true)
		for i = 1, 2 do
			if combaHero[i] then
				self.heroCells[i]:setActive(true)
				self.heroCells[i]:refresh(combaHero[i])
			else
				self.heroCells[i]:setActive(false)
			end
		end
		self.height = self.height + 164
	else
		self.heroNode:setActive(false)
	end
end

return HeroSkillInfoUI