local TalentMainUI = class("TalentMainUI", BaseUI)

local TALENT_CELL = "app/ui/talent/cell/talent_cell"

function TalentMainUI:ctor()
end

function TalentMainUI:isFullScreen()
    return false
end

function TalentMainUI:getCurrencyParams()
	if self.currencyParams == nil then
		self.currencyParams = {
			itemIds = {
				GConst.ItemConst.ITEM_ID_GOLD,
			}
		}
	end
 	return self.currencyParams
end

function TalentMainUI:getPrefabPath()
    return "assets/prefabs/ui/talent/talent_ui.prefab"
end

function TalentMainUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
	uiMap["talent_ui.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	self.getBtn = uiMap["talent_ui.get_btn"]
	self.costIcon = uiMap["talent_ui.get_btn.cost.icon"]
	self.costTx = uiMap["talent_ui.get_btn.cost.cost_tx"]
	self.descTx = uiMap["talent_ui.desc_tx"]
	self.upNode = uiMap["talent_ui.up_node"]
	self.upTalentCell = uiMap["talent_ui.up_node.node.talent_cell"]:addLuaComponent(TALENT_CELL)
	self.upDescTx = uiMap["talent_ui.up_node.desc_tx"]
	self.upNode:setActive(false)

	uiMap["talent_ui.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.TALENT_DESC_1))
	uiMap["talent_ui.get_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.TALENT_DESC_2))
	self.scrollRect = uiMap["talent_ui.bottom_bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.scrollRect:addInitCallback(function()
		return TALENT_CELL
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
		cell:refresh(index, self.list[index])
	end)
	self.scrollRect:clearCells()

	self.getBtn:addClickListener(function()
		if not GFunc.checkCost(self.cfg.cost.id, self.cfg.cost.num, true) then
			return
		end
		local can, stage = DataManager.TalentData:checkStage()
		if not can then
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.TALENT_DESC_3, stage))
			return
		end
		ModuleManager.TalentManager:talentUpgrade()
	end)

	self.upNode:addClickListener(function()
		self.upNode:setActive(false)
	end)

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

function TalentMainUI:onRefresh()
	self.list = DataManager.TalentData:getCfgList()
	self.scrollRect:refillCells(#self.list)

	local stage = DataManager.TalentData:getStage()
	self.cfg = DataManager.TalentData:getPlayerExpCfgList(stage)
	self.costIcon:setSprite(GFunc.getIconRes(self.cfg.cost.id))
	if not GFunc.checkCost(self.cfg.cost.id, self.cfg.cost.num) then
		self.costTx:setText("<color=#FA3939>" .. self.cfg.cost.num .. "</color>")
	else
		self.costTx:setText(self.cfg.cost.num)
	end

	if not DataManager.TalentData:checkStage() then
		self.descTx:setActive(true)
		self.descTx:setText(I18N:getGlobalText(I18N.GlobalConst.TALENT_DESC_3, stage))
	else
		self.descTx:setActive(false)
	end
end

function TalentMainUI:onUpgrade()
	local upData = DataManager.TalentData:getUpData()
	if not upData then
		return
	end
	self.upNode:setActive(true)
	self.upTalentCell:refresh(upData.id, self.list[upData.id], true)
	self.upDescTx:setText(I18N:getText("talent", upData.id, "desc"))
	self.upDescTx:setActive(false)
	self.root:performWithDelayGlobal(function()
		self.upDescTx:setActive(true)
	end, 0.5)
end

return TalentMainUI