local BaseTips = require "app/ui/tips/base_tips"
local SkillTips = class("SkillTips", BaseTips)
local LINE_SIZE = 4
-- 技能描述+buff描述
local WIDTH_SKILL = 260
local PADDING_SKILL = 17
local SPACING_SKILL = 10
local ICON_SIZE_SKILL = 31
local BUFF_TX_WIDTH_SKILL = 185
-- buff描述
local WIDTH_BUFF = 450
local PADDING_BUFF = 22
local SPACING_BUFF = 13
local ICON_SIZE_BUFF = 36
local BUFF_TX_WIDTH_BUFF = 370


function SkillTips:ctor(params)
    self.tarCornerScreenPos = params.tarCornerScreenPos
	self.buffNames = params.buffNames
	self.skillId = params.skillId
	self.location = self.skillId ~= nil and ModuleManager.TipsManager.ALIGN_TYPE.TOP_CENTER or ModuleManager.TipsManager.ALIGN_TYPE.BOTTOM_CENTER
end

function SkillTips:getPrefabPath()
	return "assets/prefabs/ui/tips/skill_tips.prefab"
end

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

	self.content = uiMap["skill_tips.content"]
	self.bg = uiMap["skill_tips.content.bg"]
	self.imgArrow = uiMap["skill_tips.content.bg.img_arrow"]
	self.txDesc = uiMap["skill_tips.content.bg.tx_desc"]
	self.skillList = {}
	for i = 1, 3 do
		table.insert(self.skillList, uiMap["skill_tips.content.bg.item_desc_" .. i])
	end
	self.lineList = {}
	for i = 1, 3 do
		table.insert(self.lineList, uiMap["skill_tips.content.bg.line_" .. i])
	end

    local tipsBgTransform = self.bg:getTransform()
    self.originSizeDelta = tipsBgTransform.sizeDelta
	self.originPivot = tipsBgTransform.pivot
	self.originAnchoredPosition = tipsBgTransform.anchoredPosition
	self.originLocalPosition = tipsBgTransform.localPosition
end

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

	self:refreshSkillDesc()

    if self.tarCornerScreenPos then
        self:locate(self.location, self.originSizeDelta, self.bg, self.tarCornerScreenPos)
    end
end

function SkillTips:onClose()
	if self.originSizeDelta then
		local tipsBgTransform = self.bg:getTransform()
		tipsBgTransform.sizeDelta = self.originSizeDelta
		tipsBgTransform.pivot = self.originPivot
		tipsBgTransform.anchoredPosition = self.originAnchoredPosition
		tipsBgTransform.localPosition = self.originLocalPosition
	end
end

-- 刷新技能描述
function SkillTips:refreshSkillDesc()
	local width = self.skillId ~= nil and WIDTH_SKILL or WIDTH_BUFF
	local padding = self.skillId ~= nil and PADDING_SKILL or PADDING_BUFF
	local spacing = self.skillId ~= nil and SPACING_SKILL or SPACING_BUFF
	local iconSize = self.skillId ~= nil and ICON_SIZE_SKILL or ICON_SIZE_BUFF
	local buffWidth = self.skillId ~= nil and BUFF_TX_WIDTH_SKILL or BUFF_TX_WIDTH_BUFF
	self.bg:setSizeDeltaX(width)
	self.imgArrow:setActive(self.skillId ~= nil)

	-- Logger.logHighlight("显示buff：")
	-- Logger.printTable(self.buffNames)

	local heightObjList = {}
	local txHeroList = {}

	-- 技能描述
	if self.skillId then
		self.txDesc:setActive(true)
		self.txDesc:setSizeDeltaX(width - 20)
		self.txDesc:setText(ModuleManager.HeroManager:getSkillRogueDesc(self.skillId))

		local txMeshPro = self.txDesc:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
		txMeshPro:ForceMeshUpdate()
		table.insert(heightObjList, self.txDesc)
		table.insert(txHeroList, txMeshPro.renderedHeight)
	else
		self.txDesc:setActive(false)
	end

	-- buff描述
	for index, obj in ipairs(self.skillList) do
		if self.buffNames and self.buffNames[index] then
			-- 显示buff描述
			local ui = obj:genAllChildren()
			local icon = ui["img_icon"]
			local desc = ui["tx_desc"]

			local buffInfo = ConfigManager:getConfigWithOtherKey("buff", "name")[self.buffNames[index]]
			obj:setActive(true)
			obj:setSizeDeltaX(width)
			desc:setSizeDeltaX(buffWidth)
			desc:setText(I18N:getTextWithOtherKey("buff", "name", buffInfo.name, "tips_desc"))
			icon:setSizeDelta(iconSize, iconSize)
			icon:setSprite(GConst.ATLAS_PATH.ICON_BUFF, buffInfo.icon)

			local txMeshPro = desc:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
			txMeshPro:ForceMeshUpdate()
			table.insert(heightObjList, obj)
			table.insert(txHeroList, txMeshPro.renderedHeight > iconSize and txMeshPro.renderedHeight or iconSize)
		else
			obj:setActive(false)
		end
	end

	local h = padding
	local str = "tips高度：" .. padding
	for index, obj in ipairs(heightObjList) do
		obj:setAnchoredPositionY(-h)
		h = h + txHeroList[index]
		str = str .. " + " .. txHeroList[index]

		if index < #heightObjList then
			h = h + spacing
			str = str .. " + " .. spacing
			self.lineList[1]:setAnchoredPositionY(-h)
			self.lineList[1]:setActive(true)
			self.lineList[1]:setSizeDelta(width - 10, LINE_SIZE)
			h = h + LINE_SIZE + spacing
			str = str .. " + " .. LINE_SIZE
			str = str .. " + " .. spacing
			table.remove(self.lineList, 1)
		end
	end
	for index, obj in ipairs(self.lineList) do
		obj:setActive(false)
	end
	h = h + padding
	str = str .. " + " .. padding
	-- Logger.logHighlight(str.. " = " ..h)
	self.bg:setSizeDeltaY(h)
end

return SkillTips