using System;
using InnerNet;
using UnityEngine;

public class GameStartManager : DestroyableSingleton<GameStartManager>, IDisconnectHandler
{
	public int MinPlayers = 4;

	public TextRenderer PlayerCounter;

	private int LastPlayerCount = -1;

	public GameObject GameSizePopup;

	public TextRenderer GameRoomName;

	public LobbyBehaviour LobbyPrefab;

	public TextRenderer GameStartText;

	public SpriteRenderer StartButton;

	public SpriteRenderer MakePublicButton;

	public Sprite PublicGameImage;

	public Sprite PrivateGameImage;

	private GameStartManager.StartingStates startState;

	private float countDownTimer;

	private enum StartingStates
	{
		NotStarting,
		Countdown,
		Starting
	}

	public void Start()
	{
		if (DestroyableSingleton<TutorialManager>.InstanceExists)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		string text = InnerNetClient.IntToGameName(AmongUsClient.Instance.GameId);
		if (text != null)
		{
			this.GameRoomName.Text = DestroyableSingleton<TranslationController>.Instance.GetString(StringNames.RoomCode, Array.Empty<object>()) + "\r\n" + text;
		}
		else
		{
			this.StartButton.transform.localPosition = new Vector3(0f, -0.2f, 0f);
			this.PlayerCounter.transform.localPosition = new Vector3(0f, -0.8f, 0f);
		}
		AmongUsClient.Instance.DisconnectHandlers.AddUnique(this);
		if (!AmongUsClient.Instance.AmHost)
		{
			this.StartButton.gameObject.SetActive(false);
		}
		else
		{
			LobbyBehaviour.Instance = UnityEngine.Object.Instantiate<LobbyBehaviour>(this.LobbyPrefab);
			AmongUsClient.Instance.Spawn(LobbyBehaviour.Instance, -2, SpawnFlags.None);
		}
		this.MakePublicButton.gameObject.SetActive(AmongUsClient.Instance.GameMode == GameModes.OnlineGame);
	}

	public void MakePublic()
	{
		if (AmongUsClient.Instance.AmHost)
		{
			AmongUsClient.Instance.ChangeGamePublic(!AmongUsClient.Instance.IsGamePublic);
		}
	}

	public void Update()
	{
		if (!GameData.Instance)
		{
			return;
		}
		this.MakePublicButton.sprite = (AmongUsClient.Instance.IsGamePublic ? this.PublicGameImage : this.PrivateGameImage);
		if (GameData.Instance.PlayerCount != this.LastPlayerCount)
		{
			this.LastPlayerCount = GameData.Instance.PlayerCount;
			string arg = "[FF0000FF]";
			if (this.LastPlayerCount > this.MinPlayers)
			{
				arg = "[00FF00FF]";
			}
			if (this.LastPlayerCount == this.MinPlayers)
			{
				arg = "[FFFF00FF]";
			}
			this.PlayerCounter.Text = string.Format("{0}{1}/{2}", arg, this.LastPlayerCount, PlayerControl.GameOptions.MaxPlayers);
			this.StartButton.color = ((this.LastPlayerCount >= this.MinPlayers) ? Palette.EnabledColor : Palette.DisabledColor);
			if (DestroyableSingleton<DiscordManager>.InstanceExists)
			{
				if (AmongUsClient.Instance.AmHost && AmongUsClient.Instance.GameMode == GameModes.OnlineGame)
				{
					DestroyableSingleton<DiscordManager>.Instance.SetInLobbyHost(this.LastPlayerCount, AmongUsClient.Instance.GameId);
				}
				else
				{
					DestroyableSingleton<DiscordManager>.Instance.SetInLobbyClient();
				}
			}
		}
		if (AmongUsClient.Instance.AmHost) // host来控制开始游戏
		{
			if (this.startState == GameStartManager.StartingStates.Countdown)
			{
				int num = Mathf.CeilToInt(this.countDownTimer);
				this.countDownTimer -= Time.deltaTime;
				int num2 = Mathf.CeilToInt(this.countDownTimer);
				this.GameStartText.Text = string.Format("Starting in {0}", num2);
				if (num != num2)
				{
					PlayerControl.LocalPlayer.RpcSetStartCounter(num2);
				}
				if (num2 <= 0)
				{
					this.FinallyBegin(); // 开始游戏 
					return;
				}
			}
			else
			{
				this.GameStartText.Text = string.Empty;
			}
		}
	}

	public void ResetStartState()
	{
		this.startState = GameStartManager.StartingStates.NotStarting;
		if (this.StartButton && this.StartButton.gameObject)
		{
			this.StartButton.gameObject.SetActive(AmongUsClient.Instance.AmHost);
		}
		PlayerControl.LocalPlayer.RpcSetStartCounter(-1);
	}

	public void SetStartCounter(sbyte sec)
	{
		if (sec == -1)
		{
			this.GameStartText.Text = string.Empty;
			return;
		}
		this.GameStartText.Text = string.Format("Starting in {0}", sec);
	}

	public void BeginGame()
	{
		if (this.startState != GameStartManager.StartingStates.NotStarting)
		{
			return;
		}
		if (SaveManager.ShowMinPlayerWarning && GameData.Instance.PlayerCount == this.MinPlayers)
		{
			this.GameSizePopup.SetActive(true);
			return;
		}
		if (GameData.Instance.PlayerCount < this.MinPlayers)
		{
			base.StartCoroutine(Effects.Shake(this.PlayerCounter.transform, 0.75f, 0.25f));
			return;
		}
		this.ReallyBegin(false);
	}

	public void ReallyBegin(bool neverShow)
	{
		this.startState = GameStartManager.StartingStates.Countdown;
		if (neverShow)
		{
			SaveManager.ShowMinPlayerWarning = false;
		}
		this.StartButton.gameObject.SetActive(false);
		this.countDownTimer = 10.0001f;
		this.startState = GameStartManager.StartingStates.Countdown;
	}

	public void FinallyBegin()
	{
		if (this.startState != GameStartManager.StartingStates.Countdown)
		{
			return;
		}
		this.startState = GameStartManager.StartingStates.Starting;
		AmongUsClient.Instance.StartGame();
		AmongUsClient.Instance.DisconnectHandlers.Remove(this);
		UnityEngine.Object.Destroy(base.gameObject);
	}

	public void HandleDisconnect(PlayerControl pc, DisconnectReasons reason)
	{
		if (AmongUsClient.Instance.AmHost)
		{
			this.LastPlayerCount = -1;
			if (this.StartButton)
			{
				this.StartButton.gameObject.SetActive(true);
			}
		}
	}

	public void HandleDisconnect()
	{
		this.HandleDisconnect(null, DisconnectReasons.ExitGame);
	}
}