local SummonWishUI = class("SummonWishUI", BaseUI)

local SummonHeroCell = "app/ui/summon/cell/summon_hero_cell"

function SummonWishUI:isFullScreen()
	return false
end

function SummonWishUI:ctor(params)
	self.page = params.summonId or 1
end

function SummonWishUI:getPrefabPath()
	return "assets/prefabs/ui/summon/summon_wish_ui.prefab"
end

function SummonWishUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.root:addClickListener(function()
		self:closeUI()
	end)
	uiMap["summon_wish_ui.bg.btn_close"]:addClickListener(function()
		self:closeUI()
	end)
	local okBtn = uiMap["summon_wish_ui.bg.ok_btn"]
	self.descTx = uiMap["summon_wish_ui.bg.desc_tx"]
	uiMap["summon_wish_ui.bg.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_WISH_TITLE))
	uiMap["summon_wish_ui.bg.ok_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_WISH_OK))

	self.selHeroCell = uiMap["summon_wish_ui.bg.summon_hero_cell"]:addLuaComponent(SummonHeroCell)
	self.scrollrect = uiMap["summon_wish_ui.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.scrollrect:addInitCallback(function()
		return SummonHeroCell
	end)
	self.scrollrect:addRefreshCallback(function(index, cell)
		cell:refresh(self.wishHeroId, self.list[index], function (heroId)
			if heroId == self.wishHeroId then
				self.wishHeroId = nil
			else
				self.wishHeroId = heroId
			end
			self:onRefresh()
		end)
	end)
	self.scrollrect:clearCells()

	self.list = DataManager.SummonData:getSummonWishConfig(self.page)
	self.wishHeroId = DataManager.SummonData:getSummonWishHeroId(self.page)

	okBtn:addClickListener(function()
		self:saveWish()
	end)

	self:bind(DataManager.SummonData, "isDirty", function()
		self:onRefresh()
	end)
end

function SummonWishUI:onRefresh()
	if self.page == 1 then
		self.descTx:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_WISH_TIPS))
	else
		self.descTx:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_WISH_TIPS_2))
	end
	
	self.scrollrect:refillCells(#self.list)

	self.selHeroCell:refresh(self.wishHeroId, self.wishHeroId, nil, true)
end

function SummonWishUI:saveWish()
	if not self.wishHeroId or self.wishHeroId == 0 then
		GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.BATTLE_FORCE_CHOOSE_DESC_2))
		return
	end
	ModuleManager.SummonManager:onSummonWishSetReq(self.page, self.wishHeroId)
	self:closeUI()
end

return SummonWishUI
