local SummonUnlockUI = class("SummonUnlockUI", BaseUI)

local CELL_HEIGHT = 230
local CELL_HEIGHT_1 = 120
local NOT_OWN_NODE_HEIGHT = 56
local DIFF_HEIGHT = 20
local ForceCell = "app/ui/summon/cell/summon_force_cell"

function SummonUnlockUI:isFullScreen()
	return false
end

function SummonUnlockUI:ctor(params)
	params = params or {}
	self.unlockList = params.unlockList or {}
	self.idx = 1
end

function SummonUnlockUI:getPrefabPath()
	return "assets/prefabs/ui/summon/summon_unlock_ui.prefab"
end

function SummonUnlockUI:onClose()
	self.heroNode:removeAllChildren()
end

function SummonUnlockUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.root:addClickListener(function()
		self:checkUnlockMap()
	end)

	self.heroNode = uiMap["summon_unlock_ui.hero_bg.hero_node"]
	self.nameTx = uiMap["summon_unlock_ui.name_tx"]
	self.matchTx = uiMap["summon_unlock_ui.match_node.match_tx"]
	self.matchImg = uiMap["summon_unlock_ui.match_node.match_tx.match_img"]
	self.qltBg = uiMap["summon_unlock_ui.qlt_bg"]
	self.qltBgTx = uiMap["summon_unlock_ui.qlt_bg.qlt_tx"]
	self.uiSpine = uiMap["summon_unlock_ui.ui_spine_obj"]

	uiMap["summon_unlock_ui.title_bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_3))
	uiMap["summon_unlock_ui.continue_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_TO_CONTINUE))
end

function SummonUnlockUI:onRefresh()
	self.heroNode:removeAllChildren()
	self.heroId = table.remove(self.unlockList, 1)
	local heroEntity = DataManager.HeroData:getHeroById(self.heroId)
	local matchType = heroEntity:getMatchType()
	local qlt = heroEntity:getQlt()
	self.nameTx:setText(heroEntity:getName())
	self.matchTx:setText(ModuleManager.HeroManager:getMatchTypeName(matchType))
	self.matchImg:setSprite(GConst.ATLAS_PATH.ICON_HERO, ModuleManager.HeroManager:getMatchTypeIcon(matchType))
	self.qltBg:setSprite(GFunc.getHeroQltImg(qlt))
	self.qltBgTx:setText(GFunc.getHeroQltStr(qlt))

	self:loadHero()
end

function SummonUnlockUI:loadHero()
	local cfg = DataManager.HeroData:getHeroConfig(self.heroId)
	local qlt = cfg.qlt - 1
	SpineManager:loadHeroAsync(cfg.model_id, self.heroNode, function(spineObject)
		AudioManager:playEffect(AudioManager.EFFECT_ID.UI_SUMMON_SHOW_HERO)
		spineObject:playAnimation("idle", true)
		self.uiSpine:playAnimComplete("open0" .. qlt, false, true, function()
			self.uiSpine:playAnim("idle0" .. qlt, true, true)
		end)
	end)
end

function SummonUnlockUI:checkUnlockMap()
	if #self.unlockList <= 0 then
		self:closeUI()
		return
	end
	self.idx = self.idx + 1
	self:onRefresh()
end

return SummonUnlockUI