-- 英雄编队
local HeroFormationComp = class("HeroFormationComp", LuaComponent)

function HeroFormationComp:init()
    local uiMap = self.baseObject:genAllChildren()

    self.heroCells = {
		uiMap["hero_formation_comp.hero_cell_1"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
		uiMap["hero_formation_comp.hero_cell_2"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
		uiMap["hero_formation_comp.hero_cell_3"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
		uiMap["hero_formation_comp.hero_cell_4"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
		uiMap["hero_formation_comp.hero_cell_5"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
	}
	self:setFormationActive(true)
end

-- 显示英雄升级/使用等等信息
function HeroFormationComp:refresh()
	local formation = DataManager.FormationData:getStageFormation()
	for i, heroCell in ipairs(self.heroCells) do
		if formation[i] then
			local heroEntity = DataManager.HeroData:getHeroById(formation[i])
			if heroEntity then
				heroCell:setVisible(true, 1)
				heroCell:refresh(heroEntity)
				heroCell:addClickListener(function()
					ModuleManager.HeroManager:showHeroDetailUI(heroEntity:getCfgId())
				end)
			else
				heroCell:setVisible(false)
			end
		else
			heroCell:setVisible(false)
		end
	end
end

function HeroFormationComp:refreshByFormation(formation)
	for i, heroCell in ipairs(self.heroCells) do
		if formation[i] then
			local heroEntity = DataManager.HeroData:getHeroById(formation[i])
			if heroEntity then
				heroCell:setVisible(true, 1)
				heroCell:refresh(heroEntity)
				heroCell:addClickListener(function()
					ModuleManager.HeroManager:showHeroDetailUI(heroEntity:getCfgId())
				end)
			else
				heroCell:setVisible(false)
			end
		else
			heroCell:setVisible(false)
		end
	end
end

function HeroFormationComp:refreshByEntitys(formation)
	for i, heroCell in ipairs(self.heroCells) do
		local heroEntity = formation[i]
		if heroEntity then
			heroCell:setVisible(true, 1)
			heroCell:refreshBriefInfo(heroEntity)
			heroCell:addClickListener(function()
				ModuleManager.HeroManager:showHeroDetailUI(heroEntity:getCfgId(), true, heroEntity)
			end)
		else
			heroCell:setVisible(false)
		end
	end
end

function HeroFormationComp:setFormationActive(active)
    self.baseObject:setActive(active)
end

function HeroFormationComp:getFormationPosY()
    return self.baseObject:fastGetAnchoredPositionY()
end

return HeroFormationComp