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:onClose()
	if self.aniSeq then
		self.aniSeq:Kill()
	end
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.flyTalentCell = uiMap["talent_ui.talent_node.talent_cell"]:addLuaComponent(TALENT_CELL)
	self.flyTalentNode = uiMap["talent_ui.talent_node"]
	self.flyTalentNode:setActive(false)
	self.upDescTx = uiMap["talent_ui.up_node.desc_tx"]
	self.upNode:setActive(false)
	self.touchNode = uiMap["talent_ui.touch_node"]
	self.touchNode:setActive(false)
	self.vfx01s = {}
	self.vfx02s = {}
	for i = 1, 3 do
		self.vfx01s[i] = uiMap["talent_ui.up_node.vfx_c1_ui_tianfu_0" .. i .. "_1"]
		self.vfx02s[i] = uiMap["talent_ui.up_node.vfx_c1_ui_tianfu_0" .. i .. "_2"]
	end

	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))
	uiMap["talent_ui.up_node.continue"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_CLOSE_DESC))
	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()

	local helpBtn = uiMap["talent_ui.top_bg.help_btn"]
	helpBtn:addClickListener(function()
		ModuleManager.TipsManager:showDescTips(I18N:getGlobalText(I18N.GlobalConst.TALENT_HELP), helpBtn)
		local params = {
			type = GConst.TipsConst.HELP_TIPS_TYPE.NORMAL,
			desc = I18N:getGlobalText(I18N.GlobalConst.TALENT_HELP),
		}
		ModuleManager.TipsManager:showHelpTips(params)
	end)

	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()
		-- self.id = math.random(1, 16)
		-- self.flyTalentCell:refresh(self.id, self.list[self.id], true)
		-- local cell = self.scrollRect:getListCellByIndex(self.id)
		-- -- self.scrollRect:moveToIndex(self.id)
		-- self.flyEndPos = GFunc.getTargetAnchoredPosition(cell:getBaseObject(), self.root)
		-- self:fly()
	end)

	self.upNode:addClickListener(function()
		self.upNode:setActive(false)
		if self.id then
			local cell = self.scrollRect:getListCellByIndex(self.id)
			if self.id < 5 or self.id > 12 then
				self.scrollRect:moveToIndex(self.id)
			end
			self.flyEndPos = GFunc.getTargetAnchoredPosition(cell:getBaseObject(), self.root)
			self:fly()
		end
	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
	self.getBtn:setActive(not DataManager.TalentData:isAllMax())
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.flyTalentCell:refresh(upData.id, self.list[upData.id])
	self.flyTalentNode:setActive(false)
	self.id = upData.id

	local attr, num = DataManager.TalentData:getAttrById(self.id)
	local str = I18N:getText("talent", self.id, "desc")
	if attr and #attr > 0 then
		str = str .. GFunc.getFinalAttrValue(attr[1].type, attr[1].num)
	elseif self.id == DataManager.TalentData.SELECT_SKILL_ID then
	elseif self.id == DataManager.TalentData.SKILL_REFESH_ID then
		str = str .. (num or 0)
	elseif self.id == DataManager.TalentData.HP_RESTORE_ID then
		str = str .. (num or 0) // GConst.DEFAULT_FACTOR
	else
		str = str .. (num or 0) // 100 .. "%"
	end
	self.upDescTx:setText(str)
	self.upDescTx:setActive(false)
	self.root:performWithDelayGlobal(function()
		self.upDescTx:setActive(true)
	end, 0.5)

	local qlt = self.list[self.id].qlt
	for i = 1, 3 do
		if i == qlt then
			self.vfx01s[i]:setActive(true)
			self.vfx02s[i]:setActive(true)
		else
			self.vfx01s[i]:setActive(false)
			self.vfx02s[i]:setActive(false)
		end
	end
end

function TalentMainUI:disableTouch()
	self.touchNode:setActive(true)
end

function TalentMainUI:enableTouch()
	self.touchNode:setActive(false)
end

function TalentMainUI:fly()
	if self.aniSeq then
		self.aniSeq:Kill()
	end
	self:disableTouch()
	self.flyTalentNode:setActive(true)
	local scale = 0.56
	local beginPos = {x = 0, y = 98}
	self.flyTalentNode:setAnchoredPosition(beginPos.x, beginPos.y)
	local maxDis = 1000
	local dis = BF.Vector2Distance(beginPos, self.flyEndPos)
	local time = dis/maxDis
	local aniSeq = self.flyTalentNode:createBindTweenSequence()
	-- local transform = self.flyTalentCell:getBaseObject():getTransform()
	aniSeq:Append(self.flyTalentNode:getTransform():DOScale(BF.Vector3(scale, scale, scale), 0.5):SetEase(CS.DG.Tweening.Ease.InCubic))
	aniSeq:Append(self.flyTalentNode:getTransform():DOAnchorPos(self.flyEndPos, time):SetEase(CS.DG.Tweening.Ease.InCubic))
	-- aniSeq:Join(transform:DOScale(BF.Vector3(0.1, 0.1, 0.1), time):SetEase(CS.DG.Tweening.Ease.InCubic))
	aniSeq:AppendCallback(function()
		self.flyTalentNode:setActive(false)
		self.flyTalentNode:setLocalScale(1, 1, 1)
		self:onRefresh()
		DataManager.HeroData:calcPower()
		self:enableTouch()
	end)
	self.aniSeq = aniSeq
end

return TalentMainUI