local HeroInfoComp = class("HeroInfoComp", LuaComponent)

local DEFAULT_FACTOR = GConst.BattleConst.DEFAULT_FACTOR
local BTN_ICON = {"common_btn_green_2", "common_btn_grey_2"}
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.btnSkillDesc = 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"]
	self.txAtkName = uiMap["hero_info.atk_name"]
	self.txHp = uiMap["hero_info.bg_6.hp"]
	self.txAtk = uiMap["hero_info.atk"]
	self.bgFragment = uiMap["hero_info.fragment_bg"]
	self.spineObjSkill = uiMap["hero_info.ui_spine_obj_skill"]
	self.spineObjLv = uiMap["hero_info.ui_spine_obj_lv"]
	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.btn_attr"]

	self.skill = {}
	self.skillIcon = {}
	self.skillDesc = {}
	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.skillDesc[i] = uiMap["hero_info.skill_up_" .. i .. ".desc"]
	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.spineObjSkill:setVisible(false)
	self.spineObjLv:setVisible(false)
	self.spineObj:setVisible(false)
	self.txUpdesc:setText("升1级")
	self.txUpdesc5:setText("升5级")

	self.btnUp:addClickListener(function()
		ModuleManager.HeroManager:upgradeHero(self.heroEntity:getCfgId(), self.heroEntity)
	end)
	self.btnUp5:addClickListener(function()
		local canLvUpLv = self.heroEntity:getLvUpLv()
		ModuleManager.HeroManager:upgradeHero(self.heroEntity:getCfgId(), self.heroEntity, canLvUpLv)
	end)
	self.btnSkillDesc:addClickListener(function()
		local cfg = ConfigManager:getConfig("skill")[self.heroEntity:getBaseSkill()]
		if cfg.buff_id and #cfg.buff_id > 0 then
			ModuleManager.TipsManager:showSkillTips(self.btnSkillDesc, 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: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_BIG, 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.skillDesc[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)
			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(skillUnlcokLv .. "级解锁")
				skillBg:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, "frame_0")
			else
				if nextLvUp then
					skillLv:setText(nextLvUp .. "提升")
				else
					skillLv:setText(GConst.EMPTY_STRING)
				end
				skillBg:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueBg(skillId, true))
				-- if i == activeCount and isLvChange and self.heroEntity:getLv() == skillLvs[i] then
				-- 	local x, y = skillBg:fastGetAnchoredPosition()
				-- 	self.spineObjSkill:setAnchoredPosition(x, y)
				-- 	self.spineObjSkill:setVisible(true)
				-- 	self.spineObjSkill:playAnim("idle", false, true)
				-- end
			end
		end
	end

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

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

	local lv = self.heroEntity:getLv()
	local str
	local hpStr
	local atkStr
	if self.heroEntity:isActived() then
		str = I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4)
		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
	else
		str = I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_5)
		hpStr = self.heroEntity:getCfgHp(self.heroEntity:getBeginLv()) // DEFAULT_FACTOR
		atkStr = self.heroEntity:getCfgAtk(self.heroEntity:getBeginLv()) // DEFAULT_FACTOR
	end
	local costs = {}
	costs[1] = {id = self.heroEntity:getFragmentId(), num = materials[1], type = 1}
	costs[2] = {id = self.heroEntity:getLvUpCostId(), num = materials[2], type = 1}
	for i,v in ipairs(self.costCells) do
		v:refreshByConfig(costs[i])
	end
	-- local costId = self.heroEntity:getLvUpCostId()
	-- self.imgUpIcon:setSprite(GFunc.getIconRes(costId))
	-- self.txUpdesc:setText(str)
	-- self.txUpNum:setText(materials[2])
	self.txHp:setText(hpStr)
	self.txAtk:setText(atkStr)
	if canLvUp then
		self.btnUp:addRedPoint(70, 40, 0.6)
	else
		self.btnUp:removeRedPoint()
	end

	self.spineObjLv:setVisible(canLvUp)
	if canLvUp then
		self.btnUp:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
		self.btnUp5:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
		self.spineObjLv:playAnim("animation", true, false)
	else
		self.btnUp:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[2])
		self.btnUp5:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[2])
	end
	self.btnUp:setTouchEnable(true)
	self.btnUp:setActive(not self.heroEntity:isMaxLv())
	self.btnUp5:setTouchEnable(true)
	self.btnUp5:setActive(not self.heroEntity:isMaxLv())

	if isLvChange then
		self.spineObj:setVisible(true)
		self.spineObj:playAnim("idle", false, true)
	end

	if self.onlyLook then -- 仅查看的不显示升级和激活按钮
		self.upgrade:setVisible(false)
		self.baseObject:setSizeDeltaY(SIZE_DELTA_Y_LOOK)
		self.bgFragment:setVisible(false)
		self.spineObjLv:setVisible(false)
		self.txLv:setAnchoredPositionX(lvTxW / 2)
	else
		self.upgrade:setVisible(true)
		self.baseObject:setSizeDeltaY(SIZE_DELTA_Y_HERO)
		self.txLv:setAnchoredPositionX(-44)
		self.bgFragment:setVisible(true)
	end
end

return HeroInfoComp