local LoginUI = class("LoginUI", BaseUI)

function LoginUI:getPreLoadList()
	return {
		[GConst.TYPEOF_UNITY_CLASS.BF_ATLAS] = {
			GConst.ATLAS_PATH.UI_LOGIN
		}
	}
end

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

function LoginUI:getPrefabPath()
	return "assets/prefabs/ui/login/login_ui.prefab"
end

function LoginUI:onClose()
	ModuleManager.LoginManager:removeAllLoginData()
end

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

	self.versionTx = uiMap["login_ui.version"]

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

	local logoObj = uiMap["login_ui.logo"]
	logoObj:setVisible(false)
	logoObj:setLanguageSprite(GConst.ATLAS_PATH.UI_LOGIN, "login", function()
		logoObj:setVisible(true)
	end)
	self.progressTx = uiMap["login_ui.progress_tx"]
	self.progressTx:setText("")
	uiMap["login_ui.loading_text"]:setText(I18N:getGlobalText(I18N.GlobalConst.LOADING_DESC))

	self:initListener()

	local distinctId = LocalData:getDistinctId()
	if distinctId == nil or distinctId == "" then
		distinctId = CS.ThinkingAnalytics.ThinkingAnalyticsAPI.GetDistinctId()
		LocalData:setDistinctId(distinctId)
		BIReport:setIsNewPlayer(true)
		BIReport:postGameLogin(true)
		BIReport:postFirstLoginEvent()
	else
		BIReport:setIsNewPlayer(false)
		BIReport:postGameLogin(false)
	end

	local data = {}
	data.open_num = 1
	CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserAdd(data)

	local copyTx = uiMap["login_ui.copy_account_tx"]
	copyTx:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_COPY_ACOUNT_DESC))
	copyTx:setVisible(true)
	copyTx:addClickListener(function()
		local acountInfo = LocalData:getAccountInfo()
		local str = acountInfo and acountInfo.id
		if not str or str == "" then
			str = distinctId
		end
		GFunc.copyStr(str)
	end)

	ModuleManager.LoginManager:resetServerListStartTime()
	ModuleManager.LoginManager:addServerListCallback(function(serverList)
		if EDITOR_MODE then
			Logger.logHighlight("------------serverList------------")
			Logger.printTable(serverList)
		end

		self.serverList = ModuleManager.LoginManager:refreshServerList(serverList, nil, true)
		ModuleManager.LoginManager:saveAuthArgs(false)
		ModuleManager.LoginManager:initSocket()
	end)
	ModuleManager.LoginManager:getServerList()

	CS.BF.BFMain.Instance.LuaMgr:OnGameInitSucc()
	LocalData:save()
end

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

function LoginUI: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 LoginUI: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

return LoginUI