local HeroUnlockUI = class("HeroUnlockUI", BaseUI)

function HeroUnlockUI:getPrefabPath()
    return "assets/prefabs/ui/hero/hero_unlock_ui.prefab"
end

function HeroUnlockUI:ctor(params)
	self.heroIdList = params and params.heroIdList
	if self.heroIdList and #self.heroIdList > 0 then
		table.sort(self.heroIdList, function(a, b)
			return a < b
		end)
	end
end

function HeroUnlockUI:onLoadRootComplete()
	self.uiMap = self.root:genAllChildren()
	self.uiMap["hero_unlock_ui.bg"]:addClickListener(function()
		self:closeUI()
	end)
	self:initTitleAndDesc()
	self:initHeroes()
end

function HeroUnlockUI:initTitleAndDesc()
	self.uiMap["player_level_up_ui.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_UNLOCK))
	self.uiMap["player_level_up_ui.reward_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_UNLOCK_DESC))
	self.uiMap["player_level_up_ui.continue"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_TO_CONTINUE))
end

function HeroUnlockUI:initHeroes()
	if self.heroIdList then
		local count = #self.heroIdList
		if count > 3 then
			count = 3
		end
		if count > 0 then
			for i = 1, count do
				local heroEntity = DataManager.HeroData:getHeroById(self.heroIdList[i])
				self.uiMap["hero_unlock_ui.bg_" .. i]:setVisible(true)
				local rewardCell = self.uiMap["hero_unlock_ui.bg_" .. i .. ".reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
				rewardCell:refreshItemById(heroEntity:getFragmentId(), 0)
				self.uiMap["hero_unlock_ui.bg_" .. i .. ".name_tx"]:setText(heroEntity:getName())
				self.uiMap["hero_unlock_ui.bg_" .. i .. ".desc_tx"]:setText(heroEntity:getDesc())
			end
			for i = count + 1, 3 do
				self.uiMap["hero_unlock_ui.bg_" .. i]:setVisible(false)
			end
			if count == 1 then
				self.uiMap["hero_unlock_ui.bg_1"]:setAnchoredPositionY(100)
			else
				self.uiMap["hero_unlock_ui.bg_1"]:setAnchoredPositionY(180)
			end
		else
			self:hideAllHeroes()
		end
	else
		self:hideAllHeroes()
	end
end

function HeroUnlockUI:hideAllHeroes()
	self.uiMap["hero_unlock_ui.bg_1"]:setVisible(false)
	self.uiMap["hero_unlock_ui.bg_2"]:setVisible(false)
	self.uiMap["hero_unlock_ui.bg_3"]:setVisible(false)
end

return HeroUnlockUI