local TestLoginUI = class("TestLoginUI", BaseUI)

function TestLoginUI:ctor()
	self.progress = 0
	self.retryTimes = 0
	self.connectStartTimes = 0
end

function TestLoginUI:getPrefabPath()
	return "assets/prefabs/ui/login/test_login_ui.prefab"
end

function TestLoginUI:onClose()
	ModuleManager.LoginManager:removeAllLoginData()
	if self.dropList then
		self.dropList.onValueChanged:RemoveAllListeners()
	end
end

function TestLoginUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.uiMap = uiMap

	uiMap["test_login_ui.ui_spine_obj"]:playAnimComplete("open", false, true, function()
		uiMap["test_login_ui.ui_spine_obj"]:playAnim("idle", true, true)
	end)

	self.versionTx = uiMap["test_login_ui.version"]
	local version = CS.BF.BFMain.Instance.GameLaunchMgr:GetCurrentVersion()
	self.versionTx:setText(I18N:getGlobalText(I18N.GlobalConst.APP) .. version)

	local logoObj = uiMap["test_login_ui.logo"]
	logoObj:setVisible(false)
	logoObj:setLanguageSprite(GConst.ATLAS_PATH.UI_LOGIN, "login", function()
		logoObj:setVisible(true)
	end)
	self.progressTx = uiMap["test_login_ui.progress_tx"]
	self.progressTx:setText("")
	self.inputField = uiMap["test_login_ui.login_node.input_field"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD)
	self.inputField.text = LocalData:getDistinctId()

	uiMap["test_login_ui.loading_text"]:setText(I18N:getGlobalText(I18N.GlobalConst.LOADING_DESC))
	uiMap["test_login_ui.copy_account_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_COPY_ACOUNT_DESC))
	uiMap["test_login_ui.login_node.login_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.TURNTABLE_BUTTON))
	uiMap["test_login_ui.login_node.login_btn"]:setTouchEnable(true)
	uiMap["test_login_ui.login_node.login_btn"]:addClickListener(function()
		if not self.inputField.text or self.inputField.text == "" then
			return
		end
		LocalData:setDistinctId(self.inputField.text)
		self:loginGame()
	end)

	self.dropList = uiMap["test_login_ui.login_node.dropdown"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_DROP_DOWN)
	self.dropList:ClearOptions()
	self.dropList.onValueChanged:AddListener(function(index)
		if self.serverList[index + 1] then
			self.selectUrl = self.serverList[index + 1].url
			LocalData:setString(LocalData.KEYS.GATE, self.selectUrl)
			LocalData:setString(LocalData.KEYS.LAST_LOGIN_URL, self.selectUrl)
		end
	end)

	self:addEventListener(EventManager.CUSTOM_EVENT.LOGIN_REQ_SUCCESS, function()
		self:preloadAndEnterMaincity()
	end)

	local accountId = LocalData:getAccountInfo().id or GConst.EMPTY_STRING
	local copyTx = uiMap["test_login_ui.copy_account_tx"]
	copyTx:setVisible(accountId ~= GConst.EMPTY_STRING)
	copyTx:addClickListener(function()
		GFunc.copyStr(accountId)
	end)

	ModuleManager.LoginManager:resetServerListStartTime()
	ModuleManager.LoginManager:addServerListCallback(function(serverList)
		self:refreshServerList(serverList)
	end)
	ModuleManager.LoginManager:getServerList()
end

function TestLoginUI:loginGame()
	local uiMap = self.root:genAllChildren()
	local name = uiMap["test_login_ui.login_node.input_field"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_INPUT_FIELD).text
	if name == "" then
		name = nil
	end
	ModuleManager.LoginManager:saveAuthArgs(false, name)
	ModuleManager.LoginManager:initSocket()

	self.uiMap["test_login_ui.login_node.login_btn"]:setTouchEnable(false)
	-- first 可以卸载了, 此项目只会启动时检查一次热更，之后不会再次检查
	CS.BF.BFMain.Instance.LuaMgr:OnGameInitSucc()
	LocalData:save()
end

function TestLoginUI:preloadAndEnterMaincity()
	local WhiteResManager = require "app/common/white_res_manager"
	if WhiteResManager:isLoaded() then
		ModuleManager.LoginManager:loginGame()
		return
	end
	WhiteResManager:gamePreLoad(function(progress)
		self.progress = math.floor(progress * 100)
		self:updateProgress()
	end, function()
		self.progress = 100
		self:updateProgress()
	end)
end

function TestLoginUI:updateProgress()
	if not self.slideSId then
		local curSlideValue = 0
		self.progressTx:setText("0%")

		-- 进度条表现处理一下,看起来更平滑一些,最快的话0.5秒跑完
		self.dt = 0
		self.slideSId = self:scheduleGlobal(function(dt)
			self.dt = self.dt + dt
			if curSlideValue < self.progress then
				curSlideValue = math.floor(self.dt*200)
				if curSlideValue > self.progress then
					curSlideValue = self.progress
				end
				self.progressTx:setText(curSlideValue .. "%")

				-- 为了显示好看，加一个范围
				local vfxPercent = curSlideValue
				if vfxPercent < 2 then
					vfxPercent = 2
				elseif vfxPercent > 98 then
					vfxPercent = 98
				end
			elseif curSlideValue >= 100 then
				self:unscheduleGlobal(self.slideSId)
				self.slideSId = nil
				ModuleManager.LoginManager:loginGame()
			end
		end, 0)
	end
end


function TestLoginUI:refreshServerList(serverList)
	if EDITOR_MODE then
		Logger.logHighlight("get serverList")
		Logger.printTable(serverList)
	end
	self.dropList:ClearOptions()

	self.serverList = serverList or {}
	local optionList = {}
	local lastUrl = LocalData:getString(LocalData.KEYS.LAST_LOGIN_URL, "")
	local index = 1

	local defaultUrl
	for _, info in ipairs(self.serverList) do
		local data = CS.UnityEngine.UI.Dropdown.OptionData(info.name)
		table.insert(optionList, data)
		if not defaultUrl then
			defaultUrl = info.url
		end

		if lastUrl == info.url then
			index = #optionList
			self.selectUrl = lastUrl
		end
	end

	if self.selectUrl == nil or self.selectUrl == "" then
		self.selectUrl = defaultUrl
	end
	LocalData:setString(LocalData.KEYS.GATE, self.selectUrl)
	LocalData:setString(LocalData.KEYS.LAST_LOGIN_URL, self.selectUrl)

	self.dropList:AddOptions(optionList)
	self.dropList:SetValueWithoutNotify(index - 1)
end

return TestLoginUI