local HeroInfoComp = class("HeroInfoComp", LuaComponent)

local DEFAULT_FACTOR = GConst.BattleConst.DEFAULT_FACTOR
local BTN_ICON = {"common_btn_blue_1", "common_btn_yellow_1", "common_btn_grey_1"}
local SIZE_DELTA_Y_HERO = 942
local SIZE_DELTA_Y_LOOK = 802

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

	-- self.slider = uiMap["hero_info.fragment_bg.slider"]
	self.imgSkill = uiMap["hero_info.skill_node.skill_icon"]
	-- self.imgFragment = uiMap["hero_info.fragment_bg.fragment_icon"]
	self.btnUp = uiMap["hero_info.up.up_btn"]
	self.txUpdesc = uiMap["hero_info.up.up_btn.text"]
	self.btnUp5 = uiMap["hero_info.up.up_5_btn"]
	self.txUpdesc5 = uiMap["hero_info.up.up_5_btn.text"]
	self.txLv = uiMap["hero_info.lv_desc"]
	self.txElement = uiMap["hero_info.element_bg.element_desc"]
	self.txSkill = uiMap["hero_info.skill_desc"]
	self.btnskillDescTxs = uiMap["hero_info.skill_desc.btn_skill_desc"]
	-- self.txFragmentNum = uiMap["hero_info.fragment_bg.fragment_num"]
	self.txHpName = uiMap["hero_info.bg_6.hp_name_tx"]
	self.txAtkName = uiMap["hero_info.bg_5.atk_name_tx"]
	self.txHp = uiMap["hero_info.bg_6.hp_tx"]
	self.txAtk = uiMap["hero_info.bg_5.atk_tx"]
	-- self.bgFragment = uiMap["hero_info.fragment_bg"]
	self.spineObj = uiMap["hero_info.ui_spine_obj"]
	self.upgrade = uiMap["hero_info.up"]
	self.bgElement = uiMap["hero_info.element_bg"]
	self.btnAttr = uiMap["hero_info.bg_6.attr_btn"]
	self.gotoBtn = uiMap["hero_info.up.goto_btn"]
	self.needStarNode = uiMap["hero_info.up.need_star_node"]
	self.needStarNodeDescTx1 = uiMap["hero_info.up.need_star_node.desc_tx_1"]
	self.needStarNodeStarImg = uiMap["hero_info.up.need_star_node.star_img"]
	self.needStarNodeDescTx2 = uiMap["hero_info.up.need_star_node.desc_tx_2"]
	uiMap["hero_info.up.goto_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_22))
	self.needStarNodeDescTx2:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_20))
	local meshProComp = self.needStarNodeDescTx2:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
	local contentWidth = meshProComp.preferredWidth
	self.needStarNodeDescTx2:setSizeDeltaX(contentWidth)
	self.bg5Vfx01 = uiMap["hero_info.bg_5.vfx_c1_ui_up_b01"]
	self.bg6Vfx01 = uiMap["hero_info.bg_6.vfx_c1_ui_up_b01"]

	self.skill = {}
	self.skillIcon = {}
	self.skillDescTxs = {}
	for i = 1, 4 do
		self.skill[i] = uiMap["hero_info.skill_up_" .. i]
		self.skillIcon[i] = uiMap["hero_info.skill_up_" .. i .. ".icon"]
		self.skillDescTxs[i] = uiMap["hero_info.skill_up_" .. i .. ".desc_tx"]
	end

	self.costCells = {}
	for i = 1, 2 do
		self.costCells[i] = uiMap["hero_info.up.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	end

	self.lvUpVfxs = {}
	for i = 1, 5 do
		self.lvUpVfxs[i] = uiMap["hero_info.vfx_c1_ui_uplight_b0" .. i]
	end

	self.spineObj:setVisible(false)
	self.txUpdesc:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_15, 1))
	self.txUpdesc5:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_15, 5))

	self.btnUp:addClickListener(function()
		ModuleManager.HeroManager:upgradeHero(self.heroEntity:getCfgId(), self.heroEntity)
	end)
	self.btnUp5:addClickListener(function()
		local canLvUpLv, canLvUp = self.heroEntity:getLvUpLv()
		if not canLvUp then
			return
		end
		ModuleManager.HeroManager:upgradeHero(self.heroEntity:getCfgId(), self.heroEntity, canLvUpLv)
	end)
	self.gotoBtn:addClickListener(function()
		self.parentUI:changePage(GConst.HeroConst.PANEL_TYPE.STAR)
	end)
	self.btnskillDescTxs:addClickListener(function()
		local cfg = ConfigManager:getConfig("skill")[self.heroEntity:getBaseSkill()]
		if cfg.buff_id and #cfg.buff_id > 0 then
			ModuleManager.TipsManager:showSkillTips(self.btnskillDescTxs, cfg.buff_id)
		end
	end)
	self.btnAttr:addClickListener(function()
		UIManager:showUI("app/ui/hero/hero_attr_ui", {heroEntity = self.heroEntity})
	end)
	self:bind(DataManager.BagData.ItemData, "dirty", function()
		self:refresh()
	end)
end

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

function HeroInfoComp:setHeroData(heroEntity, onlyLook)
	self.curLevel = heroEntity:getLv()
	self.heroEntity = heroEntity
	self.onlyLook = onlyLook
	self:bind(self.heroEntity, "isDirty", function()
		self:refresh(true)
	end)
end

function HeroInfoComp:refresh(checkLevel)
	local isLvChange = checkLevel and self.curLevel ~= self.heroEntity:getLv()

	self.txLv:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, self.heroEntity:getLv()))
	-- local lvTxW = self.txLv:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
	-- self.txLv:setSizeDeltaX(lvTxW)

	self.txSkill:setText(ModuleManager.HeroManager:getSkillDesc(self.heroEntity:getBaseSkill()))
	self.txHpName:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_2))
	self.txAtkName:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_3))
	self.imgSkill:setSprite(GConst.ATLAS_PATH.ICON_SKILL, ModuleManager.HeroManager:getSkillIcon(self.heroEntity:getBaseSkill()))

	local materials = self.heroEntity:getLvUpMaterials() or {}
	local fragmentCount = DataManager.BagData.ItemData:getItemNumById(self.heroEntity:getFragmentId())
	local needFragmentCount = materials[1] or 1
	-- self.txFragmentNum:setText(fragmentCount .. "/" .. needFragmentCount)

	-- if fragmentCount >= needFragmentCount then
	-- 	self.slider:setSprite(GConst.ATLAS_PATH.COMMON, "common_progress_1", nil, self.slider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER))
	-- else
	-- 	self.slider:setSprite(GConst.ATLAS_PATH.COMMON, "common_progress_2", nil, self.slider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER))
	-- end
	-- self.slider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = fragmentCount / needFragmentCount

	local skillList = self.heroEntity:getRogueSkillList()
	local lv = self.heroEntity:getLv()
	for i = 1, 4 do
		local skillInfo = skillList[i]
		if skillInfo then
			local skillUnlcokLv = skillInfo[1]
			local skillId = skillInfo[2]
			local skillBg = self.skill[i]
			local skillIcon = self.skillIcon[i]
			local skillLv = self.skillDescTxs[i]
			local nextLvUp = self.heroEntity:getNextRougeLvUp(i)
			skillBg:addClickListener(function()
				local cfg = ConfigManager:getConfig("skill_rogue")[skillId]
				-- ModuleManager.TipsManager:showSkillTips(skillIcon, cfg.buff_id, skillId)
				ModuleManager.HeroManager:showHeroSkillInfoUI(self.heroEntity, i, cfg.buff_id)
			end)
			skillIcon:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueIcon(skillId))
			skillBg:setTouchEnable(true)
			if skillUnlcokLv > lv then
				-- skillLv:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, skillLvs[i] or 0))
				skillLv:setText(I18N:getGlobalText(I18N.GlobalConst.LV_UNLOCK, skillUnlcokLv))
				skillBg:setSprite(GConst.ATLAS_PATH.ICON_ITEM, "frame_1")
			else
				if nextLvUp then
					skillLv:setText(nextLvUp .. I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_17))
				else
					skillLv:setText(GConst.EMPTY_STRING)
				end
				skillBg:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueBg(skillId, true))
			end
		end
	end

	-- self.bgFragment:setVisible(not self.heroEntity:isMaxLv())

	local canLvUp = self.heroEntity:canLvUp()
	local nextLv = self.heroEntity:getNextLv()
	-- self.imgFragment:setVisible(not canLvUp)

	local lv = self.heroEntity:getLv()
	local hpStr
	local atkStr
	if not self.heroEntity:isActived() then
		hpStr = self.heroEntity:getCfgHp(self.heroEntity:getBeginLv()) // DEFAULT_FACTOR
		atkStr = self.heroEntity:getCfgAtk(self.heroEntity:getBeginLv()) // DEFAULT_FACTOR
		self.btnUp:setActive(false)
		self.btnUp5:setActive(false)
		self.gotoBtn:setActive(false)
		self.needStarNode:setActive(false)
		for i,v in ipairs(self.costCells) do
			v:setActive(false)
		end
		self.upgrade:setVisible(false)
	else
		self.upgrade:setVisible(not self.onlyLook)
		local curHp = self.heroEntity:getHp() // DEFAULT_FACTOR
		local curAtk = self.heroEntity:getAtk() // DEFAULT_FACTOR
		local addHp = (self.heroEntity:getCfgHp(lv + 1) - self.heroEntity:getCfgHp()) // DEFAULT_FACTOR
		local addAtk = (self.heroEntity:getCfgAtk(lv + 1) - self.heroEntity:getCfgAtk()) // DEFAULT_FACTOR
		if addHp <= 0 then
			hpStr = curHp
		else
			hpStr = curHp .. "<color=#A2FF29>+" .. addHp .. "</color>"
		end
		if addAtk <= 0 then
			atkStr = curAtk
		else
			atkStr = curAtk .. "<color=#A2FF29>+" .. addAtk .. "</color>"
		end
		if self.heroEntity:isMaxLv() then
			self.btnUp:setActive(false)
			self.btnUp5:setActive(false)
			self.gotoBtn:setActive(false)
			self.needStarNode:setActive(false)
			for i,v in ipairs(self.costCells) do
				v:setActive(false)
			end
		elseif lv >= nextLv then
			self.btnUp:setActive(false)
			self.btnUp5:setActive(false)
			self.gotoBtn:setActive(true)
			self.needStarNode:setActive(true)
			local star = self.heroEntity:getStar()
			local currStar = star%5 + 1
			self.needStarNodeDescTx1:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_19, currStar))
			local meshProComp = self.needStarNodeDescTx1:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
			local contentWidth = meshProComp.preferredWidth
			self.needStarNodeDescTx1:setSizeDeltaX(contentWidth)
			self.needStarNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
			local starType = math.ceil(currStar / 5)
			self.needStarNodeStarImg:setSprite(GFunc.getStarImg(starType))

			for i,v in ipairs(self.costCells) do
				v:setActive(false)
			end
		else
			self.btnUp:setActive(true)
			self.btnUp5:setActive(true)
			self.gotoBtn:setActive(false)
			self.needStarNode:setActive(false)

			local costs = {}
			costs[1] = {id = self.heroEntity:getFragmentId(), num = materials[1], type = 1}
			costs[2] = {id = self.heroEntity:getLvUpCostId(), num = materials[2], type = 1}
			local hadNums = {DataManager.BagData.ItemData:getItemNumById(costs[1].id), DataManager.BagData.ItemData:getItemNumById(costs[2].id)}
			for i,v in ipairs(self.costCells) do
				v:setActive(true)
				v:refreshByConfig(costs[i])
				if hadNums[i] >= costs[i].num then
					v:setNumTx(hadNums[i] .. "/" .. costs[i].num)
				else
					v:setNumTx("<color=#D82F2D>" .. hadNums[i] .. "</color>" .. "/" .. costs[i].num)
				end
			end

			if canLvUp then
				self.btnUp:setTouchEnable(true)
				self.btnUp5:setTouchEnable(true)
				self.btnUp:addRedPoint(70, 30, 1)
				self.btnUp5:addRedPoint(70, 30, 1)
				self.btnUp:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
				self.btnUp5:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[2])
			else
				self.btnUp:setTouchEnable(false)
				self.btnUp5:setTouchEnable(false)
				self.btnUp:removeRedPoint()
				self.btnUp5:removeRedPoint()
				self.btnUp:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[3])
				self.btnUp5:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[3])
			end
		end
	end
	self.txHp:setText(hpStr)
	self.txAtk:setText(atkStr)
	-- self.needStarNode = uiMap["hero_info.up.need_star_node"]
	-- self.needStarNodeDescTx1 = uiMap["hero_info.up.need_star_node.desc_tx_1"]
	-- self.needStarNodeStarImg = uiMap["hero_info.up.need_star_node.star_img"]
	-- self.needStarNodeDescTx2 = uiMap["hero_info.up.need_star_node.desc_tx_2"]

	for i,v in ipairs(self.lvUpVfxs) do
		v:setActive(false)
	end
	if isLvChange then
		local match = self.heroEntity:getMatchType()
		-- self.spineObj:setVisible(true)
		-- self.spineObj:playAnim("idle", false, true)
		self.lvUpVfxs[match]:setActive(true)
		self.bg5Vfx01:setActive(true)
		self.bg6Vfx01:setActive(true)
	else
		-- self.spineObj:setVisible(false)
		self.bg5Vfx01:setActive(false)
		self.bg6Vfx01:setActive(false)
	end

	local needPop, isUnlock, skillIdx = self.heroEntity:checkSkillUnlock()
	if needPop then
		self.baseObject:performWithDelayGlobal(function()
			local skillInfo = skillList[skillIdx]
			if skillInfo then
				local skillId = skillInfo[2]
				local cfg = ConfigManager:getConfig("skill_rogue")[skillId]
				ModuleManager.HeroManager:showHeroSkillInfoUI(self.heroEntity, skillIdx, cfg.buff_id, isUnlock, needPop)
			end
		end, 0.5)
	end
end

return HeroInfoComp