local HeroCell = class("HeroCell", BaseCell)

function HeroCell:init()
	local uiMap = self.baseObject:genAllChildren()
	-- 通用
	self.icon = uiMap["hero_cell.hero_bg.icon"]
	self.heroBg = uiMap["hero_cell.hero_bg"]
	self.heroDec = uiMap["hero_cell.hero_bg.dec"]
	self.inTimeBg = uiMap["hero_cell.hero_bg.in_time_bg"]
	self.matchImg = uiMap["hero_cell.hero_bg.match_img"]
	self.lvTx = uiMap["hero_cell.hero_bg.lv_tx"]
	-- 个人节点
	self.selfNode = uiMap["hero_cell.hero_bg.self_node"]
	self.unlockTx = uiMap["hero_cell.hero_bg.unlock_tx"]
	self.lvUpArrow = uiMap["hero_cell.hero_bg.self_node.ui_spine_obj"]
	self.maskImg2 = uiMap["hero_cell.hero_bg.mask_img_2"]
	-- 他人节点
	self.nameTx = uiMap["hero_cell.hero_bg.tx_name"]
	self.starComp = uiMap["hero_cell.hero_bg.star_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.STAR_CELL)
	self.unlockTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_7))
	uiMap["hero_cell.hero_bg.in_time_bg.in_time_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_31))

	self.isGray = false
	self.baseObject:addClickListener(function()
		if self.clickCallback then
			self.clickCallback()
		end
	end)
	self:bind(DataManager.BagData.ItemData, "dirty", function()
        self:refreshRedPoint()
    end)
	self:bind(DataManager.SkinData, "isDirty", function()
        self:refreshHeroIcon()
    end)
end

function HeroCell:refresh(heroEntity, isGray)
	self.heroEntity = heroEntity
	self:bind(self.heroEntity, "isDirty", function()
        self:refreshRedPoint()
    end)

	self.selfNode:setActive(true)

	self:_refresh(heroEntity:getCfgId(), isGray)

	local canLvUp = heroEntity:canLvUp()
	local canStarUp = heroEntity:canStarUp()
	local inTeam = DataManager.HeroData:isInStageFormation(heroEntity:getCfgId())
	if canLvUp and inTeam then
		self.lvUpArrow:setActive(true)
		self.lvUpArrow:playAnim("lv", true, false)
	elseif canStarUp then
		self.lvUpArrow:setActive(true)
		self.lvUpArrow:playAnim("star", true, false)
	else
		self.lvUpArrow:setActive(false)
	end

	if inTeam then
		if DataManager.EquipData:hasEquipRedPoint(heroEntity:getMatchType()) then
			self.baseObject:addRedPoint(74, 68, 1, "common_info_3")
		else
			self.baseObject:removeRedPoint()
		end
	else
		self.baseObject:removeRedPoint()
	end

	local star = heroEntity:getStar()
	self.starComp:refresh(star)
	if heroEntity:isUnlock() then
		if heroEntity:isActived() then
			self.lvTx:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_26, heroEntity:getLv()))
			self.lvTx:setActive(true)
		else
			self.lvTx:setActive(false)
		end
		self.unlockTx:setActive(false)
	else
		self.lvTx:setActive(false)
		if canLvUp then
			self.unlockTx:setActive(false)
		else
			self.unlockTx:setActive(true)
		end
	end
	self:refreshRedPoint()
end

-- 刷新英雄icon
function HeroCell:refreshHeroIcon(iconName)
	-- if self.heroEntity ~= nil then
	-- 	self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO_2, DataManager.SkinData:getIcon(self.heroEntity:getSkinId()))
	-- elseif iconName then
		self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO_2, tostring(iconName))
	-- end
end

-- 刷新红点
function HeroCell:refreshRedPoint()
	if self.heroEntity == nil then
		return
	end

	if not self.selfNode or self.selfNode:isDestroyed() or CS.BF.Utils.IsNull(self.selfNode:getGameObject()) then
		return
	end

	-- if not self.notShowRedPoint and DataManager.FormationData:heroInFormation(GConst.BattleConst.FORMATION_TYPE.STAGE, self.heroEntity:getCfgId()) and self.heroEntity:showRedPointEntrance() then
	-- 	self.selfNode:addRedPoint(59, -47, 1)
	-- else
	-- 	self.selfNode:removeRedPoint()
    -- end
end

-- 设置是否展示红点
function HeroCell:setShowRedPoint(show)
	self.notShowRedPoint = not show
end

function HeroCell:getHeroId()
	if not self.heroEntity then
		return
	end
	return self.heroEntity:getCfgId()
end

function HeroCell:refreshBriefInfo(heroEntity)
	self.heroEntity = heroEntity
	local id = self.heroEntity:getCfgId()
	local level = self.heroEntity:getLv()
	self.selfNode:setActive(false)
	local star = heroEntity:getStar()
	self.starComp:refresh(star)
	self.unlockTx:setActive(false)

	self:_refresh(id)
	self.lvTx:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_26, level))
end

function HeroCell:refreshWithCfgId(id, isGray)
	local heroInfo = ConfigManager:getConfig("hero")[id]
	self:_refresh(id, isGray)

	local lv = heroInfo.begin_lv
	self.lvTx:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_26, lv))
	self.lvTx:setActive(true)
	self.unlockTx:setActive(false)
	self.lvUpArrow:setActive(false)
	self.fragmenImg:setActive(true)
end

function HeroCell:_refresh(id, isGray)
	local heroInfo = ConfigManager:getConfig("hero")[id]
	self.clickCallback = nil
	self.maskImg2:setActive(isGray)
	self.heroBg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.FRAME_QLT[heroInfo.qlt])
	self.heroDec:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HERO_DEC_QLT[heroInfo.qlt])
	self.matchImg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HeroConst.MATCH_ICON_NAME[heroInfo.position])
	self.inTimeBg:setActive(false)
	self:refreshHeroIcon(heroInfo.icon)
	-- self:setGray(isGray)
	self.nameTx:setText(ModuleManager.HeroManager:getHeroName(id))
end

function HeroCell:showCheck(visible)
	self.inTimeBg:setActive(visible)
end

function HeroCell:addClickListener(callback)
	self.clickCallback = callback
end

function HeroCell:setVisible(visible, scale)
	self.baseObject:setVisible(visible, scale)
end

function HeroCell:setSpineVisible(visible, scale)
	self.lvUpArrow:setActive(visible, scale)
end

function HeroCell:setGray(isGray)
	-- if self.isGray == isGray then
	-- 	return
	-- end
	-- self.isGray = isGray
	-- self.icon:setImageGray(isGray)
	-- self.matchImg:setImageGray(isGray)
end

return HeroCell
