context: using System;
  using System.Collections;
  using Mirror;
  using TMPro;
  using UnityEngine;
  using UnityEngine.UI;
  using DatabaseAPI;
  using ServerInfo;
  using AdminCommands;
  
  
  namespace UI
  {
  	public class GUI_PreRoundWindow : MonoBehaviour
  	{
  		// Text objects
  		[SerializeField]
  		private TMP_Text currentGameMode = null;
  		[SerializeField]
  		private TMP_Text timer = null;
  		[SerializeField]
  		private TMP_Text playerCount = null;
  		[SerializeField]
  		private TMP_Text readyText = null;
  
  
  		[SerializeField] private TMP_Text loadingText = null;
  
  		[SerializeField] private Scrollbar loadingBar = null;
  
  		[SerializeField] private GameObject normalWindows = null;
  
  		[SerializeField] private GameObject warnText = null;
  
  		[SerializeField] private GameObject notEnoughReady = null;
  
  		// UI panels
  		[SerializeField]
  		private GameObject adminPanel = null;
  		[SerializeField]
  		private GameObject playerWaitPanel = null;
  		[SerializeField]
  		private GameObject mainPanel = null;
  		[SerializeField]
  		private GameObject timerPanel = null;
  		[SerializeField]
  		private GameObject joinPanel = null;
  
  		[SerializeField]
  		private GameObject mapLoadingPanel = null;
  
  		[SerializeField] private GameObject rejoiningRoundPanel = null;
  
  		// Character objects
  		[SerializeField]
  		private GameObject characterCustomization = null;
  
  		[SerializeField]
  		private GUI_JobPreferences localJobPref = null;
  
  		[SerializeField]
  		private Button characterButton = null;
  
  		public GameObject serverInfo;
  
  		// Internal variables
  		private bool doCountdown;
  		private double countdownEndTime;
  		private bool isReady;
  
  		public static GUI_PreRoundWindow Instance;
  
  		private bool startedAlready = false;
  
  		private void Awake()
  		{
  			//localJobPref = null;
  			if (Instance == null)
  			{
  				Instance = this;
  			}
  			else
  			{
  				Destroy(gameObject);
  			}
  		}
  
  		private void OnEnable()
  		{
  			UpdateManager.Add(CallbackType.UPDATE, UpdateMe);
  			EventManager.AddHandler(Event.PostRoundStarted, OnCountdownEnd);
  		}
  
  		private void OnDisable()
  		{
  			startedAlready = false;
  			doCountdown = false;
  			isReady = false;
  			adminPanel.SetActive(false);
  			UpdateManager.Remove(CallbackType.UPDATE, UpdateMe);
  			EventManager.RemoveHandler(Event.PostRoundStarted, OnCountdownEnd);
  		}
  
  		private void UpdateMe()
  		{
  			if (Input.GetKeyDown(KeyCode.F7))
  			{
  				TryShowAdminPanel();
  			}
  
  			if (doCountdown)
  			{
  				UpdateCountdownUI();
  			}
  		}
  
  		private void TryShowAdminPanel()
  		{
  			if (PlayerList.Instance.AdminToken != null)
  			{
  				adminPanel.SetActive(true);
  			}
  		}
  
  		private void OnCountdownEnd()
  		{
  			doCountdown = false;
  			if (isReady)
  			{
  				// Server should spawn the player so hide this window
  				gameObject.SetActive(false);
  			}
  			else
  			{
  				SetUIForJoining();
  			}
  		}
  
  		/// <summary>
  		/// Update the UI based on the current countdown time
  		/// </summary>
  		private void UpdateCountdownUI()
  		{
  			if (NetworkTime.time >= countdownEndTime)
  			{
  				notEnoughReady.SetActive(true);
  				return;
  			}
  			timer.text = TimeSpan.FromSeconds(countdownEndTime - NetworkTime.time).ToString(@"mm\:ss");
  
  			if (GameManager.Instance.QuickLoad && mapLoadingPanel.activeSelf == false)
  			{
  				if (startedAlready == true || this.isActiveAndEnabled == false) return;
  				startedAlready = true;
  				StartCoroutine(WaitForInitialisation());
  			}
  		}
  
  		private IEnumerator WaitForInitialisation()
  		{
  			yield return null;
  			SetReady(true);
  			StartNowButton();
  		}
  
  		public void UpdatePlayerCount(int count)
  		{
  			playerCount.text = count.ToString();
  			currentGameMode.text = GameManager.Instance.GetGameModeName();
  		}
  
  		public void StartNowButton()
  		{
  			AdminCommandsManager.Instance.CmdStartRound();
  		}
  
  		public void SyncCountdown(bool started, double endTime)
  		{
  			Logger.LogFormat("SyncCountdown called with: started={0}, endTime={1}, current NetworkTime={2}", Category.Round,
  				started, endTime, NetworkTime.time);
  			countdownEndTime = endTime;
  			doCountdown = started;
  			if (started)
  			{
  				SetUIForCountdown();
  				// Update the timer now so it doesn't flash 0:00
  				UpdateCountdownUI();
  			}
  			else
  			{
  				SetUIForWaiting();
  			}
  		}
  
  		public void OnCharacterButton()
  		{
  			_ = SoundManager.Play(CommonSounds.Instance.Click01);
  			characterCustomization.SetActive(true);
  		}
  
  		/// <summary>
  		/// Toggle isReady and update the UI
  		/// </summary>
  		public void OnReadyButton()
  		{
  			_ = SoundManager.Play(CommonSounds.Instance.Click01);
  			CharacterCreator.CharacterCustomization customizerScript = characterCustomization.GetComponentInChildren<CharacterCreator.CharacterCustomization>();
  			customizerScript.GetSavedCharacters();
  			if (customizerScript.PlayerCharacters.Count == 0)
  			{
  				warnText.SetActive(true);
  				return;
  			}
  			else
  			{
  				customizerScript.ValidateCurrentCharacter();
  			}
  			SetReady(!isReady);
  			TryShowAdminPanel();
  		}
  
  		/// <summary>
  		/// Show the job select screen
  		/// </summary>
  		public void OnJoinButton()
  		{
  
gt: 			_ = SoundManager.Play(CommonSounds.Instance.Click01);