local ModuleUnlockUI = class("ModuleUnlockUI", BaseUI)

function ModuleUnlockUI:getPrefabPath()
    return "assets/prefabs/ui/main_city/module_unlock_ui.prefab"
end

function ModuleUnlockUI:isFullScreen()
	return false
end

function ModuleUnlockUI:onClose()
	if self.animAppear then
		self.animAppear:Kill()
		self.animAppear = nil
	end
	if self.animVanish then
		self.animVanish:Kill()
		self.animVanish = nil
	end
end

function ModuleUnlockUI:ctor(params)
	self.chapterId = params and params.chapterId

	self.unlockList = {}
	local cfg = ConfigManager:getConfig("func_open")
	for k, v in pairs(cfg) do
		if v.stage == self.chapterId and v.pop_ups == nil then
			table.insert(self.unlockList, k)
		end
	end
end

function ModuleUnlockUI:onLoadRootComplete()
	self.uiMap = self.root:genAllChildren()

	self.bg = self.uiMap["module_unlock_ui.bg"]
	self.bg:addClickListener(function()
		if not self.canClick then
			return
		end
		self:showModuleUnlockVanishAnim()
	end)
	self.moduleNameTx = self.uiMap["module_unlock_ui.bg.name_tx"]
	self.moduleIcon = self.uiMap["module_unlock_ui.icon"]
	self.titleTx = self.uiMap["module_unlock_ui.bg.spine_title.title_tx"]
	self.spineTitle = self.uiMap["module_unlock_ui.bg.spine_title"]
	self.spineCircle = self.uiMap["module_unlock_ui.icon.spine_circle"]
	self.spineStar = self.uiMap["module_unlock_ui.spine_star"]
	self.uiMap["module_unlock_ui.bg.continue"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_TO_CONTINUE))
	self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.FUNC_UNLOCK))

	self.initIconPos = self.uiMap["module_unlock_ui.init_pos"]:getPosition()
	self.canvasGroupBg = self.bg:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
	self.canvasGroupTitleTx = self.titleTx:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
	self.canvasGroupIcon = self.moduleIcon:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)

	self:checkShowNext()
end

-- 检查是否展示
function ModuleUnlockUI:checkShowNext()
	if #self.unlockList <= 0 then
		self:closeUI()
		return
	end
	self.moduleKey = table.remove(self.unlockList, 1)
	local info = ConfigManager:getConfig("func_open")[self.moduleKey]
	local i18nInfo = I18N:getConfig("func_open")[self.moduleKey]
	if info == nil or i18nInfo == nil then
		self:closeUI()
		return
	end
	self.moduleNameTx:setText(i18nInfo.name)
	if info.icon then
		self.moduleIcon:setSprite(GConst.ATLAS_PATH.MODULE, info.icon)
	end

	-- 侧边栏功能，检查处理侧边栏打开
	if not ModuleManager.MaincityManager:isActivSideBarModule(self.moduleKey) then
		if ModuleManager.MaincityManager:getModuleInSideBarWhere(self.moduleKey) == -1 then
			ModuleManager.MaincityManager:changeMainCityLeftSideBarOpenOrClose()
		elseif ModuleManager.MaincityManager:getModuleInSideBarWhere(self.moduleKey) == 1 then
			ModuleManager.MaincityManager:changeMainCityRightSideBarOpenOrClose()
		end
	end
	self:showModuleUnlockAppearAnim()
	AudioManager:playEffect(AudioManager.EFFECT_ID.FUNC_OPEN)
end

-- 出现
function ModuleUnlockUI:showModuleUnlockAppearAnim()
	self.canClick = false
	self.canvasGroupBg.alpha = 0
	self.canvasGroupTitleTx.alpha = 0
	self.canvasGroupIcon.alpha = 0
	self.titleTx:setLocalScale(0,0,0)
	self.moduleIcon:setLocalScale(1,1,1)
	self.moduleIcon:setPosition(self.initIconPos.x, self.initIconPos.y, self.initIconPos.z)

	self.spineTitle:playAnim("idle", false, true, true)
	self:performWithDelayGlobal(function()
        self.spineCircle:playAnim("idle", false, true, true)
    end, 0.17)
	if self.animAppear == nil then
		self.animAppear = self.root:createBindTweenSequence()
		self.animAppear:Insert(0, self.canvasGroupBg:DOFade(1, 0.14))
		self.animAppear:Insert(0.13, self.canvasGroupIcon:DOFade(1, 0.07))
		self.animAppear:Insert(0.13, self.moduleIcon:getTransform():DOScale(1.2, 0.07))
		self.animAppear:Insert(0.17, self.canvasGroupTitleTx:DOFade(1, 0.1))
		self.animAppear:Insert(0.17, self.titleTx:getTransform():DOScale(1.2, 0.1))
		self.animAppear:Insert(0.2, self.moduleIcon:getTransform():DOScale(1, 0.13))
		self.animAppear:SetAutoKill(false)
		self.animAppear:OnComplete(function()
			self.canClick = true
		end)
	else
		self.animAppear:Restart()
	end
end

-- 消失
function ModuleUnlockUI:showModuleUnlockVanishAnim()
	self.canClick = false
	self.animVanish = self.root:createBindTweenSequence()
	self.animVanish:Insert(0, self.canvasGroupBg:DOFade(0, 0.1))
	self.animVanish:Insert(0, self.titleTx:getTransform():DOScale(1, 0.16))
	self.animVanish:Insert(0, self.moduleIcon:getTransform():DOScale(0.46, 0.1))
	self.animVanish:Insert(0.33, self.canvasGroupIcon:DOFade(0, 0.07))
	local targetPos = ModuleManager.MaincityManager:getModuleEntrancePos(self.moduleKey)
	if targetPos then
		self.spineStar:setPosition(targetPos.x, targetPos.y, targetPos.z)
		self.animVanish:Insert(0.17, self.moduleIcon:getTransform():DOMove(CS.UnityEngine.Vector3(targetPos.x, targetPos.y, targetPos.z), 0.16))
		self.animVanish:OnComplete(function()
			self.animVanish = nil
			self.spineStar:playAnimComplete("idle", false, true, function()
				self:checkShowNext()
			end)
		end)
	else
		self.animVanish:OnComplete(function()
			self.animVanish = nil
			self:checkShowNext()
		end)
	end
end

return ModuleUnlockUI